Back to SEGV on unknown address
Here is the code for this example (source):
// Created for COMP2521 sanitiser guide
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
int main(void) {
struct node *n = malloc(sizeof(*n));
n->value = 0;
n->next->value = 1;
}
This code tries to create a linked list containing the values 0
and 1
.
Here is the error message:
0xbebebebebebebebe
n->next->value = 1;
Note: This error is not super helpful, if we switch to MemorySanitizer we get a more informative error message which you can see here.
0xbebebebebebebebe
is the value LeakSanitizer uses for uninitialised pointers. This means the pointer we are trying to access is uninitialised. This is further supported by the hint.
On line 13, we try to set the value
field of n->next
. However, when we allocated memory for n
we never initialised n->next
, so writing to it causes an error.
We need to initialise n->next
by allocating memory to it (i.e. n->next = malloc(...)
).