{"record":{"id":"123640f38584c624","repo":"NationalSecurityAgency/ghidra","slug":"byte-arrays-cannot-exceed-length-of-variable","errorCode":null,"errorMessage":"Byte arrays cannot exceed length of variable","messagePattern":"Byte arrays cannot exceed length of variable","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/variable/AbstractDebuggerVariableViewerVarValue.java","lineNumber":121,"sourceCode":"\t\t\t\t\tnew ByteMemBufferImpl(address, value, language.isBigEndian()), NO_SETTINGS,\n\t\t\t\t\tvalue.length);\n\t\t\tsetRawValueBytes(encoded);\n\t\t}\n\t\tcatch (final DataTypeEncodeException e) {\n\t\t\tMsg.error(this, e.getMessage());\n\t\t}\n\t}\n\n\tabstract DataType getDataType();\n\n\tabstract void setDataType(DataType dataType);\n\n\tprivate void setRawValueBytes(byte[] bytes) {\n\t\tif (address == null) {\n\t\t\tthrow new IllegalStateException(\"Cannot write to variable without an address\");\n\t\t}\n\t\tif (bytes.length > value.length) {\n\t\t\tthrow new IllegalArgumentException(\"Byte arrays cannot exceed length of variable\");\n\t\t}\n\t\tif (bytes.length < value.length) {\n\t\t\tfinal byte[] fillOld = Arrays.copyOf(value, value.length);\n\t\t\tSystem.arraycopy(bytes, 0, fillOld, 0, bytes.length);\n\t\t\tbytes = fillOld;\n\t\t}\n\t\tfinal DebuggerControlService controlService = provider.controlService;\n\t\tif (controlService == null) {\n\t\t\tthrow new AssertionError(\"No control service\");\n\t\t}\n\t\tfinal DebuggerControlService.StateEditor editor =\n\t\t\t\tcontrolService.createStateEditor(provider.currentCoordinates);\n\t\teditor.setVariable(address, bytes).exceptionally(ex -> {\n\t\t\tMsg.showError(this, null, \"Write Failed\", \"Could not modify value (on target)\", ex);\n\t\t\treturn null;\n\t\t});\n\n\t}","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/variable/AbstractDebuggerVariableViewerVarValue.java#L103-L139","documentation":"Thrown by AbstractDebuggerVariableViewerVarValue.setRawValueBytes when the supplied byte array is longer than the variable's own value buffer. The variable has a fixed length; the caller must truncate or pad to fit, never exceed.","triggerScenarios":"Calling setRawValueBytes(bytes) where bytes.length > value.length (value is the variable's fixed-size backing buffer derived from its DataType length).","commonSituations":"Paste of a hex string that decodes to more bytes than the variable's type; programming error converting a wider integer into a narrower variable; mismatch between the declared variable size and the bytes supplied by the UI.","solutions":["Truncate or resize the byte array to the variable's length (value.length) before calling setRawValueBytes.","Validate input length against the variable's DataType length in the UI before submission.","Change the variable's declared type to a wider one if more bytes are genuinely required.","Use NumericUtilities to convert a number to a fixed-width byte array of the correct length."],"exampleFix":"// before\nbyte[] bytes = ...; // possibly too long\nsetRawValueBytes(bytes); // throws if bytes.length > value.length\n\n// after\nif (bytes.length > value.length) {\n    bytes = Arrays.copyOf(bytes, value.length);\n}\nsetRawValueBytes(bytes);","handlingStrategy":"validation","validationCode":"boolean ok = bytes.length <= value.length;","typeGuard":"static boolean fitsVariable(byte[] bytes, byte[] value) {\n    return bytes.length <= value.length;\n}","tryCatchPattern":"try {\n    varValue.setRawValueBytes(bytes);\n} catch (IllegalArgumentException e) {\n    bytes = Arrays.copyOf(bytes, value.length);\n}","preventionTips":["Always size byte arrays to the variable's DataType length.","Use fixed-width converters (Utils.bigIntegerToBytes)."],"tags":["variable","bytes","length","memory","validation"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}