#include #include #include // 0: Correct code. Compare every other report against this clean one. static void case_clean(void) { int *a = malloc(4 * sizeof(int)); for (int i = 0; i < 4; i++) a[i] = i * i; printf("a[3] = %d\n", a[3]); free(a); } // 1: Memory leak. Look at the "definitely lost" section of the LEAK SUMMARY. static void case_leak(void) { char *p = malloc(100); strcpy(p, "I am never freed"); printf("%s\n", p); // missing: free(p); } // 2: Heap buffer overflow (off by one). "hello" needs 6 bytes with the '\0'. // Expect: Invalid write of size 1 ... 0 bytes after a block of size 5. static void case_heap_overflow(void) { char *p = malloc(5); strcpy(p, "hello"); printf("%s\n", p); free(p); } // 3: Invalid read past the end of a heap array (classic loop bound bug: <= vs <). static void case_invalid_read(void) { int n = 4; int *a = malloc(n * sizeof(int)); for (int i = 0; i < n; i++) a[i] = i; int sum = 0; for (int i = 0; i <= n; i++) sum += a[i]; // reads a[4], out of bounds printf("sum = %d\n", sum); free(a); } // 4: Use after free. Expect "Invalid read ... inside a block of size 16 free'd at ...". static void case_use_after_free(void) { int *a = malloc(4 * sizeof(int)); a[0] = 42; free(a); printf("a[0] = %d\n", a[0]); // freed memory } // 5: Double free. static void case_double_free(void) { char *p = malloc(10); free(p); free(p); } // 6: Conditional jump depends on an uninitialised value. The program may "work", // which is exactly why this bug is nasty. Note the --track-origins=yes output, // which points at the malloc that produced the garbage. static void case_uninitialised(void) { int *a = malloc(4 * sizeof(int)); a[0] = 1; // a[1..3] never set if (a[2] > 0) printf("positive\n"); // branching on garbage else printf("not positive\n"); free(a); } // 7: strcat onto malloc'd memory that was never initialised. strcat looks for the // '\0' of the destination first, so it scans garbage. (Fix: set buf[0] = '\0' // or use memcpy/strcpy first.) static void case_strcat_uninit(void) { char *buf = malloc(16); strcat(buf, "abc"); printf("%s\n", buf); free(buf); } // 8: Passing the ADDRESS of a pointer to realloc instead of the pointer itself. // Expect: Invalid free() / delete / realloc(), pointing at a stack address. static void case_bad_realloc(void) { int *arr = malloc(2 * sizeof(int)); arr = realloc(&arr, 8 * sizeof(int)); // should be realloc(arr, ...) free(arr); } // 9: Freeing a pointer that is not the start of a block. static void case_bad_free(void) { char *p = malloc(10); free(p + 1); free(p); } // 10: A leak hiding behind a lost pointer: overwriting the only reference. // Compare "definitely lost" with case 1; here the block is leaked by reassignment. static void case_overwritten_pointer(void) { char *p = malloc(32); p = malloc(64); // the first block is now unreachable free(p); } // Note: Valgrind's memcheck does NOT reliably catch overflows of stack arrays. // For those, compile with: gcc -g -fsanitize=address,undefined ... (AddressSanitizer) int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); return 1; } switch (atoi(argv[1])) { case 0: case_clean(); break; case 1: case_leak(); break; case 2: case_heap_overflow(); break; case 3: case_invalid_read(); break; case 4: case_use_after_free(); break; case 5: case_double_free(); break; case 6: case_uninitialised(); break; case 7: case_strcat_uninit(); break; case 8: case_bad_realloc(); break; case 9: case_bad_free(); break; case 10: case_overwritten_pointer();break; default: fprintf(stderr, "unknown case\n"); return 1; } return 0; }