{"record":{"id":"c2102b2614efa885","repo":"NationalSecurityAgency/ghidra","slug":"offset-cannot-be-a-negative-value","errorCode":null,"errorMessage":"Offset cannot be a negative value.","messagePattern":"Offset cannot be a negative value\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"warning","filePath":"Ghidra/Features/Base/ghidra_scripts/LocateMemoryAddressesForFileOffset.java","lineNumber":66,"sourceCode":"\t\t}\n\t\t//address set size is > 1, file offset matches to multiple addresses.  \n\t\t//Let the user decide which address they want.\n\t\telse {\n\t\t\tprintln(\"Possible memory block:address are:\");\n\t\t\tfor (Address addr : addressList) {\n\t\t\t\tprintln(mem.getBlock(addr).getName() + \":\" + addr.toString());\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic long getFileOffset()\n\t\t\tthrows CancelledException, NumberFormatException, IllegalArgumentException {\n\t\tString userFileOffset =\n\t\t\taskString(\"File offset\", \"Please provide a hexadecimal file offset\");\n\t\tlong myFileOffset = 0;\n\t\tmyFileOffset = Long.parseLong(userFileOffset, 16);\n\t\tif (myFileOffset < 0) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\"Offset cannot be a negative value.\" + userFileOffset);\n\t\t}\n\t\treturn myFileOffset;\n\t}\n\n\tpublic void processAddress(Address addr, String memBlockName, long fileOffset) {\n\t\tprintln(\"File offset \" + Long.toHexString(fileOffset) +\n\t\t\t\" is associated with memory block:address \" + memBlockName + \":\" + addr.toString());\n\t\tCodeUnit myCodeUnit = currentProgram.getListing().getCodeUnitContaining(addr);\n\t\tString comment = myCodeUnit.getComment(CommentType.EOL);\n\t\tif (comment == null) {\n\t\t\tmyCodeUnit.setComment(CommentType.EOL,\n\t\t\t\tthis.getScriptName() + \": File offset: \" + Long.toHexString(fileOffset) +\n\t\t\t\t\t\", Memory block:address \" + memBlockName + \":\" + addr.toString());\n\t\t}\n\t\telse {\n\t\t\tmyCodeUnit.setComment(CommentType.EOL,\n\t\t\t\tcomment + \", \" + this.getScriptName() + \": File offset: \" +","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/Base/ghidra_scripts/LocateMemoryAddressesForFileOffset.java#L48-L84","documentation":"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.","triggerScenarios":"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.","commonSituations":"User types a negative offset in the prompt. Paste/copy error prepending a minus. Confusion between signed and unsigned 64-bit offsets.","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."],"exampleFix":"// before\nmyFileOffset = Long.parseLong(userFileOffset, 16);\nif (myFileOffset < 0) {\n    throw new IllegalArgumentException(\"Offset cannot be a negative value.\" + userFileOffset);\n}\n\n// after - reject leading '-' early and parse unsigned\nif (userFileOffset.startsWith(\"-\")) {\n    throw new IllegalArgumentException(\"Offset cannot be negative: \" + userFileOffset);\n}\nlong myFileOffset = Long.parseUnsignedLong(userFileOffset, 16);","handlingStrategy":"validation","validationCode":"String s = userFileOffset.trim();\nif (s.isEmpty() || s.startsWith(\"-\")) {\n    throw new IllegalArgumentException(\"Offset must be a non-negative hex string.\");\n}\nlong v = Long.parseUnsignedLong(s, 16);","typeGuard":"static boolean isNonNegativeHex(String s) {\n    return s != null && !s.isEmpty() && !s.startsWith(\"-\") && s.matches(\"[0-9a-fA-F]+\");\n}","tryCatchPattern":"try {\n    long off = getFileOffset();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Offset cannot be a negative value\")) {\n        // re-prompt the user\n    } else throw e;\n}","preventionTips":["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."],"tags":["script","input-validation","address","parsing"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}