libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / testsuite / gcc.dg / Wstringop-overread-2.c
blob16dc06d968bf8d1596a5884a993531a77c70b953
1 /* Verify -Wstringop-overread is issued for reading more than the maximum
2 object size but not for writing.
3 { dg-do compile }
4 { dg-options "-O2 -Wno-stringop-overflow -ftrack-macro-expansion=0" } */
6 #define PTRDIFF_MAX __PTRDIFF_MAX__
7 #define SIZE_MAX __SIZE_MAX__
9 #define NOIPA __attribute__ ((noipa))
11 typedef __SIZE_TYPE__ size_t;
13 void* memchr (const void*, int, size_t);
14 int memcmp (const void*, const void*, size_t);
15 void* memcpy (const void*, const void*, size_t);
17 int strncmp (const char*, const char*, size_t);
18 char* strncat (char*, const char*, size_t);
19 char* strncpy (char*, const char*, size_t);
20 size_t strnlen (const char*, size_t);
22 void sink (int, ...);
23 #define sink(...) sink (0, __VA_ARGS__)
24 #define T(exp) sink (exp)
26 NOIPA void test_memchr (const void *p, int x)
28 size_t dmax = PTRDIFF_MAX;
29 size_t smax = SIZE_MAX;
31 T (memchr (p, x, dmax));
33 T (memchr (p, x, dmax + 1)); // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
34 T (memchr (p, x, dmax * 2)); // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
35 T (memchr (p, x, smax)); // { dg-warning "\\\[-Wstringop-overread" }
39 NOIPA void test_memcmp (const void *p, const void *q)
41 size_t dmax = PTRDIFF_MAX;
42 size_t smax = SIZE_MAX;
44 T (memcmp (p, q, dmax));
46 T (memcmp (p, q, dmax + 1)); // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
47 T (memcmp (p, q, dmax * 2)); // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
48 T (memcmp (p, q, smax)); // { dg-warning "\\\[-Wstringop-overread" }
52 NOIPA void test_memcpy (void *p, const void *q)
54 size_t dmax = PTRDIFF_MAX;
55 size_t smax = SIZE_MAX;
57 T (memcpy (p, q, dmax));
59 T (memcpy (p, q, dmax + 1)); // -Wstringop-overflow disabled
60 T (memcpy (p, q, dmax * 2)); // ditto
61 T (memcpy (p, q, smax)); // ditto
65 NOIPA void test_strncmp (const char *p, const char *q)
67 size_t dmax = PTRDIFF_MAX;
68 size_t smax = SIZE_MAX;
70 T (strncmp (p, q, dmax));
72 T (strncmp (p, q, dmax + 1)); // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" "strncmp" }
73 T (strncmp (p, q, dmax * 2)); // { dg-warning "\\\[-Wstringop-overread" "strncmp" }
74 T (strncmp (p, q, smax)); // { dg-warning "\\\[-Wstringop-overread" "strncmp" }
77 NOIPA void test_strncat (char *p, const char *q)
79 size_t dmax = PTRDIFF_MAX;
80 size_t smax = SIZE_MAX;
82 T (strncat (p, q, dmax));
84 T (strncat (p, q, dmax + 1)); // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
85 T (strncat (p, q, dmax * 2)); // { dg-warning "\\\[-Wstringop-overread" }
86 T (strncat (p, q, smax)); // { dg-warning "\\\[-Wstringop-overread" }
89 NOIPA void test_strncpy (char *p, const char *q)
91 #if 0
92 /* Disabled: strncpy calls with an excissve bound trigger both
93 -Wstringop-overflow and, when the former option is disabled,
94 -Wstringop-overread. The latter should probably not trigger. */
96 size_t dmax = PTRDIFF_MAX;
97 size_t smax = SIZE_MAX;
99 T (strncpy (p, q, dmax));
101 T (strncpy (p, q, dmax + 1)); // -Wstringop-overflow disabled
102 T (strncpy (p, q, dmax * 2)); // ditto
103 T (strncpy (p, q, smax)); // ditto
104 #endif
107 NOIPA void test_strnlen (const char *p)
109 size_t dmax = PTRDIFF_MAX;
110 size_t smax = SIZE_MAX;
112 T (strnlen (p, dmax));
114 T (strnlen (p, dmax + 1)); // { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" }
115 T (strnlen (p, dmax * 2)); // { dg-warning "\\\[-Wstringop-overread" }
116 T (strnlen (p, smax)); // { dg-warning "\\\[-Wstringop-overread" }