thradams / cake

Cake a C23 front end and transpiler written in C
http://thradams.com/cake/index.html
GNU General Public License v3.0
533 stars 21 forks source link

[P1] Segmentation fault on `-fanalyzer` #146

Closed iphydf closed 6 months ago

iphydf commented 6 months ago

crash.c:

struct a {
  char b;
} c;
void d() {
  struct a e = c;
}

Invocation: cake -fanalyzer crash.c

Segfault happens here: https://github.com/thradams/cake/blob/e322139e9d493c2c7b9f9614037a5e5b569bd69e/src/object.c#L633

#22 0.968 Program received signal SIGSEGV, Segmentation fault.
#22 0.968 0x000000000041cd2e in set_object_state (ctx=ctx@entry=0x7fffb7a76810, p_type=p_type@entry=0x7fd0819df190, p_object=p_object@entry=0x7fd0819df158, p_source_type=p_source_type@entry=0x7fffb7a76110, p_object_source=p_object_source@entry=0x7fd0819d9a88, error_position=error_position@entry=0x7fd0819f62f0) at object.c:633
#22 0.968 633                                   set_object_state(ctx,
iphydf commented 6 months ago

Another segfault, I didn't check if it's the same root cause:

#include <ownership.h>

typedef struct Foo {
    int *owner p;
} Foo;

Foo *owner foo_new(void) {
    Foo *owner foo = (Foo *owner)calloc(1, sizeof(Foo));

    if (foo == NULL) {
        return NULL;
    }

    return foo;
}
thradams commented 6 months ago

The static variables state were not begin tracked.

struct a {
  char b;
} c;
void d() {
  struct a e = c;
}

here is the plan... initialize the global variables at declaration. if it is const the state if fixed in all the program. if the it is not const, each time we enter in a function the state will be "unknown" at the begging of the function. not sure how to implement. maybe at first usage inside the function make it "unknown" then i put in a list "already used".

iphydf commented 6 months ago

Cool, then the second one is a separate bug.

thradams commented 6 months ago

this sample also needs attention

void d() {
  static struct a {  char b;} c;
  struct a e = c;
}
thradams commented 6 months ago

I fixed the segmentation fault, but the state of global variables still wrong. I need to reset the state of global variables.

thradams commented 6 months ago

created global variables with -fanalyzer #150

thradams commented 6 months ago

segmentation fault fixed