{"record":{"id":"ed8f2a0608e24490","repo":"NationalSecurityAgency/ghidra","slug":"please-provide-a-hexadecimal-file-offset","errorCode":null,"errorMessage":"Please provide a hexadecimal file offset.","messagePattern":"Please provide a hexadecimal file offset\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"Ghidra/Features/Base/ghidra_scripts/LocateMemoryAddressesForFileOffset.py","lineNumber":36,"sourceCode":"#Print the associated memory address to the Ghidra console\n#Print the file offset as a Ghidra comment at the memory address in the Ghidra Listing\n#If multiple addresses are located, then print the addresses to the console (do not set a Ghidra comment)\n# @category Examples   \n# @runtime Jython\n\nimport sys\nfrom ghidra.program.model.address import Address\nfrom ghidra.program.model.listing import CodeUnit\nfrom ghidra.program.model.listing import Listing\nfrom ghidra.program.model.mem import Memory\nfrom java.util import Set\n\ndef getFileOffset():\n  userFileOffset = askString('File offset', 'Please provide a hexadecimal file offset')\n  try:\n    int(userFileOffset,16)\n  except ValueError:\n     raise ValueError('Please provide a hexadecimal file offset.')\n  myFileOffset = long(userFileOffset,16) #specify base 16 since we expect address in hex\n  if myFileOffset < 0:\n    raise ValueError('Offset cannot be a negative value.')\n  return myFileOffset\n\ndef processAddress(addr, memBlockName, fileOffset):\n  println('File offset ' + hex(fileOffset) + ' is associated with memory block:address ' + memBlockName + ':' + addr.toString());\n  myCodeUnit = currentProgram.getListing().getCodeUnitContaining(addr)\n  comment = myCodeUnit.getComment(0)\n  if not comment:\n    myCodeUnit.setComment(0, getScriptName() + ': File offset: ' + hex(fileOffset) + \n      ', Memory block:address ' + memBlockName + ':'+ addr.toString())\n  else:\n    myCodeUnit.setComment(0, comment + ' ' + getScriptName() + ': File offset: ' + hex(fileOffset) + \n      ', Memory block:address ' + memBlockName + ':' + addr.toString())\n\nmyFileOffset = getFileOffset()\nmem = currentProgram.getMemory()","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/Base/ghidra_scripts/LocateMemoryAddressesForFileOffset.py#L18-L54","documentation":"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.","triggerScenarios":"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.","commonSituations":"User pastes a decimal offset. User includes a '0x' prefix inconsistently. User leaves the field blank. Typo in the hex string.","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."],"exampleFix":"# before\ntry:\n    int(userFileOffset,16)\nexcept ValueError:\n    raise ValueError('Please provide a hexadecimal file offset.')\n\n# after - also accept a 0x prefix and re-prompt\nuserFileOffset = userFileOffset.strip()\nif userFileOffset.lower().startswith('0x'):\n    userFileOffset = userFileOffset[2:]\ntry:\n    int(userFileOffset, 16)\nexcept ValueError:\n    raise ValueError('Please provide a hexadecimal file offset (digits 0-9, a-f).')","handlingStrategy":"validation","validationCode":"s = userFileOffset.strip()\nif s.lower().startswith('0x'):\n    s = s[2:]\nif not s or not all(c in '0123456789abcdefABCDEF' for c in s):\n    raise ValueError('Please provide a hexadecimal file offset.')","typeGuard":"def is_hex(s):\n    if s.lower().startswith('0x'):\n        s = s[2:]\n    return bool(s) and all(c in '0123456789abcdefABCDEF' for c in s)","tryCatchPattern":"try:\n    off = getFileOffset()\nexcept ValueError as e:\n    if 'hexadecimal' in str(e):\n        # re-prompt the user\n        pass\n    else:\n        raise","preventionTips":["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."],"tags":["script","python","jython","input-validation","parsing"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}