{"record":{"id":"c5f0898859ddb5c6","repo":"NationalSecurityAgency/ghidra","slug":"length-must-match-the-register","errorCode":null,"errorMessage":"Length must match the register","messagePattern":"Length must match the register","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/memory/DBTraceObjectRegister.java","lineNumber":68,"sourceCode":"\t}\n\n\t@Override\n\tpublic String getName() {\n\t\tKeyPath path = object.getCanonicalPath();\n\t\treturn KeyPath.parseIfIndex(path.key());\n\t}\n\n\t@Override\n\tpublic int getBitLength(long snap) {\n\t\treturn TraceObjectInterfaceUtils.getValue(object, snap, TraceRegister.KEY_BITLENGTH,\n\t\t\tInteger.class, 0);\n\t}\n\n\t@Override\n\tpublic void setValue(Lifespan lifespan, byte[] value) {\n\t\tint length = getByteLength(lifespan.lmin());\n\t\tif (length != 0 && value.length != length) {\n\t\t\tthrow new IllegalArgumentException(\"Length must match the register\");\n\t\t}\n\t\tobject.setValue(lifespan, TraceRegister.KEY_VALUE, value);\n\t}\n\n\t@Override\n\tpublic byte[] getValue(long snap) {\n\t\tTraceObjectValue ov = object.getValue(snap, TraceRegister.KEY_VALUE);\n\t\tif (ov == null) {\n\t\t\treturn null;\n\t\t}\n\t\tObject val = ov.getValue();\n\t\tif (val instanceof byte[] arr) {\n\t\t\t// TODO: Should I correct mismatched size?\n\t\t\treturn arr;\n\t\t}\n\t\tif (val instanceof String str) {\n\t\t\t// Always base 16. Model API says byte array for register value is big endian.\n\t\t\tBigInteger bigVal = new BigInteger(str, 16);","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/memory/DBTraceObjectRegister.java#L50-L86","documentation":"Thrown by DBTraceObjectRegister.setValue() when the byte array value's length does not match the register's expected byte length. The method computes length = getByteLength(lifespan.lmin()) and checks length != 0 (meaning the register size is known) and value.length != length. An IllegalArgumentException (unchecked) enforces that register writes must use exactly the register's native width.","triggerScenarios":"Calling setValue(lifespan, value) on a DBTraceObjectRegister where value.length differs from the register's byte length at the given snap. For example, writing a 4-byte array to an 8-byte register, or a 2-byte array to a 1-byte register. The check is skipped only when getByteLength returns 0 (register size unknown at that snap).","commonSituations":"Recording register values from a target whose register width differs from the trace's language definition. Endianness or width conversion errors when preparing register values. Using a register value from one architecture in a trace configured for another.","solutions":["Pad or truncate the byte array to match getByteLength(snap) before calling setValue().","Use Utils.bigIntegerToBytes(BigInteger.valueOf(val), reg.getByteLength(snap), true) to generate a correctly-sized byte array.","Verify the register's bit length via getBitLength(snap) and compute expected byte length before writing."],"exampleFix":"// before\nreg.setValue(lifespan, rawBytes); // rawBytes.length != expected\n\n// after\nint expectedLen = reg.getByteLength(lifespan.lmin());\nbyte[] fixed = Arrays.copyOf(rawBytes, expectedLen);\nreg.setValue(lifespan, fixed);","handlingStrategy":"validation","validationCode":"// Verify byte length matches before setting register value\nint expectedLen = reg.getByteLength(lifespan.lmin());\nif (expectedLen != 0 && value.length != expectedLen) {\n    value = Arrays.copyOf(value, expectedLen); // pad or truncate\n}\nreg.setValue(lifespan, value);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always size byte arrays to match getByteLength(snap) before calling setValue().","Use Utils.bigIntegerToBytes() to generate correctly-sized arrays.","Check getBitLength(snap) to compute expected byte length."],"tags":["register","trace-database","memory","validation","unchecked-exception","object-model"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}