{"record":{"id":"4f9b23a6e5011d85","repo":"NationalSecurityAgency/ghidra","slug":"cannot-convert-to-length","errorCode":null,"errorMessage":"Cannot convert '{}' to length","messagePattern":"Cannot convert '(.+?)' to length","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Debugger-agent-drgn/src/main/py/src/ghidradrgn/commands.py","lineNumber":447,"sourceCode":"        base, addr = trace.extra.require_mm().map(nproc, start)\n        if base != addr.space:\n            trace.create_overlay_space(base, addr.space)\n        count = trace.put_bytes(addr, buf)\n        if display_result:\n            print(\"Wrote {} bytes\".format(count))\n        if isinstance(count, Future):\n            return {'count': -1}\n        else:\n            return {'count': count}\n    return {'count': 0}\n\n\ndef eval_range(address: Union[str, int], length: Union[str, int]) -> Tuple[int, int]:\n    start = int(address)\n    try:\n        end = start + int(length)\n    except Exception as e:\n        raise RuntimeError(\"Cannot convert '{}' to length\".format(length))\n    return start, end\n\n\ndef putmem(address: Union[str, int], length: Union[str, int],\n           pages: bool = True, display_result: bool = True) -> Dict[str, int]:\n    start, end = eval_range(address, length)\n    return put_bytes(start, end, pages, display_result)\n\n\ndef ghidra_trace_putmem(address: Union[str, int], length: Union[str, int],\n                        pages: bool = True) -> Dict[str, int]:\n    \"\"\"\n    Record the given block of memory into the Ghidra trace.\n    \"\"\"\n\n    STATE.require_tx()\n    return putmem(address, length, pages, True)\n","sourceCodeStart":429,"sourceCodeEnd":465,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Debugger-agent-drgn/src/main/py/src/ghidradrgn/commands.py#L429-L465","documentation":"Thrown by eval_range in the drgn agent when int(length) fails to convert the length argument to an integer. This function is called by putmem and related memory-reading commands to compute the address range [start, start+length). Any non-integer length value triggers this RuntimeError.","triggerScenarios":"Calling ghidra_trace_putmem(address, '0x10') with a hex string length, ghidra_trace_putmem(address, 'abc'), or passing a float string like '1.5' as the length. The int() call only accepts base-10 integer strings (or int type directly).","commonSituations":"Passing hex-formatted lengths (common in debugging contexts like '0x100'); passing a GDB/drgn Value object that is not directly int-convertible; accidental string concatenation producing '0x100 ' with trailing space.","solutions":["Pass length as a base-10 integer or integer-valued string, e.g. 256 or '256'.","If you have a hex value, convert it first: int(hexstr, 16) before passing.","Ensure no whitespace or non-digit characters are in the length string."],"exampleFix":"// before\nghidra_trace_putmem('0x7fff0000', '0x100')\n// after\nghidra_trace_putmem('0x7fff0000', str(int('0x100', 16)))","handlingStrategy":"validation","validationCode":"def parse_length(length) -> int:\n    if isinstance(length, int):\n        return length\n    if isinstance(length, str):\n        length = length.strip()\n        if length.startswith('0x') or length.startswith('0X'):\n            return int(length, 16)\n        return int(length)\n    raise ValueError(f\"Cannot parse length: {length!r}\")\n\n# Use parsed value:\nlength_int = parse_length(length)","typeGuard":"def is_valid_length(length) -> bool:\n    if isinstance(length, int):\n        return length >= 0\n    if isinstance(length, str):\n        try:\n            int(length)\n            return True\n        except ValueError:\n            return False\n    return False","tryCatchPattern":"try:\n    ghidra_trace_putmem(address, length)\nexcept RuntimeError as e:\n    if 'Cannot convert' in str(e):\n        print(f\"Length '{length}' is not a valid integer. Pass a base-10 number.\")\n    raise","preventionTips":["Pass lengths as Python int, not hex strings.","If you have a hex value, convert with int(hexstr, 16) before calling.","Strip whitespace from length strings read from files or user input."],"tags":["drgn","input-validation","memory","type-conversion"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}