comparison gcc/testsuite/gcc.dg/Wstringop-overflow-22.c @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
1 /* { dg-do compile }
2 { dg-options "-O2 -Wall -Wno-stringop-truncation -ftrack-macro-expansion=0" } */
3
4 #define NULL (void*)0
5
6 const char a[] = { 'a', 'b', 'c', 'd' };
7 const char b[] = { 'a', '\0', 'c', '\0', 'e' };
8
9 #define CONCAT(a, b) a ## b
10 #define CAT(a, b) CONCAT (a, b)
11
12 typedef struct FILE FILE;
13 extern FILE *fp;
14
15 extern char *d;
16 extern const char *s;
17 extern int n;
18
19 #define T(func, ...) \
20 __attribute__ ((noipa)) void \
21 CAT (test_ ## func, __LINE__) (void) \
22 { \
23 sink (0, __builtin_ ## func (__VA_ARGS__), d, s, n); \
24 } typedef void dummy_type
25
26 void sink (void*, ...);
27
28
29 // Exercise string functions.
30 T (index, a, 'x'); // { dg-warning "missing terminating nul" "index" }
31 T (index, a, *s); // { dg-warning "missing terminating nul" "index" }
32
33 T (index, b, '0');
34 T (index, b + 1, '1');
35 T (index, b + 2, '2');
36 T (index, b + 3, '3');
37 T (index, b + 4, '4'); // { dg-warning "missing terminating nul" "index" }
38
39 T (rindex, a, 'x'); // { dg-warning "missing terminating nul" "rindex" }
40 T (rindex, a, *s); // { dg-warning "missing terminating nul" "rindex" }
41
42 T (rindex, b, '0');
43 T (rindex, b + 1, '1');
44 T (rindex, b + 2, '2');
45 T (rindex, b + 3, '3');
46 T (rindex, b + 4, '4'); // { dg-warning "missing terminating nul" "rindex" }
47
48 T (stpcpy, d, a); // { dg-warning "missing terminating nul" "stpcpy" }
49
50 T (stpncpy, d, a, 4);
51 T (stpncpy, d, a, 5); // { dg-warning "missing terminating nul" "stpncpy" }
52 T (stpncpy, d, a, n);
53
54 T (stpncpy, d, a + n, 4);
55 T (stpncpy, d, a + n, 5); // { dg-warning "missing terminating nul" "stpncpy" }
56
57 T (stpncpy, d, b, 4);
58 T (stpncpy, d, b, 5);
59 T (stpncpy, d, b, n);
60
61 T (stpncpy, d, b + 1, 4);
62 T (stpncpy, d, b + 1, 5);
63 T (stpncpy, d, b + 1, n);
64
65 T (stpncpy, d, b + 3, 4);
66 T (stpncpy, d, b + 3, 5);
67 T (stpncpy, d, b + 3, n);
68
69 T (stpncpy, d, b + 4, 1);
70 T (stpncpy, d, b + 4, 2); // { dg-warning "missing terminating nul" "stpncpy" }
71 T (stpncpy, d, b + 4, n);
72 /* The following might be worth warning about since it's only safe with
73 n < 4. */
74 T (stpncpy, d, b + n, 5);
75
76 T (strcasecmp, a, "ab"); // { dg-warning "missing terminating nul" "strcasecmp" }
77 T (strcasecmp, a, s); // { dg-warning "missing terminating nul" "strcasecmp" }
78 T (strcasecmp, a, b); // { dg-warning "missing terminating nul" "strcasecmp" }
79 T (strcasecmp, b, b + 1);
80 T (strcasecmp, b, b + 2);
81 T (strcasecmp, b, b + 3);
82 T (strcasecmp, b, b + 4); // { dg-warning "missing terminating nul" "strcasecmp" }
83
84 T (strcat, d, a); // { dg-warning "missing terminating nul" "strcat" }
85
86 T (strncat, d, a, 4);
87 T (strncat, d, a, 5); // { dg-warning "missing terminating nul" "strncat" }
88 T (strncat, d, a, n);
89
90 T (strncat, d, b, n);
91 T (strncat, d, b + 1, n);
92 T (strncat, d, b + 2, n);
93 T (strncat, d, b + 3, n);
94 T (strncat, d, b + 4, 0);
95 T (strncat, d, b + 4, 1);
96 T (strncat, d, b + 4, 2); // { dg-warning "missing terminating nul" "strncat" }
97 /* The following should probably trigger a warning since it's only safe
98 when n < 2, makes little sense with n == 0, and not much more with
99 n == 1. */
100 T (strncat, d, b + 4, n); // { dg-warning "missing terminating nul" "strncat" { xfail *-*-* } }
101
102 T (strchr, a, 'x'); // { dg-warning "missing terminating nul" "strchr" }
103 T (strchr, a, *s); // { dg-warning "missing terminating nul" "strchr" }
104
105 T (strcmp, a, "ab"); // { dg-warning "missing terminating nul" "strcmp" }
106 T (strcmp, "bc", a); // { dg-warning "missing terminating nul" "strcmp" }
107 T (strcmp, a, s); // { dg-warning "missing terminating nul" "strcmp" }
108 T (strcmp, s, a); // { dg-warning "missing terminating nul" "strcmp" }
109
110 T (strcmp, a, b); // { dg-warning "missing terminating nul" "strcmp" }
111 /* Even though most likely safe in reality because b[1] is nul,
112 the following is strictly undefined because a is not a string.
113 The warning is not issued because GCC folds the call to (int)*a. */
114 T (strcmp, a, b + 1); // { dg-warning "missing terminating nul" "bug" { xfail *-*-* } }
115
116 T (strncmp, a, "ab", 4);
117 T (strncmp, "bc", a, 4);
118 T (strncmp, a, a, 4);
119 T (strncmp, a, s, 4);
120 T (strncmp, s, a, 4);
121
122 /* The warning below is not issued because GCC folds strncmp calls with
123 the same arguments to zero before it checks for the missing nul. */
124 T (strncmp, a, a, 5); // { dg-warning "missing terminating nul" "pr92624" { xfail *-*-*} }
125 T (strncmp, a, s, 5); // { dg-warning "missing terminating nul" "strcmp" }
126 T (strncmp, s, a, 5); // { dg-warning "missing terminating nul" "strcmp" }
127
128 T (strcpy, d, a); // { dg-warning "missing terminating nul" "strcpy" }
129
130 T (strcspn, a, s); // { dg-warning "missing terminating nul" "strcspn" }
131 T (strcspn, s, a); // { dg-warning "missing terminating nul" "strcspn" }
132
133 T (strspn, a, s); // { dg-warning "missing terminating nul" "strcspn" }
134 T (strspn, s, a); // { dg-warning "missing terminating nul" "strcspn" }
135
136 T (strdup, a); // { dg-warning "missing terminating nul" "strdup" }
137
138 T (strndup, a, 4);
139 T (strndup, a, 5); // { dg-warning "missing terminating nul" "strndup" }
140 T (strndup, b + 3, 2);
141 T (strndup, b + 4, 1);
142 T (strndup, b + 4, 2); // { dg-warning "missing terminating nul" "strndup" }
143
144 T (strlen, a); // { dg-warning "missing terminating nul" "strlen" }
145
146 T (strnlen, a, 4);
147 T (strnlen, a, 5); // { dg-warning "specified bound 5 exceeds the size 4 of unterminated array" "strnlen" }
148 T (strnlen, a, n);
149
150 T (strpbrk, s, a); // { dg-warning "missing terminating nul" "strpbrk" }
151
152 T (strrchr, a, 'x'); // { dg-warning "missing terminating nul" "strrchr" }
153 T (strrchr, a, *s); // { dg-warning "missing terminating nul" "strrchr" }
154
155 T (strstr, a, "cde"); // { dg-warning "missing terminating nul" "strstr" }
156 T (strstr, a, s); // { dg-warning "missing terminating nul" "strstr" }
157
158
159 // Exercise a few string checking functions.
160 T (__stpcpy_chk, d, a, -1); // { dg-warning "missing terminating nul" "stpcpy" }
161
162
163 T (__stpncpy_chk, d, a, 4, -1);
164 T (__stpncpy_chk, d, a, 5, -1); // { dg-warning "missing terminating nul" "stpncpy_chk" }
165 T (__stpncpy_chk, d, a, n, -1);
166
167 T (__stpncpy_chk, d, a + n, 4, -1);
168 T (__stpncpy_chk, d, a + n, 5, -1); // { dg-warning "missing terminating nul" "stpncpy_chk" }
169
170 T (__stpncpy_chk, d, b, 4, -1);
171 T (__stpncpy_chk, d, b, 5, -1);
172 T (__stpncpy_chk, d, b, n, -1);
173
174 T (__stpncpy_chk, d, b + 1, 4, -1);
175 T (__stpncpy_chk, d, b + 1, 5, -1);
176 T (__stpncpy_chk, d, b + 1, n, -1);
177
178 T (__stpncpy_chk, d, b + 3, 4, -1);
179 T (__stpncpy_chk, d, b + 3, 5, -1);
180 T (__stpncpy_chk, d, b + 3, n, -1);
181
182 T (__stpncpy_chk, d, b + 4, 1, -1);
183 T (__stpncpy_chk, d, b + 4, 2, -1); // { dg-warning "missing terminating nul" "stpncpy_chk" }
184 T (__stpncpy_chk, d, b + 4, n, -1);
185
186
187 T (__strncat_chk, d, a, 4, -1);
188 T (__strncat_chk, d, a, 5, -1); // { dg-warning "missing terminating nul" "strncat_chk" }
189 T (__strncat_chk, d, a, n, -1);
190
191 T (__strncat_chk, d, a + n, 4, -1);
192 T (__strncat_chk, d, a + n, 5, -1); // { dg-warning "missing terminating nul" "strncat_chk" }
193
194 T (__strncat_chk, d, b, 4, -1);
195 T (__strncat_chk, d, b, 5, -1);
196 T (__strncat_chk, d, b, n, -1);
197
198 T (__strncat_chk, d, b + 1, 4, -1);
199 T (__strncat_chk, d, b + 1, 5, -1);
200 T (__strncat_chk, d, b + 1, n, -1);
201
202 T (__strncat_chk, d, b + 3, 4, -1);
203 T (__strncat_chk, d, b + 3, 5, -1);
204 T (__strncat_chk, d, b + 3, n, -1);
205
206 T (__strncat_chk, d, b + 4, 1, -1);
207 T (__strncat_chk, d, b + 4, 2, -1); // { dg-warning "missing terminating nul" "strncat_chk" }
208 T (__strncat_chk, d, b + 4, n, -1);
209
210
211 T (__strncpy_chk, d, a, 4, -1);
212 T (__strncpy_chk, d, a, 5, -1); // { dg-warning "missing terminating nul" "strncpy_chk" }
213 T (__strncpy_chk, d, a, n, -1);
214
215 T (__strncpy_chk, d, a + n, 4, -1);
216 T (__strncpy_chk, d, a + n, 5, -1); // { dg-warning "missing terminating nul" "strncpy_chk" }
217
218 T (__strncpy_chk, d, b, 4, -1);
219 T (__strncpy_chk, d, b, 5, -1);
220 T (__strncpy_chk, d, b, n, -1);
221
222 T (__strncpy_chk, d, b + 1, 4, -1);
223 T (__strncpy_chk, d, b + 1, 5, -1);
224 T (__strncpy_chk, d, b + 1, n, -1);
225
226 T (__strncpy_chk, d, b + 3, 4, -1);
227 T (__strncpy_chk, d, b + 3, 5, -1);
228 T (__strncpy_chk, d, b + 3, n, -1);
229
230 T (__strncpy_chk, d, b + 4, 1, -1);
231 T (__strncpy_chk, d, b + 4, 2, -1); // { dg-warning "missing terminating nul" "strncpy" }
232 T (__strncpy_chk, d, b + 4, n, -1);
233
234
235 // Exercise some stdio functions.
236 T (printf, a); // { dg-warning "unterminated format string" "printf" }
237 T (printf, "%s", a); // { dg-warning "not a nul-terminated string" "printf" }
238 T (sprintf, d, "%s", a); // { dg-warning "not a nul-terminated string" "sprintf" }
239 T (snprintf, d, n, "%s", a); // { dg-warning "not a nul-terminated string" "sprintf" }
240
241 T (__sprintf_chk, d, 0, -1, "%s", a); // { dg-warning "not a nul-terminated string" "sprintf" }
242 T (__snprintf_chk, d, n, 0, -1, "%s", a); // { dg-warning "not a nul-terminated string" "sprintf" }
243
244 T (fputs, a, fp); // { dg-warning "missing terminating nul" "fputs" }
245 T (fputs_unlocked, a, fp); // { dg-warning "missing terminating nul" "fputs_unlocked" }
246 T (puts, a); // { dg-warning "missing terminating nul" "puts" }
247 T (puts_unlocked, a); // { dg-warning "missing terminating nul" "puts_unlocked" }
248
249
250
251 // Exerise exec functions.
252 T (execl, a, s, NULL); // { dg-warning "missing terminating nul" "execl" }
253 T (execl, a, s, NULL); // { dg-warning "missing terminating nul" "execl" }
254 T (execle, a, s, NULL, NULL); // { dg-warning "missing terminating nul" "execl" }
255 T (execlp, a, s, NULL); // { dg-warning "missing terminating nul" "execl" }
256
257 T (execv, a, &d); // { dg-warning "missing terminating nul" "execl" }
258 T (execve, a, &d, &d); // { dg-warning "missing terminating nul" "execl" }
259 T (execvp, a, &d); // { dg-warning "missing terminating nul" "execl" }
260
261 T (gettext, a); // { dg-warning "missing terminating nul" "gettext" }
262
263 T (strfmon, d, n, a); // { dg-warning "unterminated format string" "strfmon" }