1 /* Verify -Wstringop-overread is issued appropriately for calls to string
2 functions at -O0 and without -Wall.
4 { dg-options "-O0 -ftrack-macro-expansion=0" } */
6 typedef __SIZE_TYPE__
size_t;
11 // <libint.h> functions.
13 char* gettext (const char *);
15 // <stdio.h> functions.
17 typedef struct FILE FILE;
19 int fputs (const char*, FILE*);
20 int fputs_unlocked (const char*, FILE*);
22 int puts (const char*);
23 int puts_unlocked (const char*);
25 // <string.h> functions.
27 void* memchr (const void*, int, size_t);
28 int memcmp (const void*, const void*, size_t);
29 void* memcpy (void*, const void*, size_t);
30 void* mempcpy (void*, const void*, size_t);
31 void* memmove (void*, const void*, size_t);
33 char* strchr (const char*, int);
34 char* strrchr (const char*, int);
36 int strcmp (const char*, const char*);
37 int strncmp (const char*, const char*, size_t);
39 char* strcat (char*, const char*);
40 char* stpcpy (char*, const char*);
41 char* strcpy (char*, const char*);
42 char* stpncpy (char*, const char*, size_t);
43 char* strncpy (char*, const char*, size_t);
44 char* strdup (const char*);
45 char* strndup (const char*, size_t);
47 char* strpbrk (const char*, const char*);
48 size_t strcspn (const char*, const char*);
49 size_t strspn (const char*, const char*);
50 char* strstr (const char*, const char*);
52 size_t strlen (const char*);
53 size_t strnlen (const char*, size_t);
56 extern void* malloc (size_t);
64 const char arr
[7] = "abc\0def";
66 /* Unterminated array at the end of ARR above. */
67 #define unterm (arr + __builtin_strlen (arr) + 1)
69 /* Size of the unterminated array - 1. */
70 #define unterm_size (sizeof arr - __builtin_strlen (arr) - 1)
72 const void* nowarn_memchr (int x
)
74 const char *p1
= unterm
;
75 return memchr (p1
, x
, unterm_size
);
78 const void* warn_memchr (int x
)
80 const char *p1
= unterm
;
81 return memchr (p1
, x
, unterm_size
+ 1); // { dg-warning "-Wstringop-overread" }
85 void* nowarn_memcpy (void)
87 const char *s
= unterm
;
88 return memcpy (d
, s
, unterm_size
);
91 void* warn_memcpy (void)
93 const char *s
= unterm
;
94 /* Use + 2 for an odd size to prevent the memmove --> MEM_REF transform
95 from defeating the warning (for now). */
96 return memcpy (d
, s
, unterm_size
+ 2 | 1); // { dg-warning "-Wstringop-overread" }
100 void* nowarn_mempcpy (void)
102 const char *s
= unterm
;
103 return mempcpy (d
, s
, unterm_size
);
106 void* warn_mempcpy (void)
108 const char *s
= unterm
;
109 /* Use + 2 for an odd size to prevent the memmove --> MEM_REF transform
110 from defeating the warning (for now). */
111 return mempcpy (d
, s
, unterm_size
+ 2 | 1); // { dg-warning "-Wstringop-overread" }
115 void* nowarn_memmove (void)
117 const char *s
= unterm
;
118 return memmove (d
, s
, unterm_size
);
121 void* warn_memmove (void)
123 const char *s
= unterm
;
124 /* Use + 2 for an odd size to prevent the memmove --> MEM_REF transform
125 from defeating the warning (for now). */
126 return memmove (d
, s
, unterm_size
+ 2); // { dg-warning "-Wstringop-overread" }
130 int nowarn_memcmp_1 (const char *p2
)
132 const char *p1
= unterm
;
133 return memcmp (p1
, p2
, unterm_size
);
136 int warn_memcmp_1 (const char *p2
)
138 const char *p1
= unterm
;
139 return memcmp (p1
, p2
, unterm_size
+ 1); // { dg-warning "-Wstringop-overread" }
142 int nowarn_memcmp_2 (const char *p1
)
144 const char *p2
= unterm
;
145 return memcmp (p1
, p2
, unterm_size
);
148 int warn_memcmp_2 (const char *p1
)
150 const char *p2
= unterm
;
151 return memcmp (p1
, p2
, unterm_size
+ 1); // { dg-warning "-Wstringop-overread" }
155 void warn_strcat (void)
157 strcat (d
, unterm
); // { dg-warning "-Wstringop-overread" }
160 void warn_strcat_a0 (void)
162 strcat (d
, a0
); // { dg-warning "-Wstringop-overread" }
165 void warn_strcat_end (void)
167 const char *s
= arr
+ sizeof arr
;
168 strcat (d
, s
); // { dg-warning "-Wstringop-overread" }
171 char* warn_stpcpy (void)
173 return stpcpy (d
, unterm
); // { dg-warning "-Wstringop-overread" }
176 char* warn_stpcpy_a0 (void)
178 return stpcpy (d
, a0
); // { dg-warning "-Wstringop-overread" }
181 char* warn_stpcpy_end (void)
183 const char *s
= arr
+ sizeof arr
;
184 return stpcpy (d
, s
); // { dg-warning "-Wstringop-overread" }
187 char* warn_stpcpy_malloc0 (void)
189 char *s
= malloc (0);
191 return stpcpy (d
, s
); // { dg-warning "-Wstringop-overread" }
195 void warn_strcpy (void)
197 strcpy (d
, unterm
); // { dg-warning "-Wstringop-overread" }
200 void warn_strcpy_a0 (void)
202 strcpy (d
, a0
); // { dg-warning "-Wstringop-overread" }
205 void warn_strcpy_end (void)
207 const char *s
= arr
+ sizeof arr
;
208 strcpy (d
, s
); // { dg-warning "-Wstringop-overread" }
211 void warn_strcpy_malloc0 (void)
213 char *s
= malloc (0);
215 strcpy (d
, s
); // { dg-warning "-Wstringop-overread" }
219 char* nowarn_stpncpy (void)
221 const char *s
= unterm
;
222 return stpncpy (d
, s
, unterm_size
);
225 char* warn_stpncpy (void)
227 const char *s
= unterm
;
228 return stpncpy (d
, s
, unterm_size
+ 1); // { dg-warning "-Wstringop-overread" }
231 char* warn_stpncpy_a0 (void)
233 return stpncpy (d
, a0
, 3); // { dg-warning "-Wstringop-overread" }
236 char* warn_stpncpy_end (void)
238 const char *s
= arr
+ sizeof arr
;
239 return stpncpy (d
, s
, sizeof arr
); // { dg-warning "-Wstringop-overread" }
243 void nowarn_strncpy (void)
245 const char *s
= unterm
;
246 strncpy (d
, s
, unterm_size
);
249 void warn_strncpy (void)
251 const char *s
= unterm
;
252 strncpy (d
, s
, unterm_size
+ 1); // { dg-warning "-Wstringop-overread" }
255 void warn_strncpy_a0 (void)
258 strncpy (d
, s
, sizeof arr
); // { dg-warning "-Wstringop-overread" }
261 void warn_strncpy_end (void)
263 const char *s
= arr
+ sizeof arr
;
264 strncpy (d
, s
, sizeof arr
); // { dg-warning "-Wstringop-overread" }
268 int warn_strlen (void)
270 return strlen (unterm
); // { dg-warning "-Wstringop-overread" }
273 int warn_strlen_a0 (void)
275 return strlen (a0
); // { dg-warning "-Wstringop-overread" }
278 int warn_strlen_end (void)
280 const char *s
= arr
+ sizeof arr
;
281 return strlen (s
); // { dg-warning "-Wstringop-overread" }
284 int warn_strlen_malloc0 (void)
286 char *s
= malloc (0);
288 return strlen (s
); // { dg-warning "-Wstringop-overread" }
292 int nowarn_strnlen (void)
294 return strnlen (unterm
, unterm_size
);
297 int warn_strnlen (void)
299 return strnlen (unterm
, unterm_size
+ 1); // { dg-warning "-Wstringop-overread" }
302 int warn_strnlen_end (void)
304 const char *s
= arr
+ sizeof arr
;
305 return strnlen (s
, 2); // { dg-warning "-Wstringop-overread" }
309 int warn_strcmp_1 (const char *s
)
311 return strcmp (unterm
, s
); // { dg-warning "-Wstringop-overread" }
314 int warn_strcmp_2 (const char *s
)
316 return strcmp (s
, unterm
); // { dg-warning "-Wstringop-overread" }
319 int warn_strcmp_2_end (const char *s
)
321 const char *t
= arr
+ sizeof arr
;
322 return strcmp (s
, t
); // { dg-warning "-Wstringop-overread" }
326 int nowarn_strncmp_1 (const char *s2
)
328 const char *s1
= unterm
;
329 return strncmp (s1
, s2
, unterm_size
);
332 int warn_strncmp_1 (const char *s2
)
334 const char *s1
= unterm
;
335 return strncmp (s1
, s2
, unterm_size
+ 1); // { dg-warning "-Wstringop-overread" }
338 int nowarn_strncmp_2 (const char *s1
)
340 const char *s2
= unterm
;
341 return strncmp (s1
, s2
, unterm_size
);
344 int warn_strncmp_2 (const char *s1
)
346 const char *s2
= unterm
;
347 return strncmp (s1
, s2
, unterm_size
+ 1); // { dg-warning "-Wstringop-overread" }
350 int warn_strncmp_2_end (const char *s1
)
352 const char *s2
= arr
+ sizeof arr
;;
353 return strncmp (s1
, s2
, sizeof arr
); // { dg-warning "-Wstringop-overread" }
357 int nowarn_strncmp_1_s2 (void)
359 /* Since the read is also bounded by the length of the S2 literal
360 and so safe, expect no warning. */
361 const char *s
= unterm
;
362 return strncmp (s
, S2
, unterm_size
+ 1); // { dg-bogus "-Wstringop-overread" "pr101778" { xfail *-*-* } }
365 int warn_strncmp_2_s2 (void)
368 const char *t
= unterm
;
369 return strncmp (S2
, t
, unterm_size
+ 1); // { dg-bogus "-Wstringop-overread" "pr101778" { xfail *-*-* } }
373 int warn_strncmp_1_s9 (void)
375 /* Since both the bound and the length of the S9 literal are greater
376 than the size of UNNTERM the call reads past the end of the array.
378 const char *s1
= unterm
;
379 return strncmp (s1
, S9
, unterm_size
+ 1); // { dg-warning "-Wstringop-overread" }
382 int warn_strncmp_2_s9 (void)
385 const char *s2
= unterm
;
386 return strncmp (S9
, s2
, unterm_size
+ 1); // { dg-warning "-Wstringop-overread" }
390 const char* warn_strchr (int x
)
392 return strchr (unterm
, x
); // { dg-warning "-Wstringop-overread" }
395 const char* warn_strchr_end (int x
)
397 const char *s
= arr
+ sizeof arr
;
398 return strchr (s
, x
); // { dg-warning "-Wstringop-overread" }
402 const char* warn_strrchr (int x
)
404 return strrchr (unterm
, x
); // { dg-warning "-Wstringop-overread" }
407 const char* warn_strrchr_end (int x
)
409 const char *s
= arr
+ sizeof arr
;
410 return strrchr (s
, x
); // { dg-warning "-Wstringop-overread" }
414 char* warn_strdup (void)
416 return strdup (unterm
); // { dg-warning "-Wstringop-overread" }
419 char* warn_strdup_end (void)
421 const char *s
= arr
+ sizeof arr
;
422 return strdup (s
); // { dg-warning "-Wstringop-overread" }
426 char* nowarn_strndup (void)
428 return strndup (unterm
, unterm_size
);
431 char* warn_strndup (void)
433 return strndup (unterm
, unterm_size
+ 1); // { dg-warning "-Wstringop-overread" }
436 char* warn_strndup_end (void)
438 const char *s
= arr
+ sizeof arr
;
439 return strndup (s
, sizeof arr
); // { dg-warning "-Wstringop-overread" }
443 const char* warn_strpbrk_1 (const char *s2
)
445 return strpbrk (unterm
, s2
); // { dg-warning "-Wstringop-overread" }
448 const char* warn_strpbrk_2 (const char *s1
)
450 return strpbrk (s1
, unterm
); // { dg-warning "-Wstringop-overread" }
454 size_t warn_strspn_1 (const char *s2
)
456 return strspn (unterm
, s2
); // { dg-warning "-Wstringop-overread" }
459 size_t warn_strspn_1_end (const char *s2
)
461 const char *s1
= arr
+ sizeof arr
;
462 return strspn (s1
, s2
); // { dg-warning "-Wstringop-overread" }
465 size_t warn_strspn_2 (const char *s1
)
467 return strspn (s1
, unterm
); // { dg-warning "-Wstringop-overread" }
470 size_t warn_strspn_2_end (const char *s1
)
472 const char *s2
= arr
+ sizeof arr
;
473 return strspn (s1
, s2
); // { dg-warning "-Wstringop-overread" }
477 size_t warn_strcspn_1 (const char *s2
)
479 return strcspn (unterm
, s2
); // { dg-warning "-Wstringop-overread" }
482 size_t warn_strcspn_1_end (const char *s2
)
484 const char *s1
= arr
+ sizeof arr
;
485 return strcspn (s1
, s2
); // { dg-warning "-Wstringop-overread" }
488 size_t warn_strcspn_2 (const char *s1
)
490 return strcspn (s1
, unterm
); // { dg-warning "-Wstringop-overread" }
493 size_t warn_strcspn_2_end (const char *s1
)
495 const char *s2
= arr
+ sizeof arr
;
496 return strcspn (s1
, s2
); // { dg-warning "-Wstringop-overread" }
500 const char* warn_strstr_1 (const char *s2
)
502 return strstr (unterm
, s2
); // { dg-warning "-Wstringop-overread" }
505 const char* warn_strstr_1_end (const char *s2
)
507 const char *s1
= arr
+ sizeof arr
;
508 return strstr (s1
, s2
); // { dg-warning "-Wstringop-overread" }
512 const char* warn_strstr_2 (const char *s1
)
514 return strstr (s1
, unterm
); // { dg-warning "-Wstringop-overread" }
517 const char* warn_strstr_2_end (const char *s1
)
519 const char *s2
= arr
+ sizeof arr
;
520 return strstr (s1
, s2
); // { dg-warning "-Wstringop-overread" }
524 void warn_puts (void)
526 puts (unterm
); // { dg-warning "-Wstringop-overread" }
529 void warn_puts_end (void)
531 const char *s
= arr
+ sizeof arr
;
532 puts (s
); // { dg-warning "-Wstringop-overread" }
536 void warn_fputs (FILE *f
)
538 fputs (unterm
, f
); // { dg-warning "-Wstringop-overread" }
541 void warn_fputs_end (FILE *f
)
543 const char *s
= arr
+ sizeof arr
;
544 fputs (s
, f
); // { dg-warning "-Wstringop-overread" }
548 void warn_puts_unlocked (void)
550 puts_unlocked (unterm
); // { dg-warning "-Wstringop-overread" }
553 void warn_puts_unlocked_end (void)
555 const char *s
= arr
+ sizeof arr
;
556 puts_unlocked (s
); // { dg-warning "-Wstringop-overread" }
559 void warn_fputs_unlocked (FILE *f
)
561 fputs_unlocked (unterm
, f
); // { dg-warning "-Wstringop-overread" }
565 const char* warn_gettext (void)
567 return gettext (unterm
); // { dg-warning "-Wstringop-overread" }
570 const char* warn_gettext_end (void)
572 const char *s
= arr
+ sizeof arr
;
573 return gettext (s
); // { dg-warning "-Wstringop-overread" }