{"record":{"id":"478f401da139b9b8","repo":"NationalSecurityAgency/ghidra","slug":"byte-arrays-cannot-exceed-length-of-variable-478f40","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/watch/DefaultWatchRow.java","lineNumber":467,"sourceCode":"\t\tfinal BigInteger val;\n\t\tif (intString.startsWith(\"0x\")) {\n\t\t\tval = new BigInteger(intString.substring(2), 16);\n\t\t}\n\t\telse {\n\t\t\tval = new BigInteger(intString, 10);\n\t\t}\n\t\tsetRawValueBytes(\n\t\t\tUtils.bigIntegerToBytes(val, value.length, provider.language.isBigEndian()));\n\t}\n\n\tpublic void setRawValueBytes(byte[] bytes) {\n\t\tsynchronized (lock) {\n\t\t\tif (address == null) {\n\t\t\t\tthrow new IllegalStateException(\n\t\t\t\t\t\"Cannot write to watch variable without an address\");\n\t\t\t}\n\t\t\tif (bytes.length > value.length) {\n\t\t\t\tthrow new IllegalArgumentException(\"Byte arrays cannot exceed length of variable\");\n\t\t\t}\n\t\t\tif (bytes.length < value.length) {\n\t\t\t\tbyte[] fillOld = Arrays.copyOf(value, value.length);\n\t\t\t\tSystem.arraycopy(bytes, 0, fillOld, 0, bytes.length);\n\t\t\t\tbytes = fillOld;\n\t\t\t}\n\t\t\tDebuggerControlService controlService = provider.controlService;\n\t\t\tif (controlService == null) {\n\t\t\t\tthrow new AssertionError(\"No control service\");\n\t\t\t}\n\t\t\tStateEditor editor = controlService.createStateEditor(provider.current);\n\t\t\teditor.setVariable(address, bytes).exceptionally(ex -> {\n\t\t\t\tMsg.showError(this, null, \"Write Failed\",\n\t\t\t\t\t\"Could not modify watch value (on target)\", ex);\n\t\t\t\treturn null;\n\t\t\t});\n\t\t}\n\t}","sourceCodeStart":449,"sourceCodeEnd":485,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/watch/DefaultWatchRow.java#L449-L485","documentation":"Thrown by DefaultWatchRow.setRawValueBytes when the supplied byte array is longer than the watch's fixed value buffer. Mirrors the variable-viewer length check: writes must fit within the watch's declared length.","triggerScenarios":"Calling setRawValueBytes(bytes) where bytes.length > value.length inside the synchronized write block.","commonSituations":"Pasting a hex/byte literal wider than the watch's register/variable size; converting a larger integer into a narrow watch; type-length mismatch between the watch and supplied bytes.","solutions":["Trim the byte array to value.length before writing.","Recreate the watch with the correct (wider) length/type if more bytes are needed.","Validate byte length against getValueLength() in the UI.","Use Utils.bigIntegerToBytes(val, value.length, bigEndian) which sizes to the variable."],"exampleFix":"// before\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 <= row.getValueLength();","typeGuard":"static boolean fitsWatch(byte[] bytes, int valueLen) {\n    return bytes.length <= valueLen;\n}","tryCatchPattern":"try {\n    row.setRawValueBytes(bytes);\n} catch (IllegalArgumentException e) {\n    bytes = Arrays.copyOf(bytes, row.getValueLength());\n}","preventionTips":["Size bytes to getValueLength() before writing.","Use bigIntegerToBytes with the watch length."],"tags":["watch","bytes","length","validation"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}