Debian Codegraph @

gcc/testsuite/c-c++-common/Wstringop-truncation.c

raw
1 /* PR middle-end/81117 - Improve buffer overflow checking in strncpy
2 { dg-do compile }
3 { dg-options "-O2 -Wstringop-truncation -Wno-stringop-overflow -ftrack-macro-expansion=0" } */
4
5
6 typedef __SIZE_TYPE__ size_t;
7
8 #if __cplusplus
9 extern "C" {
10 #endif
11
12 size_t strlen (const char*);
13 char* strncat (char*, const char*, size_t);
14 char* strncpy (char*, const char*, size_t);
15
16 #if __cplusplus
17 }
18 #endif
19
20 static size_t unsigned_value (void)
21 {
22 extern volatile size_t unsigned_value_source;
23 return unsigned_value_source;
24 }
25
26 static size_t unsigned_range (size_t min, size_t max)
27 {
28 size_t val = unsigned_value ();
29 return val < min || max < val ? min : val;
30 }
31
32 #define UR(min, max) unsigned_range (min, max)
33
34 void sink (void*);
35
36 #define S4 "123"
37 const char a4[] = "123";
38
39 #define CHOOSE(a, b) (unsigned_value () & 1 ? a : b)
40
41
42 typedef struct Dest
43 {
44 char a5[5];
45 char b7[7];
46 char c3ns[3] __attribute__ ((nonstring));
47 } Dest;
48
49 char dst7[7];
50 char dst2_5[2][5];
51
52 /* Verify strncat warnings for arrays of known bounds. */
53
54 void test_strncat_array (Dest *pd)
55 {
56 #define CAT(d, s, len) (strncat ((d), (s), (len)), sink (d))
57
58 CAT (dst7, S4, 2); /* { dg-warning "output truncated copying 2 bytes from a string of length 3" } */
59
60 CAT (dst7, a4, 1); /* { dg-warning "output truncated copying 1 byte from a string of length 3" } */
61
62 /* There is no truncation here but possible overflow so these
63 are diagnosed by -Wstringop-overflow:
64 CAT (dst7, S4, 3);
65 CAT (dst7, a4, 3);
66 */
67
68 CAT (pd->a5, S4, 2); /* { dg-warning "output truncated copying 2 bytes from a string of length 3" } */
69 CAT (pd->a5, S4, 1); /* { dg-warning "output truncated copying 1 byte from a string of length 3" } */
70 }
71
72 /* Verify strncat warnings for arrays of known bounds and a non-const
73 character count in some range. */
74
75 void test_strncat_array_range (Dest *pd)
76 {
77 CAT (dst7, S4, UR (0, 1)); /* { dg-warning "output truncated copying between 0 and 1 bytes from a string of length 3" } */
78 CAT (dst7, S4, UR (0, 2)); /* { dg-warning "output truncated copying between 0 and 2 bytes from a string of length 3" } */
79 CAT (dst7, S4, UR (1, 3)); /* { dg-warning "output truncated copying between 1 and 3 bytes from a string of length 3" } */
80 CAT (dst7, S4, UR (2, 4)); /* { dg-warning "output may be truncated copying between 2 and 4 bytes from a string of length 3" } */
81
82 CAT (dst7, S4, UR (0, 7));
83 CAT (dst7, S4, UR (1, 7));
84 CAT (dst7, S4, UR (6, 7));
85
86 CAT (dst7, S4, UR (0, 99));
87
88 CAT (dst7, S4, UR (0, 99));
89 }
90
91 /* Verify strncat warnings for arrays of unknown bounds. */
92
93 void test_strncat_vla (char *d, unsigned n)
94 {
95 CAT (d, S4, 2); /* { dg-warning "output truncated copying 2 bytes from a string of length 3" } */
96 CAT (d, S4, 4);
97
98 CAT (d, a4, 2); /* { dg-warning "output truncated copying 2 bytes from a string of length 3" } */
99
100 /* There is no truncation here but possible overflow so these
101 are diagnosed by -Wstringop-overflow:
102 CAT (d, S4, 3);
103 CAT (d, a4, 3);
104 */
105 CAT (d, a4, 4);
106
107 char vla[n];
108
109 CAT (vla, S4, 2); /* { dg-warning "output truncated copying 2 bytes from a string of length 3" } */
110
111 CAT (vla, S4, 4);
112 CAT (vla, S4, n);
113
114 CAT (vla, a4, 2); /* { dg-warning "output truncated copying 2 bytes from a string of length 3" } */
115
116 CAT (vla, a4, 4);
117 CAT (vla, a4, n);
118
119 CAT (d, vla, 1);
120 CAT (d, vla, 3);
121 CAT (d, vla, 4);
122 CAT (d, vla, n);
123
124 /* There is no truncation here but possible overflow so these
125 are diagnosed by -Wstringop-overflow:
126 CAT (vla, S4, 3);
127 CAT (vla, a4, 3);
128 */
129 }
130
131 /* Verify strncpy warnings with at least one pointer to an object
132 or string of unknown size (destination) or length (source). */
133
134 void test_strncpy_ptr (char *d, const char* s, const char *t, int i)
135 {
136 #define CPY(d, s, len) (strncpy ((d), (s), (len)), sink (d))
137
138 /* Strncpy doesn't nul-terminate so the following is diagnosed. */
139 CPY (d, "", 0); /* { dg-warning ".strncpy\[^\n\r\]* destination unchanged after copying no bytes" } */
140 CPY (d, s, 0); /* { dg-warning ".strncpy\[^\n\r\]* destination unchanged after copying no bytes" } */
141
142 /* This is safe. */
143 CPY (d, "", 1);
144 CPY (d, "", 2);
145
146 /* This could be safe. */
147 CPY (d, s, 1);
148 CPY (d, s, 2);
149
150 /* Truncation. */
151 CPY (d, "123", 1); /* { dg-warning ".strncpy\[^\n\r\]* output truncated copying 1 byte from a string of length 3" } */
152 CPY (d, "123", 2); /* { dg-warning ".strncpy\[^\n\r\]* output truncated copying 2 bytes from a string of length 3" } */
153 CPY (d, "123", 3); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying 3 bytes from a string of the same length" } */
154 CPY (d, "123", 4);
155 CPY (d, "123", 9);
156
157 CPY (d, S4, sizeof S4); /* Covered by -Wsizeof-pointer-memaccess. */
158 CPY (d, S4, sizeof S4 - 1); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying 3 bytes from a string of the same length" } */
159
160 CPY (d, a4, sizeof a4); /* Covered by -Wsizeof-pointer-memaccess. */
161 CPY (d, a4, sizeof a4 - 1); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying 3 bytes from a string of the same length" } */
162 CPY (d, a4, sizeof a4 - 3); /* { dg-warning ".strncpy\[^\n\r\]* output truncated copying 1 byte from a string of length 3" } */
163 CPY (d, a4, sizeof a4 - 4); /* { dg-warning ".strncpy\[^\n\r\]* destination unchanged after copying no bytes from a string of length 3" } */
164
165 CPY (d, S4, strlen (S4)); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying 3 bytes from a string of the same length" } */
166 /* Likely buggy but no truncation. Diagnosed by -Wstringop-overflow. */
167 CPY (d, a4, strlen (a4) + 1);
168 CPY (d, S4, strlen (S4) + i);
169
170 CPY (d, a4, strlen (a4)); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying 3 bytes from a string of the same length" } */
171 /* As above, buggy but no evidence of truncation. */
172 CPY (d, S4, strlen (S4) + 1);
173
174 CPY (d, CHOOSE ("", "1"), 0); /* { dg-warning ".strncpy\[^\n\r\]* destination unchanged after copying no bytes" } */
175 CPY (d, CHOOSE ("1", "12"), 0); /* { dg-warning ".strncpy\[^\n\r\]* destination unchanged after copying no bytes" } */
176
177 CPY (d, CHOOSE ("", "1"), 1); /* { dg-warning ".strncpy\[^\n\r\]* output may be truncated copying 1 byte from a string of length 1" } */
178 CPY (d, CHOOSE ("1", ""), 1); /* { dg-warning ".strncpy\[^\n\r\]* output may be truncated copying 1 byte from a string of length 1" } */
179 CPY (d, CHOOSE (s, "1"), 1); /* { dg-warning ".strncpy\[^\n\r\]* output may be truncated copying 1 byte from a string of length 1" } */
180 CPY (d, CHOOSE (s, t), 1);
181
182 CPY (d, CHOOSE ("", "1"), 2);
183 CPY (d, CHOOSE ("1", ""), 2);
184 CPY (d, CHOOSE ("1", "2"), 2);
185 CPY (d, CHOOSE ("1", s), 2);
186 CPY (d, CHOOSE (s, "1"), 2);
187 CPY (d, CHOOSE (s, t), 2);
188
189 CPY (d, CHOOSE ("", "123"), 1); /* { dg-warning ".strncpy\[^\n\r\]* output may be truncated copying 1 byte from a string of length 3" } */
190 CPY (d, CHOOSE ("1", "123"), 1); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying 1 byte from a string of the same length" } */
191 CPY (d, CHOOSE ("12", "123"), 1); /* { dg-warning ".strncpy\[^\n\r\]* output truncated copying 1 byte from a string of length 2" } */
192 CPY (d, CHOOSE ("123", "12"), 1); /* { dg-warning ".strncpy\[^\n\r\]* output truncated copying 1 byte from a string of length 2" } */
193
194 {
195 signed char n = strlen (s); /* { dg-message "length computed here" } */
196 CPY (d, s, n); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying as many bytes from a string as its length" } */
197 }
198
199 {
200 short n = strlen (s); /* { dg-message "length computed here" } */
201 CPY (d, s, n); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying as many bytes from a string as its length" } */
202 }
203
204 {
205 int n = strlen (s); /* { dg-message "length computed here" } */
206 CPY (d, s, n); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying as many bytes from a string as its length" } */
207 }
208
209 {
210 unsigned n = strlen (s); /* { dg-message "length computed here" } */
211 CPY (d, s, n); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying as many bytes from a string as its length" } */
212 }
213
214 {
215 size_t n;
216 n = strlen (s); /* { dg-message "length computed here" } */
217 CPY (d, s, n); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying as many bytes from a string as its length" } */
218 }
219
220 {
221 size_t n;
222 char *dp2 = d + 1;
223 n = strlen (s); /* { dg-message "length computed here" } */
224 CPY (dp2, s, n); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying as many bytes from a string as its length" } */
225 }
226
227 {
228 /* The following truncates the terminating nul. The warning should
229 say that but doesn't. */
230 int n;
231 n = strlen (s) - 1;
232 CPY (d, s, n); /* { dg-warning "\\\[-Wstringop-truncation" } */
233 }
234
235 {
236 /* Same as above. */
237 size_t n;
238 n = strlen (s) - 1;
239 CPY (d, s, n); /* { dg-warning "\\\[-Wstringop-truncation" } */
240 }
241
242 {
243 size_t n = strlen (s) - strlen (s);
244 CPY (d, s, n); /* { dg-warning ".strncpy\[^\n\r\]* destination unchanged after copying no bytes" } */
245 }
246
247 {
248 /* This use of strncpy is dubious but it's probably not worth
249 worrying about (truncation may not actually take place when
250 i is the result). It is diagnosed with -Wstringop-overflow
251 (although more by accident than by design).
252
253 size_t n = i < strlen (s) ? i : strlen (s);
254 CPY (d, s, n);
255 */
256 }
257 }
258
259
260 /* Verify strncpy warnings for arrays of known bounds. */
261
262 void test_strncpy_array (Dest *pd, int i, const char* s)
263 {
264 #undef CPY
265 #define CPY(d, s, len) (strncpy ((d), (s), (len)), sink (d))
266
267 CPY (dst7, s, 7); /* { dg-warning "specified bound 7 equals destination size" } */
268 CPY (dst7, s, sizeof dst7); /* { dg-warning "specified bound 7 equals destination size" } */
269
270 CPY (dst2_5[0], s, sizeof dst2_5[0]); /* { dg-warning "specified bound 5 equals destination size" "bug 77293" } */
271 CPY (dst2_5[1], s, sizeof dst2_5[1]); /* { dg-warning "specified bound 5 equals destination size" } */
272
273 /* Verify that copies that nul-terminate are not diagnosed. */
274 CPY (dst7, "", sizeof dst7);
275 CPY (dst7 + 6, "", sizeof dst7 - 6);
276 CPY (dst7, "1", sizeof dst7);
277 CPY (dst7 + 1, "1", sizeof dst7 - 1);
278 CPY (dst7, "123456", sizeof dst7);
279 CPY (dst7 + 1, "12345", sizeof dst7 - 1);
280
281 CPY (dst7 + i, s, 6);
282 CPY (dst7 + i, s, 7); /* { dg-warning "specified bound 7 equals destination size" } */
283 /* The following two calls are diagnosed by -Wstringop-overflow. */
284 CPY (dst7 + i, s, 8);
285 CPY (dst7 + i, s, UR (8, 9));
286
287 /* No nul-termination here. */
288 CPY (dst7 + 2, "12345", sizeof dst7 - 2); /* { dg-warning "output truncated before terminating nul copying 5 bytes from a string of the same length" } */
289
290 /* Because strnlen appends as many NULs as necessary to write the specified
291 number of byts the following doesn't (necessarily) truncate but rather
292 overflow, and so is diagnosed by -Wstringop-overflow. */
293 CPY (dst7, s, 8);
294
295 CPY (dst7 + 1, s, 6); /* { dg-warning "specified bound 6 equals destination size" } */
296 CPY (dst7 + 6, s, 1); /* { dg-warning "specified bound 1 equals destination size" } */
297
298 CPY (pd->a5, s, 5); /* { dg-warning "specified bound 5 equals destination size" } */
299 CPY (pd->a5, s, sizeof pd->a5); /* { dg-warning "specified bound 5 equals destination size" } */
300
301 CPY (pd->a5 + i, s, sizeof pd->a5); /* { dg-warning "specified bound 5 equals destination size" "member array" } */
302
303 /* Verify that a copy that nul-terminates is not diagnosed. */
304 CPY (pd->a5, "1234", sizeof pd->a5);
305
306 /* Same above, diagnosed by -Wstringop-overflow. */
307 CPY (pd->a5, s, 6);
308
309 /* Exercise destination with attribute "nonstring". */
310 CPY (pd->c3ns, "", 3);
311 CPY (pd->c3ns, "", 1);
312 /* It could be argued that truncation in the literal case should be
313 diagnosed even for non-strings. Using strncpy in this case is
314 pointless and should be replaced with memcpy. But it would likely
315 be viewed as a false positive. */
316 CPY (pd->c3ns, "12", 1);
317 CPY (pd->c3ns, "12", 2);
318 CPY (pd->c3ns, "12", 3);
319 CPY (pd->c3ns, "123", 3);
320 CPY (pd->c3ns, s, 3);
321 CPY (pd->c3ns, s, sizeof pd->c3ns);
322
323 /* Verify that the idiom of calling strncpy with a bound equal to
324 the size of the destination (and thus potentially without NUL-
325 terminating it) immediately followed by setting the last element
326 of the array to NUL is not diagnosed. */
327 {
328 /* This might be better written using memcpy() but it's safe so
329 it shouldn't be diagnosed. */
330 strncpy (dst7, "0123456", sizeof dst7); /* { dg-bogus "\\\[-Wstringop-truncation]" } */
331 dst7[sizeof dst7 - 1] = '\0';
332 sink (dst7);
333 }
334
335 {
336 const char a[] = "0123456789";
337 strncpy (dst7, a, sizeof dst7);
338 dst7[sizeof dst7 - 1] = '\0';
339 sink (dst7);
340 }
341
342 {
343 strncpy (dst7, s, sizeof dst7);
344 dst7[sizeof dst7 - 1] = '\0';
345 sink (dst7);
346 }
347
348 {
349 strncpy (pd->a5, "01234", sizeof pd->a5); /* { dg-bogus "\\\[-Wstringop-truncation]" } */
350 pd->a5[sizeof pd->a5 - 1] = '\0';
351 sink (pd);
352 }
353
354 {
355 strncpy (pd->a5, s, sizeof pd->a5);
356 pd->a5[sizeof pd->a5 - 1] = '\0';
357 sink (pd);
358 }
359
360 {
361 unsigned n = 7;
362 char *p = (char*)__builtin_malloc (n);
363 strncpy (p, s, n);
364 p[n - 1] = '\0';
365 sink (p);
366 }
367
368 {
369 /* This should be diagnosed because the NUL-termination doesn't
370 immediately follow the strncpy call (sink may expect pd->a5
371 to be NUL-terminated). */
372 strncpy (pd->a5, s, sizeof pd->a5); /* { dg-warning "specified bound 5 equals destination size" } */
373 sink (pd);
374 pd->a5[sizeof pd->a5] = '\0';
375 sink (pd);
376 }
377 }
378
379 typedef struct Flex
380 {
381 size_t n;
382 char a0[0];
383 char ax[];
384 } Flex;
385
386 extern char array[];
387
388 /* Verify that no warning is issued for array of unknown bound, flexible
389 array members, or zero-length arrays, except when the source is definitely
390 truncated. */
391
392 void test_strncpy_flexarray (Flex *pf, const char* s)
393 {
394 #undef CPY
395 #define CPY(d, s, len) (strncpy ((d), (s), (len)), sink (d))
396
397 CPY (array, "12345", 7);
398 CPY (array, "12345", 123);
399
400 CPY (array, s, 7);
401 CPY (array, s, 123);
402
403 CPY (pf->a0, s, 1);
404 CPY (pf->a0, s, 1234);
405
406 CPY (pf->a0, "", 1);
407 CPY (pf->a0, "12345", 5); /* { dg-warning "output truncated before terminating nul copying 5 bytes from a string of the same length" } */
408 CPY (pf->a0, "12345", 1234);
409
410 CPY (pf->ax, s, 5);
411 CPY (pf->ax, s, 12345);
412
413 CPY (pf->ax, "1234", 5);
414 CPY (pf->ax, "12345", 5); /* { dg-warning "output truncated before terminating nul copying 5 bytes from a string of the same length" } */
415 CPY (pf->ax, "12345", 12345);
416 }
417
418 /* Verify warnings for dynamically allocated objects. */
419
420 void test_strncpy_alloc (const char* s)
421 {
422 size_t n = 7;
423 char *d = (char *)__builtin_malloc (n);
424
425 CPY (d, s, n); /* { dg-warning "specified bound 7 equals destination size" } */
426
427 Dest *pd = (Dest *)__builtin_malloc (sizeof *pd * n);
428 CPY (pd->a5, s, 5); /* { dg-warning "specified bound 5 equals destination size" } */
429 CPY (pd->a5, s, sizeof pd->a5); /* { dg-warning "specified bound 5 equals destination size" } */
430 }
431
432 /* Verify warnings for VLAs. */
433
434 void test_strncpy_vla (unsigned n, const char* s)
435 {
436 char vla[n];
437 CPY (vla, s, 0); /* { dg-warning ".strncpy\[^\n\r\]* destination unchanged after copying no bytes" } */
438
439 CPY (vla, s, 1);
440 CPY (vla, s, 2);
441 CPY (vla, s, n);
442
443 CPY (vla, "", 0); /* { dg-warning ".strncpy\[^\n\r\]* destination unchanged after copying no bytes" } */
444 CPY (vla, "", 1);
445 CPY (vla, S4, 3); /* { dg-warning ".strncpy\[^\n\r\]* output truncated before terminating nul copying 3 bytes from a string of the same length" } */
446 CPY (vla, S4, n);
447 }