NationalSecurityAgency/ghidra · warning · ValueError
Please provide a hexadecimal file offset.
Error message
Please provide a hexadecimal file offset.
What it means
Raised by LocateMemoryAddressesForFileOffset.py getFileOffset (Jython) when askString returns a value that int(userFileOffset, 16) cannot parse - i.e. it is not a valid hexadecimal string. The Python version validates hex-ness explicitly via try/int, unlike the Java sibling which only checks negativity.
Source
Thrown at Ghidra/Features/Base/ghidra_scripts/LocateMemoryAddressesForFileOffset.py:36
#Print the associated memory address to the Ghidra console
#Print the file offset as a Ghidra comment at the memory address in the Ghidra Listing
#If multiple addresses are located, then print the addresses to the console (do not set a Ghidra comment)
# @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()View on GitHub (pinned to d5f144c24d)
Solutions
- Enter a plain hexadecimal value with digits 0-9 and a-f/A-F only, no '0x' prefix.
- Strip an optional '0x'/'0X' prefix before validation.
- Re-prompt the user on ValueError instead of failing the script.
Example fix
# before
try:
int(userFileOffset,16)
except ValueError:
raise ValueError('Please provide a hexadecimal file offset.')
# after - also accept a 0x prefix and re-prompt
userFileOffset = userFileOffset.strip()
if userFileOffset.lower().startswith('0x'):
userFileOffset = userFileOffset[2:]
try:
int(userFileOffset, 16)
except ValueError:
raise ValueError('Please provide a hexadecimal file offset (digits 0-9, a-f).') Defensive patterns
Strategy: validation
Validate before calling
s = userFileOffset.strip()
if s.lower().startswith('0x'):
s = s[2:]
if not s or not all(c in '0123456789abcdefABCDEF' for c in s):
raise ValueError('Please provide a hexadecimal file offset.') Type guard
def is_hex(s):
if s.lower().startswith('0x'):
s = s[2:]
return bool(s) and all(c in '0123456789abcdefABCDEF' for c in s) Try / catch
try:
off = getFileOffset()
except ValueError as e:
if 'hexadecimal' in str(e):
# re-prompt the user
pass
else:
raise Prevention
- Accept and strip a '0x' prefix to be lenient.
- Validate with an explicit character set before parsing.
- Re-prompt on ValueError instead of failing the script.
When it happens
Trigger: User enters a non-hex string (e.g. 'xyz', '1g', '0x1f' with the 0x prefix, or empty) in the prompt. int('0x1f', 16) works but int('xyz',16) raises ValueError; int('',16) raises ValueError.
Common situations: User pastes a decimal offset. User includes a '0x' prefix inconsistently. User leaves the field blank. Typo in the hex string.
Related errors
- Offset cannot be a negative value.
- Offset cannot be a negative value.
- Could not get MD5 on file:
- Bad nodeSize: {}
- Invalid number of elements specified: {}
AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14).
Data as JSON: /api/errors/ed8f2a0608e24490.
Report an issue: GitHub.