Michael B
2012-01-17 09:04:07 UTC
struct bar {
int field1;
int field2;
int field3;
};
/* init_bar takes a pointer to a struct bar and initializes its fields */
void init_bar(struct bar *b, int val1, int val2, int val3) {
b->field1 = val1;
b->field2 = val2;
b->field3 = val3;
}
int buggy_func() {
struct bar *mybar;
init_bar(mybar, 42, 92, 86);
return mybar->field1;
}
int main() {
printf("The answer is %d\n",buggy_func());
return 0;
}
There is a segfault at the line:
return mybar->field1;
I think that this has to do with the that whatever happens in init_bar is local and the stack of init_bar disappears after it finishes. Even, if this is correct, please help me figure out how to describe this properly and refer to stacks and buffer overflow.