A.2 NULL Pointer Dereferences

Memory is divided into pages. Typically, a process, a thread, or the kernel cannot read from or write to a memory location on the zero page. Example A-2 shows a simple example of what happens if the zero page gets referenced due to a programming error.

Example A-2. Using unowned memory—an example NULL pointer dereference

01    #include <stdio.h>
02
03    typedef struct pkt {
04        char *  value;
05    } pkt_t;
06
07    int
08    main (void)
09    {
10        pkt_t *  packet  = NULL;
11
12        printf ("%s", packet->value);
13
14        return 0;
15    }

In line 10 of Example A-2 the data structure packet is initialized with NULL, and in line 12 a structure member gets referenced. Since packet points to NULL, this reference can be represented as NULL->value. This ...

Get A Bug Hunter's Diary now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.