From e60ead6e7cce56ec3f3e145e5727e52decd8aa6d Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Tue, 17 Nov 2026 12:59:36 +0000 Subject: [PATCH] Fix incorrect dereferences in indexed structure member access --- amd64.c | 19 ++++++++++++++++++- i386.c | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/amd64.c b/amd64.c index 909ad54..40a6152 100644 --- a/amd64.c +++ b/amd64.c @@ -14976,6 +14976,8 @@ static void emit_apply_postfix_member_access_to_reg_now (const char *reg) { emit_parse_postfix_subscript_scaled_address_to_reg_now (reg, subscript_elem_size); } + postfix_member_seen = 1; + if (tok.kind == TOK_ARROW && subscript_elem_size <= (DATA_PTR & 0x1f)) { emit_load_deref_reg_now (reg, DATA_PTR & 0x1f); @@ -20193,12 +20195,18 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) { int subscript_pointer_depth = rhs_last_pointer_depth; int subscript_elem_size = rhs_last_pointed_size; + const char *subscript_tag_name = postfix_copy_lvalue_tag_name; + int subscript_object_size = postfix_copy_lvalue_size; + if (subscript_elem_size <= 0) { subscript_elem_size = DATA_INT & 0x1f; } emit_parse_postfix_subscript_scaled_address_to_reg_now (reg, subscript_elem_size); + postfix_copy_lvalue_size = subscript_object_size; + postfix_copy_lvalue_tag_name = subscript_tag_name; + if (is_assignment_operator (tok.kind)) { enum token_kind assign_op = tok.kind; @@ -20277,7 +20285,10 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) { } - emit_load_deref_reg_ex_now (reg, subscript_elem_size, pending_cast_subscript_is_unsigned >= 0 ? pending_cast_subscript_is_unsigned : 0); + if (tok.kind != TOK_DOT) { + emit_load_deref_reg_ex_now (reg, subscript_elem_size, pending_cast_subscript_is_unsigned >= 0 ? pending_cast_subscript_is_unsigned : 0); + } + pending_cast_subscript_is_unsigned = -1; if (subscript_pointer_depth > 0) { @@ -21246,6 +21257,9 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) { clear_rhs_last_pointer_info (); } + postfix_copy_lvalue_size = lhs_sym->pointed_size; + postfix_copy_lvalue_tag_name = lhs_sym->pointed_tag_name; + if (emit_handle_subscript_after_loaded_pointer_to_reg_now (reg, lhs_sym->pointer_depth > 0 ? lhs_sym->pointer_depth - 1 : 0, lhs_sym->pointed_size, lhs_sym->pointed_is_unsigned, 0)) { free (lhs_name); @@ -21285,6 +21299,9 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) { clear_rhs_last_pointer_info (); } + postfix_copy_lvalue_size = get_global_symbol_pointed_size (lhs_name); + postfix_copy_lvalue_tag_name = get_global_symbol_tag_name (lhs_name); + if (emit_handle_subscript_after_loaded_pointer_to_reg_now (reg, get_global_symbol_pointer_depth (lhs_name) > 0 ? get_global_symbol_pointer_depth (lhs_name) - 1 : 0, get_global_symbol_pointed_size (lhs_name), get_global_symbol_pointed_is_unsigned (lhs_name), 0)) { free (lhs_name); diff --git a/i386.c b/i386.c index 5e17f1a..94c4f94 100644 --- a/i386.c +++ b/i386.c @@ -13874,6 +13874,8 @@ static void emit_apply_postfix_member_access_to_reg_now (const char *reg) { subscript_count = emit_parse_postfix_subscript_scaled_address_to_reg_now (reg, subscript_elem_size); } + postfix_member_seen = 1; + if (tok.kind == TOK_ARROW && subscript_elem_size <= (DATA_PTR & 0x1f)) { emit_load_deref_reg_now (reg, DATA_PTR & 0x1f); @@ -19034,12 +19036,18 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) { int subscript_pointer_depth = rhs_last_pointer_depth; int subscript_elem_size = rhs_last_pointed_size; + const char *subscript_tag_name = postfix_copy_lvalue_tag_name; + int subscript_object_size = postfix_copy_lvalue_size; + if (subscript_elem_size <= 0) { subscript_elem_size = DATA_INT & 0x1f; } emit_parse_postfix_subscript_scaled_address_to_reg_now (reg, subscript_elem_size); + postfix_copy_lvalue_size = subscript_object_size; + postfix_copy_lvalue_tag_name = subscript_tag_name; + if (is_assignment_operator (tok.kind)) { enum token_kind assign_op = tok.kind; @@ -19118,7 +19126,10 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) { } - emit_load_deref_reg_ex_now (reg, subscript_elem_size, pending_cast_subscript_is_unsigned >= 0 ? pending_cast_subscript_is_unsigned : 0); + if (tok.kind != TOK_DOT) { + emit_load_deref_reg_ex_now (reg, subscript_elem_size, pending_cast_subscript_is_unsigned >= 0 ? pending_cast_subscript_is_unsigned : 0); + } + pending_cast_subscript_is_unsigned = -1; if (subscript_pointer_depth > 0) { @@ -20095,6 +20106,9 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) { clear_rhs_last_pointer_info (); } + postfix_copy_lvalue_size = lhs_sym->pointed_size; + postfix_copy_lvalue_tag_name = lhs_sym->pointed_tag_name; + if (emit_handle_subscript_after_loaded_pointer_to_reg_now (reg, lhs_sym->pointer_depth > 0 ? lhs_sym->pointer_depth - 1 : 0, lhs_sym->pointed_size, lhs_sym->pointed_is_unsigned, 0)) { free (lhs_name); @@ -20135,6 +20149,9 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) { clear_rhs_last_pointer_info (); } + postfix_copy_lvalue_size = get_global_symbol_pointed_size (lhs_name); + postfix_copy_lvalue_tag_name = get_global_symbol_tag_name (lhs_name); + if (emit_handle_subscript_after_loaded_pointer_to_reg_now (reg, get_global_symbol_pointer_depth (lhs_name) > 0 ? get_global_symbol_pointer_depth (lhs_name) - 1 : 0, get_global_symbol_pointed_size (lhs_name), get_global_symbol_pointed_is_unsigned (lhs_name), 0)) { free (lhs_name); -- 2.34.1