Skip to content

Don't expose a memory reference for non-pointer evaluation results#1601

Open
tieo wants to merge 1 commit into
microsoft:mainfrom
tieo:fix-scalar-memory-reference
Open

Don't expose a memory reference for non-pointer evaluation results#1601
tieo wants to merge 1 commit into
microsoft:mainfrom
tieo:fix-scalar-memory-reference

Conversation

@tieo

@tieo tieo commented Jun 29, 2026

Copy link
Copy Markdown

Problem

In a debug data-tip (or the Variables view), hovering a non-pointer scalar shows the value correctly but attaches a memoryReference, so VS Code renders the "view binary data" hex icon and clicking it navigates to an address equal to the value. For example, hovering a uint32_t stsdig whose value is 1 offers a memory view that opens at address 0x1.

Cause

AD7Property.GetMemoryContext (src/MIDebugEngine/AD7.Impl/AD7Property.cs) tries to interpret the result's display value as a memory address, for every type:

// try to interpret the result as an address
string v = _variableInformation.Value;
...
UInt64.TryParse(v, NumberStyles.Any, ...)   // "1" -> addr = 1
ppMemory = new AD7MemoryAddress(_engine, addr, null);

For a pointer this is correct — the value is an address (0x...). For a plain scalar (e.g. int/uint32_t) the value is a number, not an address, so UInt64.TryParse("1") succeeds and the memory reference becomes 0x1. OpenDebugAD7 then reports that as memoryReference on the evaluate/variables response, and VS Code's memory navigation goes to address 1.

Fix

Only treat the value as an address when the result is a pointer (or an array, which already re-evaluates to its address with &(...) further down). For any other type, return S_GETMEMORYCONTEXT_NO_MEMORY_CONTEXT so no memoryReference is reported and no spurious memory icon/navigation appears.

bool isArrayValue = v[0] == '[' && v[v.Length - 1] == ']';
string typeName = _variableInformation.TypeName;
bool isPointer = !string.IsNullOrEmpty(typeName) && typeName.TrimEnd().EndsWith("*", StringComparison.Ordinal);
if (!isArrayValue && !isPointer)
{
    return AD7_HRESULT.S_GETMEMORYCONTEXT_NO_MEMORY_CONTEXT;
}

TypeName is already on IVariableInformation, and the same EndsWith("*") pointer test is used elsewhere in Variables.cs (IsPointer).

Testing

Built the patched engine and ran it against a live cppdbg/gdb session:

  • Pointer (int32_t *, const uint32_t *) — still shows its address and the memory icon, unchanged.
  • Scalar (uint32_t) — now shows just the value, with no memory icon and no navigation to 0x<value>.
  • Array — unchanged (still resolves to its base address via the existing &(...) path).

Note: the change was written with the help of an LLM and verified manually in VS Code against a real gdb target.

AD7Property.GetMemoryContext interpreted the evaluation result's display
value as a memory address for every type. For a pointer this is correct
(the value is an address), but for a scalar such as a uint32_t whose value
is 1, the value 1 was returned as the memory reference, so the data-tip
memory icon navigated to address 0x1.

Only treat the value as an address when the result is a pointer (or an
array, which already re-evaluates to its address below); otherwise return
S_GETMEMORYCONTEXT_NO_MEMORY_CONTEXT so no memoryReference is reported.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant