fix: struct array run core dump
authoryu.dongliang <18588496441@163.com>
Fri, 1 Aug 2025 13:23:21 +0000 (21:23 +0800)
committeryu.dongliang <18588496441@163.com>
Fri, 1 Aug 2025 13:23:21 +0000 (21:23 +0800)
examples/struct_array.c [new file with mode: 0644]
native/x64/scf_x64_inst.c
parse/scf_operator_handler_expr.c

diff --git a/examples/struct_array.c b/examples/struct_array.c
new file mode 100644 (file)
index 0000000..06b6143
--- /dev/null
@@ -0,0 +1,20 @@
+int printf(const char* fmt, ...);
+
+struct A {
+       int x;
+};
+
+struct B
+{
+       A a[2];
+};
+
+B b = {
+       {{1}, {2}}
+};
+
+int main()
+{
+       printf("%d\n", b.a[0].x);
+       return 0;
+}
index be69b2e5988c8109ecd33b4dea52194696a0c6ec..985438098bc5046025323f5501642a2cb8c78137 100644 (file)
@@ -1049,7 +1049,7 @@ static int _x64_inst_array_index(scf_native_t* ctx, scf_3ac_code_t* c, int lea_f
                return ret;
        }
 
-       if (vb->nb_dimentions > 1 || lea_flag) {
+       if (vb->nb_dimentions > 1 || (vb->type >= SCF_STRUCT && 0 == vb->nb_pointers) || lea_flag) {
                OpCode = x64_find_OpCode(SCF_X64_LEA, rd->bytes, rd->bytes, SCF_X64_E2G);
 
        } else {
index d5f643709623facdd2177551aa842ef547d538d5..62faa07509906d3865ad87dadd8d110553de4bb9 100644 (file)
@@ -105,7 +105,7 @@ static int _scf_op_expr_array_index(scf_ast_t* ast, scf_node_t** nodes, int nb_n
                return -EINVAL;
        }
 
-       if (!v0->const_literal_flag) {
+       if (!v0->const_literal_flag && !v0->member_flag) {
                scf_loge("\n");
                return -EINVAL;
        }