{"record":{"id":"c768d973f5536702","repo":"NationalSecurityAgency/ghidra","slug":"arrays-must-be-the-same-length","errorCode":null,"errorMessage":"Arrays must be the same length","messagePattern":"Arrays must be the same length","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/util/ByteArrayUtils.java","lineNumber":34,"sourceCode":"package ghidra.trace.util;\n\nimport ghidra.program.model.address.Address;\nimport ghidra.program.model.address.AddressSet;\n\npublic enum ByteArrayUtils {\n\t;\n\n\t/**\n\t * Compute the address set where two byte arrays differ, given a start address\n\t * \n\t * @param start the address of the first byte in each array\n\t * @param a the first array\n\t * @param b the second array\n\t * @return the address set where the arrays differ\n\t */\n\tpublic static AddressSet computeDiffsAddressSet(Address start, byte[] a, byte[] b) {\n\t\tif (a.length != b.length) {\n\t\t\tthrow new IllegalArgumentException(\"Arrays must be the same length\");\n\t\t}\n\t\t// A means of early parameter checking, and I'll need it later\n\t\tAddress end = start.add(a.length - 1);\n\n\t\tAddressSet result = new AddressSet();\n\n\t\tAddress diffStart = null;\n\t\tfor (int i = 0; i < a.length; i++) {\n\t\t\tif (a[i] == b[i]) {\n\t\t\t\tif (diffStart != null) {\n\t\t\t\t\tresult.add(diffStart, start.add(i - 1));\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (diffStart == null) {\n\t\t\t\t\tdiffStart = start.add(i);\n\t\t\t\t}\n\t\t\t}","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/util/ByteArrayUtils.java#L16-L52","documentation":"ByteArrayUtils.computeDiffsAddressSet compares two byte arrays starting from a given address and returns the address ranges where they differ. The arrays must be the same length because the method maps each byte position to a corresponding address. Different lengths would produce an ambiguous or incomplete diff.","triggerScenarios":"Calling computeDiffsAddressSet(start, a, b) where a.length != b.length. This commonly happens when comparing memory snapshots of different sizes, or when one array was truncated or extended.","commonSituations":"Comparing trace memory states where the memory range changed between snapshots. Reading memory blocks that were resized. Comparing a partial buffer against a full expected buffer. Using arrays from different memory spaces or regions with different sizes.","solutions":["Ensure both arrays are the same length before calling: if (a.length != b.length) trim or pad","Slice both arrays to the shorter length: int len = Math.min(a.length, b.length)","Validate array lengths as a precondition and log a clear diagnostic before calling","If comparing regions of different sizes, compare them in sub-ranges of equal length"],"exampleFix":"// before\nAddressSet diffs = ByteArrayUtils.computeDiffsAddressSet(start, a, b); // a.length=16, b.length=32\n// after\nint len = Math.min(a.length, b.length);\nbyte[] aTrimmed = Arrays.copyOf(a, len);\nbyte[] bTrimmed = Arrays.copyOf(b, len);\nAddressSet diffs = ByteArrayUtils.computeDiffsAddressSet(start, aTrimmed, bTrimmed);","handlingStrategy":"validation","validationCode":"boolean sameLength(byte[] a, byte[] b) {\n    return a != null && b != null && a.length == b.length;\n}","typeGuard":"// No type-level guard in Java; use runtime length check","tryCatchPattern":"// IllegalArgumentException; validate before calling instead","preventionTips":["Always compare array lengths before passing to diff methods","Pad or truncate to the shorter array when sizes differ","Document the equal-length contract at call sites"],"tags":["ghidra","trace-utils","byte-array","precondition"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}