{"record":{"id":"4250f6beee879cc9","repo":"NationalSecurityAgency/ghidra","slug":"length-0","errorCode":null,"errorMessage":"length < 0","messagePattern":"length < 0","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Debugger-api/src/main/java/ghidra/debug/flatapi/FlatDebuggerAPI.java","lineNumber":793,"sourceCode":"\t\tTraceSchedule time = current.getTime();\n\t\tTraceSchedule patched = time.patched(requireThread(thread), platform.getLanguage(), sleigh);\n\t\treturn emulate(platform, patched, monitor);\n\t}\n\n\t/**\n\t * Create an address range, avoiding address overflow by truncating\n\t * \n\t * <p>\n\t * If the length would cause address overflow, it is adjusted such that the range's maximum\n\t * address is the space's maximum address.\n\t * \n\t * @param start the minimum address\n\t * @param length the desired length\n\t * @return the range\n\t */\n\tdefault AddressRange safeRange(Address start, int length) {\n\t\tif (length < 0) {\n\t\t\tthrow new IllegalArgumentException(\"length < 0\");\n\t\t}\n\t\tlong maxLength = start.getAddressSpace().getMaxAddress().subtract(start);\n\t\ttry {\n\t\t\treturn new AddressRangeImpl(start, MathUtilities.unsignedMin(length, maxLength));\n\t\t}\n\t\tcatch (AddressOverflowException e) {\n\t\t\tthrow new AssertionError(e);\n\t\t}\n\t}\n\n\t/**\n\t * The target service\n\t * \n\t * @return the service\n\t */\n\tdefault DebuggerTargetService getTargetService() {\n\t\treturn requireService(DebuggerTargetService.class);\n\t}","sourceCodeStart":775,"sourceCodeEnd":811,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Debugger-api/src/main/java/ghidra/debug/flatapi/FlatDebuggerAPI.java#L775-L811","documentation":"Thrown by FlatDebuggerAPI.safeRange(start, length) when the supplied length is negative. safeRange creates an address range, truncating to avoid overflow, but a negative length is a programming error, not an overflow.","triggerScenarios":"Calling safeRange(addr, len) where len < 0.","commonSituations":"Computing length as a difference (end - start) that went negative due to swapped bounds; off-by-one or underflow in size calculation; passing an unvalidated user input as length.","solutions":["Validate length >= 0 before calling safeRange().","Fix the length computation (e.g., use Math.abs or correct the bound order).","Guard the caller's arithmetic so the difference cannot go negative."],"exampleFix":"// before\nAddressRange r = safeRange(start, end.subtract(start)); // throws if end<start\n\n// after\nlong len = end.subtract(start);\nif (len < 0) {\n    println(\"end before start\");\n    return;\n}\nAddressRange r = safeRange(start, (int) len);","handlingStrategy":"validation","validationCode":"if (length < 0) {\n    println(\"length must be >= 0\");\n    return;\n}","typeGuard":null,"tryCatchPattern":"try {\n    AddressRange r = safeRange(start, length);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().equals(\"length < 0\")) {\n        // fix the length computation and retry\n    } else throw e;\n}","preventionTips":["Compute lengths from ordered bounds (start <= end).","Guard any computed length with a >= 0 check before passing to safeRange."],"tags":["flat-debugger-api","address-range","argument-validation","debugger"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}