Test case 1: Uninitialized memory access

These are also known as uninitialized memory reads (UMR) bugs. A classic case: local (or automatic) variables are, by definition, uninitialized (unlike globals, which are always preset to zero):

/* test case 1 : uninitialized var test case */static void uninit_var(){    int x;   /* static mem */    if (x)        printf("true case: x=%d\n", x);    else        printf("false case\n");}

In the preceding code, it's undefined what will occur at runtime as x is uninitialized and will thus have random content. Now, we run this test case as follows:

$ ./membugs 1true case: x=32604$ ./membugs 1true case: x=32611$ ./membugs 1true case: x=32627$ ./membugs 1true case: x=32709$ 

Thankfully, modern versions of the compiler (both gcc and ...

Get Hands-On System Programming with Linux 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.