Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/out-of-bounds-5.c

raw
1 /* { dg-additional-options "-Wno-unused-but-set-variable" } */
2
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6
7 /* Tests with symbolic values. */
8
9 void test1 (size_t size)
10 {
11 char *buf = (char *)__builtin_malloc (size);
12 if (!buf) return;
13
14 buf[size] = '\0'; /* { dg-warning "heap-based buffer overflow" } */
15 __builtin_free (buf);
16 }
17
18 void test2 (size_t size)
19 {
20 char *buf = (char *)__builtin_malloc (size);
21 if (!buf) return;
22
23 buf[size + 1] = '\0'; /* { dg-warning "heap-based buffer overflow" } */
24 __builtin_free (buf);
25 }
26
27 void test3 (size_t size, size_t op)
28 {
29 char *buf = (char *)__builtin_malloc (size);
30 if (!buf) return;
31
32 buf[size + op] = '\0'; /* { dg-warning "heap-based buffer overflow" } */
33 __builtin_free (buf);
34 }
35
36 void test4 (size_t size, unsigned short s)
37 {
38 char *buf = (char *)__builtin_alloca (size);
39 buf[size + s] = '\0'; /* { dg-warning "stack-based buffer overflow" } */
40 }
41
42 void test5 (size_t size)
43 {
44 int32_t *buf = (int32_t *)__builtin_alloca (4 * size);
45 buf[size] = 42; /* { dg-warning "stack-based buffer overflow" } */
46 }
47
48 void test6 (size_t size)
49 {
50 int32_t *buf = (int32_t *)__builtin_alloca (4 * size);
51 memset (buf, 0, 4 * size);
52 int32_t last = *(buf + 4 * size); /* { dg-warning "stack-based buffer over-read" } */
53 }
54
55 void test7 (size_t size)
56 {
57 int32_t *buf = (int32_t *)__builtin_alloca (4 * size + 3); /* { dg-warning "allocated buffer size is not a multiple of the pointee's size" } */
58 buf[size] = 42; /* { dg-warning "stack-based buffer overflow" } */
59 }
60
61 /* Test where the offset itself is not out-of-bounds
62 but multiple bytes are read. */
63
64 void test8 (size_t size, size_t offset)
65 {
66 char src[size];
67 char dst[size];
68 memcpy (dst, src, size + offset); /* { dg-line test8 } */
69 /* { dg-warning "over-read" "warning" { target *-*-* } test8 } */
70 /* { dg-warning "overflow" "warning" { target *-*-* } test8 } */
71 }
72
73 void test9 (size_t size, size_t offset)
74 {
75 int32_t src[size];
76 int32_t dst[size];
77 memcpy (dst, src, 4 * size + 1); /* { dg-line test9 } */
78 /* { dg-warning "over-read" "warning" { target *-*-* } test9 } */
79 /* { dg-warning "overflow" "warning" { target *-*-* } test9 } */
80 }
81
82 /* Test for no false-positives. */
83
84 void test10 (size_t size)
85 {
86 int32_t buf[4 * size];
87 /* 4 * size is smaller than 4 * 4 * size. */
88 buf[size] = 42;
89 }
90
91 void test11 (size_t size)
92 {
93 int32_t *buf = (int32_t *)__builtin_alloca (4 * size + 5); /* { dg-warning "allocated buffer size is not a multiple of the pointee's size" } */
94 buf[size] = 42;
95 }
96
97 void test12 (size_t size, size_t offset)
98 {
99 int buf[size];
100 buf[offset] = 42;
101 }
102
103 void test13 (size_t size, int offset)
104 {
105 int buf[size];
106 /* We don't know whether offset is positive or not. */
107 buf[size + offset] = 42;
108 }
109
110 void test14 (size_t size, size_t offset, size_t offset2)
111 {
112 int buf[size];
113 /* We don't know whether offset > offset2. */
114 buf[size + offset - offset2] = 42;
115 }
116
117 void test15 (size_t a, size_t b)
118 {
119 int buf[a * b];
120 /* We can't reason about a*b < a+b either. */
121 buf[a + b] = 42;
122 }
123
124 /* Misc. */
125
126 char *test98 (const char *x, const char *y)
127 {
128 size_t len_x = __builtin_strlen (x);
129 size_t len_y = __builtin_strlen (y);
130 size_t sz = len_x + len_y + 1;
131 char *result = (char *)__builtin_malloc (sz);
132 if (!result)
133 return NULL;
134 __builtin_memcpy (result, x, len_x);
135 __builtin_memcpy (result + len_x, y, len_y);
136 result[len_x + len_y] = '\0';
137 return result;
138 }
139
140 char *test99 (const char *x, const char *y)
141 {
142 size_t len_x = __builtin_strlen (x);
143 size_t len_y = __builtin_strlen (y);
144 /* BUG (root cause): forgot to add 1 for terminator. */
145 size_t sz = len_x + len_y;
146 char *result = (char *)__builtin_malloc (sz);
147 if (!result)
148 return NULL;
149 __builtin_memcpy (result, x, len_x);
150 __builtin_memcpy (result + len_x, y, len_y);
151 /* BUG (symptom): off-by-one out-of-bounds write to heap. */
152 result[len_x + len_y] = '\0'; /* { dg-warning "heap-based buffer overflow" } */
153 return result;
154 }