NationalSecurityAgency/ghidra · warning · ValueError
Offset cannot be a negative value.
Error message
Offset cannot be a negative value.
What it means
Raised by LocateMemoryAddressesForFileOffset.py getFileOffset (Jython) when the hex string parses but the resulting long is negative. Python's long(s, 16) accepts a leading '-', so '-1f' becomes -31 and fails the `myFileOffset < 0` check. This mirrors the Java sibling's negative-offset guard.
Source
Thrown at Ghidra/Features/Base/ghidra_scripts/LocateMemoryAddressesForFileOffset.py:39
# @category Examples
# @runtime Jython
import sys
from ghidra.program.model.address import Address
from ghidra.program.model.listing import CodeUnit
from ghidra.program.model.listing import Listing
from ghidra.program.model.mem import Memory
from java.util import Set
def getFileOffset():
userFileOffset = askString('File offset', 'Please provide a hexadecimal file offset')
try:
int(userFileOffset,16)
except ValueError:
raise ValueError('Please provide a hexadecimal file offset.')
myFileOffset = long(userFileOffset,16) #specify base 16 since we expect address in hex
if myFileOffset < 0:
raise ValueError('Offset cannot be a negative value.')
return myFileOffset
def processAddress(addr, memBlockName, fileOffset):
println('File offset ' + hex(fileOffset) + ' is associated with memory block:address ' + memBlockName + ':' + addr.toString());
myCodeUnit = currentProgram.getListing().getCodeUnitContaining(addr)
comment = myCodeUnit.getComment(0)
if not comment:
myCodeUnit.setComment(0, getScriptName() + ': File offset: ' + hex(fileOffset) +
', Memory block:address ' + memBlockName + ':'+ addr.toString())
else:
myCodeUnit.setComment(0, comment + ' ' + getScriptName() + ': File offset: ' + hex(fileOffset) +
', Memory block:address ' + memBlockName + ':' + addr.toString())
myFileOffset = getFileOffset()
mem = currentProgram.getMemory()
addressList = mem.locateAddressesForFileOffset(myFileOffset)
if addressList.isEmpty():
println('No memory address found for: ' + hex(myFileOffset))View on GitHub (pinned to d5f144c24d)
Solutions
- Enter the offset as a positive hex string with no leading '-'.
- Pre-strip a leading '-' from the input and re-prompt.
- For high 64-bit offsets, treat the value as unsigned.
Example fix
# before
myFileOffset = long(userFileOffset,16)
if myFileOffset < 0:
raise ValueError('Offset cannot be a negative value.')
# after
if userFileOffset.startswith('-'):
raise ValueError('Offset cannot be negative: ' + userFileOffset)
myFileOffset = long(userFileOffset, 16) Defensive patterns
Strategy: validation
Validate before calling
s = userFileOffset.strip()
if s.startswith('-'):
raise ValueError('Offset cannot be negative: ' + s)
myFileOffset = long(s, 16) Type guard
def is_non_negative_hex(s):
return bool(s) and not s.startswith('-') and all(c in '0123456789abcdefABCDEF' for c in s) Try / catch
try:
off = getFileOffset()
except ValueError as e:
if 'negative' in str(e):
# strip '-' and retry, or re-prompt
pass
else:
raise Prevention
- Strip or reject a leading '-' before parsing.
- Validate the string with a hex character set.
- Treat offsets as unsigned 64-bit values.
When it happens
Trigger: User enters a hex string beginning with '-' (e.g. '-10'). long('-10', 16) returns -16, which is < 0, raising ValueError.
Common situations: Negative offset typed in the prompt. Copy/paste with stray minus. Confusion about signedness of 64-bit offsets.
Related errors
- Please provide a hexadecimal file offset.
- Offset cannot be a negative value.
- Could not get MD5 on file:
- Cannot write to watch variable without an address
- Address does not belong to a memory in the trace
AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14).
Data as JSON: /api/errors/7e6ec2582a4d4cb7.
Report an issue: GitHub.