{"record":{"id":"7e6ec2582a4d4cb7","repo":"NationalSecurityAgency/ghidra","slug":"offset-cannot-be-a-negative-value-7e6ec2","errorCode":null,"errorMessage":"Offset cannot be a negative value.","messagePattern":"Offset cannot be a negative value\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"Ghidra/Features/Base/ghidra_scripts/LocateMemoryAddressesForFileOffset.py","lineNumber":39,"sourceCode":"# @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()\naddressList = mem.locateAddressesForFileOffset(myFileOffset)\nif addressList.isEmpty():\n  println('No memory address found for: ' + hex(myFileOffset))","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/Base/ghidra_scripts/LocateMemoryAddressesForFileOffset.py#L21-L57","documentation":"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.","triggerScenarios":"User enters a hex string beginning with '-' (e.g. '-10'). long('-10', 16) returns -16, which is < 0, raising ValueError.","commonSituations":"Negative offset typed in the prompt. Copy/paste with stray minus. Confusion about signedness of 64-bit offsets.","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."],"exampleFix":"# before\nmyFileOffset = long(userFileOffset,16)\nif myFileOffset < 0:\n    raise ValueError('Offset cannot be a negative value.')\n\n# after\nif userFileOffset.startswith('-'):\n    raise ValueError('Offset cannot be negative: ' + userFileOffset)\nmyFileOffset = long(userFileOffset, 16)","handlingStrategy":"validation","validationCode":"s = userFileOffset.strip()\nif s.startswith('-'):\n    raise ValueError('Offset cannot be negative: ' + s)\nmyFileOffset = long(s, 16)","typeGuard":"def is_non_negative_hex(s):\n    return bool(s) and not s.startswith('-') and all(c in '0123456789abcdefABCDEF' for c in s)","tryCatchPattern":"try:\n    off = getFileOffset()\nexcept ValueError as e:\n    if 'negative' in str(e):\n        # strip '-' and retry, or re-prompt\n        pass\n    else:\n        raise","preventionTips":["Strip or reject a leading '-' before parsing.","Validate the string with a hex character set.","Treat offsets as unsigned 64-bit values."],"tags":["script","python","jython","input-validation","address"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}