-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.c
45 lines (35 loc) · 698 Bytes
/
2.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static void write_overflow_compilemem(void)
{
int i, arr[5];
for (i = 0; i <= 50; i++) {
arr[i] = 100; /* Bug: 'arr' overflows on i==5*/
}
}
static void write_overflow_dynmem(void)
{
char *dest, src[] = "abcd56789123456789";
dest = malloc(8);
if (!dest)
printf("malloc failed\n");
strcpy(dest, src); /* Bug: write overflow */
free(dest);
}
static void write_underflow(void)
{
char *p = malloc(8);
if (!p)
printf("malloc failed\n");
p--;
strncpy(p, "abcd5678", 8); /* Bug: write underflow */
free(++p);
}
int main()
{
//write_underflow();
write_overflow_dynmem();
//write_overflow_compilemem();
return 0;
}