{"record":{"id":"d964672dcf6bc211","repo":"NationalSecurityAgency/ghidra","slug":"cannot-convert-length-to-length","errorCode":null,"errorMessage":"Cannot convert '{length}' to length","messagePattern":"Cannot convert '(.+?)' to length","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Debugger-agent-dbgeng/src/main/py/src/ghidradbg/commands.py","lineNumber":558,"sourceCode":"\n\ndef eval_address(address: Union[str, int]) -> int:\n    try:\n        result = util.parse_and_eval(address)\n        if isinstance(result, int):\n            return result\n        raise ValueError(f\"Value '{address}' does not evaluate to an int\")\n    except Exception:\n        raise RuntimeError(f\"Cannot convert '{address}' to address\")\n\n\ndef eval_range(address: Union[str, int],\n               length: Union[str, int]) -> Tuple[int, int]:\n    start = eval_address(address)\n    try:\n        l = util.parse_and_eval(length)\n    except Exception as e:\n        raise RuntimeError(f\"Cannot convert '{length}' to length\")\n    if not isinstance(l, int):\n        raise ValueError(f\"Value '{address}' does not evaluate to an int\")\n    end = start + l\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    \"\"\"Record the given block of memory into the Ghidra trace.\"\"\"\n\n    STATE.require_tx()\n    return putmem(address, length, pages, True)","sourceCodeStart":540,"sourceCodeEnd":576,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Debugger-agent-dbgeng/src/main/py/src/ghidradbg/commands.py#L540-L576","documentation":"Raised by eval_range() (commands.py:555-558) when util.parse_and_eval(length) throws while resolving the length operand. It is the length-side counterpart of the address error: the start address resolved fine, but the length expression (symbol/number) could not be evaluated to any value.","triggerScenarios":"Calling ghidra_trace_putmem(addr, 'sizeof_bogus') with an undefined symbol; passing a malformed length expression; resolving length against a module not currently loaded.","commonSituations":"Typo in a sizeof/symbol expression; using a C++ expression in MASM mode or vice versa; length expressed as a symbol from a not-yet-loaded header.","solutions":["Verify the length expression evaluates standalone with '? <expr>' in the debugger.","Pass an integer literal (e.g. 0x100) for length to avoid symbol resolution.","Check the evaluator syntax/mode and that referenced symbols are loaded."],"exampleFix":"// before\nghidra_trace_putmem(0x140001000, 'mytype!cb')\n\n// after\nghidra_trace_putmem(0x140001000, 0x100)\n# or verify 'mytype!cb' resolves first","handlingStrategy":"try-catch","validationCode":"def resolve_length_safe(expr):\n    try:\n        v = util.parse_and_eval(expr)\n    except Exception:\n        return None\n    return v\n\nlength_val = resolve_length_safe(length)\nif length_val is None:\n    raise ValueError(f'cannot resolve length {length!r}')","typeGuard":"def length_resolves(expr) -> bool:\n    try:\n        util.parse_and_eval(expr)\n        return True\n    except Exception:\n        return False","tryCatchPattern":"try:\n    ghidra_trace_putmem(address, length)\nexcept RuntimeError as e:\n    if 'Cannot convert' in str(e) and 'to length' in str(e):\n        raise ValueError(f'unresolvable length: {length!r}') from e\n    raise","preventionTips":["Prefer integer literals for length.","Confirm length expressions evaluate standalone in the debugger.","Match the evaluator mode (MASM/C++) to the expression syntax."],"tags":["expression-eval","symbols","memory","ghidra"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}