NationalSecurityAgency/ghidra · error · RuntimeError

Cannot convert '{length}' to length: {e}

Error message

Cannot convert '{length}' to length: {e}

What it means

Thrown by eval_range() when util.parse_and_eval(length) raises while converting the LENGTH argument into an integer byte count. The original exception is wrapped so the user sees both the offending length token and the underlying LLDB error. This guards the address+length range computation used by putmem, putmem-state, delmem, and get-values-rng.

Source

Thrown at Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/commands.py:694


def eval_address(address: str) -> Optional[int]:
    try:
        return util.parse_and_eval(address)
    except BaseException as e:
        if "can't evaluate expressions when the process is running" not in str(e):
            raise RuntimeError(f"Cannot convert '{address}' to address: {e}")
        return None


def eval_range(address: str, length: str) -> Tuple[Optional[int], Optional[int]]:
    start = eval_address(address)
    if start is None:
        return None, None
    try:
        end = start + util.parse_and_eval(length)
    except BaseException as e:
        raise RuntimeError(f"Cannot convert '{length}' to length: {e}")
    return start, end


def putmem(address: str, length: str, result: lldb.SBCommandReturnObject,
           pages: bool = True) -> None:
    start, end = eval_range(address, length)
    if start is not None and end is not None:
        put_bytes(start, end, result, pages)


@convert_errors
def ghidra_trace_putmem(debugger: lldb.SBDebugger, command: str,
                        result: lldb.SBCommandReturnObject,
                        internal_dict: Dict[str, Any]) -> None:
    """Record the given block of memory into the Ghidra trace.

    Usage: ghidra trace putmem ADDRESS LENGTH [PAGES]

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Stop/halt the target first so LLDB can evaluate the length expression (process must not be running).
  2. Re-type LENGTH as an explicit decimal or hex literal that does not depend on symbol resolution (e.g. 0x40).
  3. If a symbol/size is intended, verify it exists with 'image lookup' or 'expr sizeof(T)' before issuing the command.
  4. Quote the whole command so shlex does not split the length on unexpected whitespace.

Example fix

// before
ghidra trace putmem 0x10000 buffersize
// after
ghidra trace putmem 0x10000 0x100
Defensive patterns

Strategy: validation

Validate before calling

# Before calling eval_range-based commands, sanity-check the length token.
import shlex
from ghidralldb import util

def safe_length(length_token: str) -> bool:
    try:
        util.parse_and_eval(length_token)
        return True
    except BaseException:
        return False

Type guard

def is_evaluatable_length(tok: str) -> bool:
    # Heuristic: hex/decimal literals always parse; symbols need a stopped target.
    import re
    return bool(re.fullmatch(r'(0x[0-9a-fA-F]+|\d+)', tok)) or tok.isidentifier()

Try / catch

try:
    start, end = eval_range(address, length)
except RuntimeError as e:
    if 'to length' in str(e):
        # fall back: treat length as a hex literal if possible
        import re
        m = re.fullmatch(r'\s*(0x[0-9a-fA-F]+|\d+)\s*', length)
        if not m:
            raise
        start, end = eval_range(address, m.group(1))
    else:
        raise

Prevention

When it happens

Trigger: Calling ghidra trace putmem ADDRESS LENGTH with a LENGTH token that is not a valid LLDB expression (e.g. an empty string, a typo, an undefined symbol, or a register alias LLDB cannot resolve). Also triggered if the inferior is currently running and LLDB refuses to evaluate expressions (SBDebugger.EvaluateExpression fails).

Common situations: User typos a length like '0x100z' or passes a label name instead of a numeric size; running the command before stopping the target so LLDB reports 'cannot evaluate expressions when the process is running'; copy-pasting a length from another architecture whose units differ.

Related errors


AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14). Data as JSON: /api/errors/b7a118f7b017fa53. Report an issue: GitHub.