NationalSecurityAgency/ghidra · error · IllegalArgumentException

Arrays must be the same length

Error message

Arrays must be the same length

What it means

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.

Source

Thrown at Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/util/ByteArrayUtils.java:34

package ghidra.trace.util;

import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSet;

public enum ByteArrayUtils {
	;

	/**
	 * Compute the address set where two byte arrays differ, given a start address
	 * 
	 * @param start the address of the first byte in each array
	 * @param a the first array
	 * @param b the second array
	 * @return the address set where the arrays differ
	 */
	public static AddressSet computeDiffsAddressSet(Address start, byte[] a, byte[] b) {
		if (a.length != b.length) {
			throw new IllegalArgumentException("Arrays must be the same length");
		}
		// A means of early parameter checking, and I'll need it later
		Address end = start.add(a.length - 1);

		AddressSet result = new AddressSet();

		Address diffStart = null;
		for (int i = 0; i < a.length; i++) {
			if (a[i] == b[i]) {
				if (diffStart != null) {
					result.add(diffStart, start.add(i - 1));
				}
			}
			else {
				if (diffStart == null) {
					diffStart = start.add(i);
				}
			}

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Ensure both arrays are the same length before calling: if (a.length != b.length) trim or pad
  2. Slice both arrays to the shorter length: int len = Math.min(a.length, b.length)
  3. Validate array lengths as a precondition and log a clear diagnostic before calling
  4. If comparing regions of different sizes, compare them in sub-ranges of equal length

Example fix

// before
AddressSet diffs = ByteArrayUtils.computeDiffsAddressSet(start, a, b); // a.length=16, b.length=32
// after
int len = Math.min(a.length, b.length);
byte[] aTrimmed = Arrays.copyOf(a, len);
byte[] bTrimmed = Arrays.copyOf(b, len);
AddressSet diffs = ByteArrayUtils.computeDiffsAddressSet(start, aTrimmed, bTrimmed);
Defensive patterns

Strategy: validation

Validate before calling

boolean sameLength(byte[] a, byte[] b) {
    return a != null && b != null && a.length == b.length;
}

Type guard

// No type-level guard in Java; use runtime length check

Try / catch

// IllegalArgumentException; validate before calling instead

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14). Data as JSON: /api/errors/c768d973f5536702. Report an issue: GitHub.