Skip to content

Bug in initialization (accessing a local pointer variable, and failing) #1573

Closed
@certik

Description

@certik

The following diff:

--- a/integration_tests/structs_21.py
+++ b/integration_tests/structs_21.py
@@ -18,10 +18,19 @@ def f(c: CPtr):
     print(A)
     assert A == 3
 
+def g(c: CPtr):
+    i: i32
+    for i in range(3):
+        p: Pointer[S] = c_p_pointer(c, S)
+        A: i32 = p.a
+        print(A)
+        assert A == 3
+
 def main():
     s: S = S(3)
     p: CPtr = empty_c_void_p()
     p_c_pointer(pointer(s, S), p)
     f(p)
+    g(p)
 
 main()

Produces:

$ lpython --show-c structs_21.py
[...]
void g(void* c)
{
    int32_t i;
    for (i=0; i<=3 - 1; i++) {
        {
            struct S* p;
            int32_t A = p->a;
            p = (struct S*) c;
            A = p->a;
            printf("%d\n", A);
            ASSERT(A == 3);
        }
    }
}
[...]

Which is wrong. The p->a is accessed before it is initialized. This leads to a segfault.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions