Skip to content

Semantic analyser: no check for cycles in struct definitions #1

@cakemanny

Description

@cakemanny

This program is currently accepted

struct X {
    y: Y,
}
struct Y {
    x: X,
}

fn g() -> Y {
    let y: *Y = new Y{f()};
    *y
}
fn f() -> X {
    let x: *X = new X{g()};
    *x
}

fn main() -> int { f(); 0 }

The program is invalid as X and Y are infinitely embedded within themselves, thus calculation of sizeof(X) or sizeof(Y) does not terminate.

It leads to a stack overflow in the activation record calculation

Details
~/src/c/structlang (master) % echo '
struct X {
    y: Y,
}
struct Y {
    x: X,
}

fn g() -> Y {
    let y: *Y = new Y{f()};
    *y
}
fn f() -> X {
    let x: *X = new X{g()};
    *x
}

fn main() -> int { f(); 0 }

' | ./build/debug/structlangc -
AddressSanitizer:DEADLYSIGNAL
=================================================================
==84056==ERROR: AddressSanitizer: stack-overflow on address 0x00016d05fff0 (pc 0x0001025afcf4 bp 0x00016d060020 sp 0x00016d05ffd0 T0)
    #0 0x1025afcf4 in lookup_builtin activation.c:139
    #1 0x1025af7a4 in alignment_of_type activation.c:167
    #2 0x1025af968 in alignment_of_type activation.c:178
    #3 0x1025af968 in alignment_of_type activation.c:178
    #4 0x1025af968 in alignment_of_type activation.c:178
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions