1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {

    int* a = malloc(sizeof(*a) * 3);

    // BUFFER OVERFLOW ("INVALID WRITE")
    a[100] = 5;


    free(a);
    a == NULL; // TIP: set to NULL after free(…) unless you will return immediately

    return EXIT_SUCCESS;
}
/*
==30093== Invalid write of size 4
==30093==    at 0x4005CD: main (buffer_overflow.c:9)
==30093==  Address 0x4c3e1d0 is not stack'd, malloc'd or (recently) free'd
==30093==
==30093==
==30093== HEAP SUMMARY:
==30093==     in use at exit: 0 bytes in 0 blocks
==30093==   total heap usage: 1 allocs, 1 frees, 12 bytes allocated
==30093==
==30093== All heap blocks were freed -- no leaks are possible
*/
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2017 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.