Fix incorrect dereferences in indexed structure member access
authorRobert Pengelly <robertapengelly@hotmail.com>
Tue, 17 Nov 2026 12:59:36 +0000 (12:59 +0000)
committerRobert Pengelly <robertapengelly@hotmail.com>
Tue, 17 Nov 2026 12:59:36 +0000 (12:59 +0000)
amd64.c
i386.c

diff --git a/amd64.c b/amd64.c
index 909ad543b126b40f9a459b180a8eff56a7cc04c7..40a61522ea462f196fb34e268e05ee8441071f05 100644 (file)
--- 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 5e17f1a49af7a2a733cde41d0996a67957f49ff2..94c4f94a61b63d119421000155a0ddf5a3d4077a 100644 (file)
--- 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);