NationalSecurityAgency/ghidra · warning · IllegalArgumentException
Offset cannot be a negative value.
Error message
Offset cannot be a negative value.
What it means
Thrown by LocateMemoryAddressesForFileOffset.getFileOffset (Java) when the user-supplied string, parsed as base-16, yields a negative long. Java's Long.parseLong(..., 16) accepts a leading '-' so an input like '-10' parses to -16; the script rejects that. Note: a non-hex string throws NumberFormatfirst and is not caught here.
Source
Thrown at Ghidra/Features/Base/ghidra_scripts/LocateMemoryAddressesForFileOffset.java:66
}
//address set size is > 1, file offset matches to multiple addresses.
//Let the user decide which address they want.
else {
println("Possible memory block:address are:");
for (Address addr : addressList) {
println(mem.getBlock(addr).getName() + ":" + addr.toString());
}
}
}
public long getFileOffset()
throws CancelledException, NumberFormatException, IllegalArgumentException {
String userFileOffset =
askString("File offset", "Please provide a hexadecimal file offset");
long myFileOffset = 0;
myFileOffset = Long.parseLong(userFileOffset, 16);
if (myFileOffset < 0) {
throw new IllegalArgumentException(
"Offset cannot be a negative value." + userFileOffset);
}
return myFileOffset;
}
public void processAddress(Address addr, String memBlockName, long fileOffset) {
println("File offset " + Long.toHexString(fileOffset) +
" is associated with memory block:address " + memBlockName + ":" + addr.toString());
CodeUnit myCodeUnit = currentProgram.getListing().getCodeUnitContaining(addr);
String comment = myCodeUnit.getComment(CommentType.EOL);
if (comment == null) {
myCodeUnit.setComment(CommentType.EOL,
this.getScriptName() + ": File offset: " + Long.toHexString(fileOffset) +
", Memory block:address " + memBlockName + ":" + addr.toString());
}
else {
myCodeUnit.setComment(CommentType.EOL,
comment + ", " + this.getScriptName() + ": File offset: " +View on GitHub (pinned to d5f144c24d)
Solutions
- Enter the offset as a positive hexadecimal value without a leading '-'.
- If you need to express a high 64-bit value whose MSB is set, use Long.parseUnsignedLong or accept it as unsigned.
- Pre-validate the input string: reject or strip leading '-' before parsing.
Example fix
// before
myFileOffset = Long.parseLong(userFileOffset, 16);
if (myFileOffset < 0) {
throw new IllegalArgumentException("Offset cannot be a negative value." + userFileOffset);
}
// after - reject leading '-' early and parse unsigned
if (userFileOffset.startsWith("-")) {
throw new IllegalArgumentException("Offset cannot be negative: " + userFileOffset);
}
long myFileOffset = Long.parseUnsignedLong(userFileOffset, 16); Defensive patterns
Strategy: validation
Validate before calling
String s = userFileOffset.trim();
if (s.isEmpty() || s.startsWith("-")) {
throw new IllegalArgumentException("Offset must be a non-negative hex string.");
}
long v = Long.parseUnsignedLong(s, 16); Type guard
static boolean isNonNegativeHex(String s) {
return s != null && !s.isEmpty() && !s.startsWith("-") && s.matches("[0-9a-fA-F]+");
} Try / catch
try {
long off = getFileOffset();
} catch (IllegalArgumentException e) {
if (e.getMessage().startsWith("Offset cannot be a negative value")) {
// re-prompt the user
} else throw e;
} Prevention
- Reject or strip a leading '-' before parsing.
- Use Long.parseUnsignedLong for high 64-bit offsets.
- Validate the string with a regex ([0-9a-fA-F]+) before parsing.
When it happens
Trigger: Calling getFileOffset after the user enters a hex string with a leading minus sign (e.g. '-1f'). Long.parseLong('-1f', 16) returns -31, which is < 0, triggering the IllegalArgumentException.
Common situations: User types a negative offset in the prompt. Paste/copy error prepending a minus. Confusion between signed and unsigned 64-bit offsets.
Related errors
- Please provide a hexadecimal file offset.
- Offset cannot be a negative value.
- Address string should have at most one colon (:)
- Bad nodeSize: {}
- Invalid number of elements specified: {}
AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14).
Data as JSON: /api/errors/c2102b2614efa885.
Report an issue: GitHub.