Skip to content

Fixes binaryninja-api#6825 #6916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions lang/c/pseudoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2569,7 +2569,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H
if (type && (type->GetClass() == NamedTypeReferenceClass))
type = GetFunction()->GetView()->GetTypeByRef(type->GetNamedTypeReference());

bool derefOffset = false;
// Handle structure member access
if (type && (type->GetClass() == StructureTypeClass))
{
std::optional<size_t> memberIndexHint;
Expand Down Expand Up @@ -2610,51 +2610,62 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H
memberIndexHint)
return;
}
else if (type && (type->GetClass() == StructureTypeClass))

// For non-struct types or when struct member resolution fails,
// render as pointer arithmetic: *[(type*)]([(char*)]expr[ + offset])
bool hasOffset = offset != 0;
bool needsOuterParens = precedence > UnaryOperatorPrecedence;
bool showTypeCasts = !settings || settings->IsOptionSet(ShowTypeCasts);

if (needsOuterParens)
tokens.AppendOpenParen();

tokens.Append(OperationToken, "*");

// Skip the outer cast if we're dereferencing a single byte and are
// already casting to char* for the pointer arithmetic
bool skipOuterCast = hasOffset && instr.size == 1;
if (showTypeCasts && !skipOuterCast)
{
derefOffset = true;
tokens.AppendOpenParen();
AppendSizeToken(hasOffset && type ? srcExpr.size : instr.size, true, tokens);
tokens.Append(TextToken, "*");
tokens.AppendCloseParen();
}

if (derefOffset || offset != 0)
if (hasOffset)
{
bool parens = precedence > UnaryOperatorPrecedence;
if (parens)
tokens.AppendOpenParen();

tokens.Append(OperationToken, "*");
if (!settings || settings->IsOptionSet(ShowTypeCasts))
{
tokens.AppendOpenParen();
AppendSizeToken(!derefOffset ? srcExpr.size : instr.size, true, tokens);
tokens.Append(TextToken, "*");
tokens.AppendCloseParen();
}
tokens.AppendOpenParen();
if (!settings || settings->IsOptionSet(ShowTypeCasts))
if (showTypeCasts)
{
tokens.AppendOpenParen();
tokens.Append(TypeNameToken, "char");
tokens.Append(TextToken, "*");
tokens.AppendCloseParen();
}
}

if (srcExpr.operation == HLIL_CONST_PTR)
{
const auto constant = srcExpr.GetConstant<HLIL_CONST_PTR>();
tokens.AppendPointerTextToken(srcExpr, constant, settings, DisplaySymbolOnly, precedence);
}
else
{
GetExprTextInternal(srcExpr, tokens, settings, AddOperatorPrecedence);
}
if (srcExpr.operation == HLIL_CONST_PTR)
{
const auto constant = srcExpr.GetConstant<HLIL_CONST_PTR>();
tokens.AppendPointerTextToken(srcExpr, constant, settings, DisplaySymbolOnly, precedence);
}
else
{
GetExprTextInternal(srcExpr, tokens, settings,
hasOffset ? AddOperatorPrecedence : UnaryOperatorPrecedence);
}

if (hasOffset)
{
tokens.Append(OperationToken, " + ");
tokens.AppendIntegerTextToken(instr, offset, instr.size);
tokens.AppendCloseParen();
if (parens)
tokens.AppendCloseParen();
}

if (needsOuterParens)
tokens.AppendCloseParen();

if (statement)
tokens.AppendSemicolon();
}();
Expand Down