Welcome, guest | Sign In | My Account | Store | Cart

Free segmentation fault caused by buffer overflow

C, 52 lines
 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int main(int argc, char **argv)
{

    char *p;
    char *q;


    int num = 0;

    while(num < 512 ){
        printf("%d\n", num);
        p = calloc(128, 1);
        q = calloc(128,1);

        int i = 0;
        while( i < num){
            *(p+i) = 'a';
            i++;
        }

        printf("zzzz\n");
        free(p);
        printf("aaaa\n");
        free(q);
        num++;
    }


    return 0;
}

output:

zzzz
aaaa
132
zzzz
aaaa
133
zzzz
Segmentation fault


The above code indicates the Pre-Bufferoverflow. There's also post-Bufferoverflow, e.g.

char *p;
char *q;

p = malloc(...)
q = malloc(.N..)

strcpy(q, 2N)
Created by J Y on Sat, 10 Oct 2009 (MIT)
C recipes (32)
J Y's recipes (21)

Required Modules

  • (none specified)

Other Information and Tasks