Skip to content
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

[CExpressionGenerator] MemberAccess/Call on complex expression is missing parentheses #372

Merged
Show file tree
Hide file tree
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
11 changes: 5 additions & 6 deletions decompiler/backend/cexpressiongenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ def visit_list_operation(self, op: operations.ListOperation) -> str:

def visit_unary_operation(self, op: operations.UnaryOperation) -> str:
"""Return a string representation of the given unary operation (e.g. !a or &a)."""
operand = self._visit_bracketed(op.operand) if self._has_lower_precedence(op.operand, op) else self.visit(op.operand)
if isinstance(op, MemberAccess):
operator_str = "->" if isinstance(op.struct_variable.type, Pointer) else self.C_SYNTAX[op.operation]
return f"{self.visit(op.struct_variable)}{operator_str}{op.member_name}"
operand = self._visit_bracketed(op.operand) if self._has_lower_precedence(op.operand, op) else self.visit(op.operand)
return f"{operand}{operator_str}{op.member_name}"
if op.operation == OperationType.cast and op.contraction:
return f"({int(op.type.size / 8)}: ){operand}"
if op.operation == OperationType.cast:
Expand Down Expand Up @@ -209,10 +209,9 @@ def visit_call(self, op: operations.Call) -> str:
Generic labels starting with 'arg' e.g. 'arg1', 'arg2' are being filtered.
Additionally we filter ellipsis argument '...' that is lifted from type string.
"""
func_name = self.visit(op.function)
if isinstance(op.function, expressions.Constant):
func_name = func_name.strip('"')
output = f"{func_name}("
func_expr_str = self._visit_bracketed(op.function) if self._has_lower_precedence(op.function, op) else self.visit(op.function)

output = f"{func_expr_str}("
if op.meta_data is not None:
parameter_names = op.meta_data.get("param_names", [])
is_tailcall = op.meta_data.get("is_tailcall")
Expand Down
16 changes: 16 additions & 0 deletions tests/backend/test_codegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,21 @@ def test_array_element_access_aggressive(self, operation, result):
),
"ptr->x->z->w",
),
(
MemberAccess(
offset=0,
member_name="x",
operands=[
BinaryOperation(
OperationType.plus,
[Variable("ptr", Pointer(Integer.int32_t())), Constant(1, Integer.int32_t())],
Pointer(Integer.int32_t()),
)
],
vartype=Integer.int32_t(),
),
"(ptr + 1)->x",
),
],
)
def test_member_access(self, operation, result):
Expand Down Expand Up @@ -816,6 +831,7 @@ def test_member_access(self, operation, result):
),
"foo(/* param1 */ x, /* param2 */ y, z)",
),
(Assignment(ListOperation([]), Call(UnaryOperation(OperationType.dereference, [var_x]), [])), "(*x)()"),
],
)
def test_call(self, expr, result):
Expand Down
Loading