]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 2004-2024 Free Software Foundation, Inc. |
fcfc9086 | 2 | Copyright The GNU Toolchain Authors. |
b5cc329c | 3 | This file is part of the GNU C Library. |
b5cc329c UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
b5cc329c | 18 | |
7caa5054 ZW |
19 | /* This file tests gets. Force it to be declared. */ |
20 | #include <features.h> | |
21 | #undef __GLIBC_USE_DEPRECATED_GETS | |
22 | #define __GLIBC_USE_DEPRECATED_GETS 1 | |
23 | ||
f9a906e7 | 24 | #include <assert.h> |
a9055cab | 25 | #include <fcntl.h> |
9556acd2 | 26 | #include <limits.h> |
b799f91d | 27 | #include <locale.h> |
dff37515 | 28 | #include <obstack.h> |
b5cc329c UD |
29 | #include <setjmp.h> |
30 | #include <signal.h> | |
31 | #include <stdio.h> | |
32 | #include <stdlib.h> | |
33 | #include <string.h> | |
8215c9ec UD |
34 | #include <unistd.h> |
35 | #include <wchar.h> | |
d9a216c0 | 36 | #include <sys/poll.h> |
a0f33f99 | 37 | #include <sys/select.h> |
88764ae2 UD |
38 | #include <sys/socket.h> |
39 | #include <sys/un.h> | |
9556acd2 AZ |
40 | #include <paths.h> |
41 | ||
42 | #include <support/temp_file.h> | |
43 | #include <support/support.h> | |
b5cc329c | 44 | |
fcfc9086 SP |
45 | #ifndef _GNU_SOURCE |
46 | # define MEMPCPY memcpy | |
47 | # define WMEMPCPY wmemcpy | |
48 | # define MEMPCPY_RET(x) 0 | |
49 | # define WMEMPCPY_RET(x) 0 | |
50 | #else | |
51 | # define MEMPCPY mempcpy | |
52 | # define WMEMPCPY wmempcpy | |
53 | # define MEMPCPY_RET(x) __builtin_strlen (x) | |
54 | # define WMEMPCPY_RET(x) wcslen (x) | |
55 | #endif | |
8ea79a61 | 56 | |
dff37515 UD |
57 | #define obstack_chunk_alloc malloc |
58 | #define obstack_chunk_free free | |
59 | ||
9556acd2 | 60 | static char *temp_filename; |
b5cc329c | 61 | |
121aad59 AZ |
62 | static int temp_fd_dprintf; |
63 | ||
b5cc329c | 64 | static void |
9556acd2 | 65 | do_prepare (int argc, char *argv[]) |
b5cc329c UD |
66 | { |
67 | int temp_fd = create_temp_file ("tst-chk1.", &temp_filename); | |
68 | if (temp_fd == -1) | |
69 | { | |
70 | printf ("cannot create temporary file: %m\n"); | |
71 | exit (1); | |
72 | } | |
73 | ||
74 | const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ"; | |
de1c3ebb | 75 | if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs)) |
b5cc329c UD |
76 | { |
77 | puts ("could not write test strings into file"); | |
78 | unlink (temp_filename); | |
79 | exit (1); | |
80 | } | |
121aad59 AZ |
81 | |
82 | temp_fd_dprintf = create_temp_file ("tst-chk2.", NULL); | |
83 | if (temp_fd_dprintf == -1) | |
84 | { | |
85 | printf ("cannot create temporary file: %m\n"); | |
86 | exit (1); | |
87 | } | |
b5cc329c | 88 | } |
9556acd2 | 89 | #define PREPARE do_prepare |
b5cc329c | 90 | |
9556acd2 AZ |
91 | static volatile int chk_fail_ok; |
92 | static volatile int ret; | |
93 | static jmp_buf chk_fail_buf; | |
b5cc329c UD |
94 | |
95 | static void | |
96 | handler (int sig) | |
97 | { | |
98 | if (chk_fail_ok) | |
99 | { | |
100 | chk_fail_ok = 0; | |
101 | longjmp (chk_fail_buf, 1); | |
102 | } | |
103 | else | |
104 | _exit (127); | |
105 | } | |
106 | ||
ad6f2a01 SP |
107 | #if __USE_FORTIFY_LEVEL == 3 |
108 | volatile size_t buf_size = 10; | |
109 | #else | |
b5cc329c | 110 | char buf[10]; |
8215c9ec | 111 | wchar_t wbuf[10]; |
ad6f2a01 SP |
112 | #define buf_size sizeof (buf) |
113 | #endif | |
114 | ||
9556acd2 AZ |
115 | static volatile size_t l0; |
116 | static volatile char *p; | |
117 | static volatile wchar_t *wp; | |
118 | static const char *str1 = "JIHGFEDCBA"; | |
119 | static const char *str2 = "F"; | |
120 | static const char *str3 = "%s%n%s%n"; | |
121 | static const char *str4 = "Hello, "; | |
122 | static const char *str5 = "World!\n"; | |
123 | static const wchar_t *wstr1 = L"JIHGFEDCBA"; | |
124 | static const wchar_t *wstr2 = L"F"; | |
125 | static const wchar_t *wstr3 = L"%s%n%s%n"; | |
126 | static const wchar_t *wstr4 = L"Hello, "; | |
127 | static const wchar_t *wstr5 = L"World!\n"; | |
128 | static char buf2[10] = "%s"; | |
129 | static int num1 = 67; | |
130 | static int num2 = 987654; | |
b5cc329c UD |
131 | |
132 | #define FAIL() \ | |
133 | do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0) | |
134 | #define CHK_FAIL_START \ | |
135 | chk_fail_ok = 1; \ | |
136 | if (! setjmp (chk_fail_buf)) \ | |
137 | { | |
138 | #define CHK_FAIL_END \ | |
139 | chk_fail_ok = 0; \ | |
140 | FAIL (); \ | |
b5cc329c | 141 | } |
bf320000 | 142 | #if __USE_FORTIFY_LEVEL >= 2 |
a0f33f99 UD |
143 | # define CHK_FAIL2_START CHK_FAIL_START |
144 | # define CHK_FAIL2_END CHK_FAIL_END | |
b5cc329c | 145 | #else |
a0f33f99 UD |
146 | # define CHK_FAIL2_START |
147 | # define CHK_FAIL2_END | |
b5cc329c UD |
148 | #endif |
149 | ||
150 | static int | |
151 | do_test (void) | |
152 | { | |
ad6f2a01 SP |
153 | #if __USE_FORTIFY_LEVEL == 3 |
154 | char *buf = (char *) malloc (buf_size); | |
155 | wchar_t *wbuf = (wchar_t *) malloc (buf_size * sizeof (wchar_t)); | |
156 | #endif | |
c5bb8e23 | 157 | set_fortify_handler (handler); |
a9055cab | 158 | |
b5cc329c | 159 | struct A { char buf1[9]; char buf2[1]; } a; |
8215c9ec | 160 | struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa; |
b5cc329c UD |
161 | |
162 | printf ("Test checking routines at fortify level %d\n", | |
163 | #ifdef __USE_FORTIFY_LEVEL | |
164 | (int) __USE_FORTIFY_LEVEL | |
165 | #else | |
166 | 0 | |
167 | #endif | |
168 | ); | |
169 | ||
5ac3ea17 | 170 | #if defined __USE_FORTIFY_LEVEL && !defined __fortify_function |
de1c3ebb UD |
171 | printf ("Test skipped"); |
172 | if (l0 == 0) | |
173 | return 0; | |
174 | #endif | |
175 | ||
b5cc329c UD |
176 | /* These ops can be done without runtime checking of object size. */ |
177 | memcpy (buf, "abcdefghij", 10); | |
178 | memmove (buf + 1, buf, 9); | |
179 | if (memcmp (buf, "aabcdefghi", 10)) | |
180 | FAIL (); | |
181 | ||
d3bf0bad ZW |
182 | memcpy (buf, "abcdefghij", 10); |
183 | bcopy (buf, buf + 1, 9); | |
184 | if (memcmp (buf, "aabcdefghi", 10)) | |
185 | FAIL (); | |
186 | ||
fcfc9086 | 187 | if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde") |
8215c9ec | 188 | || memcmp (buf, "aabcdabcde", 10)) |
b5cc329c UD |
189 | FAIL (); |
190 | ||
191 | memset (buf + 8, 'j', 2); | |
192 | if (memcmp (buf, "aabcdabcjj", 10)) | |
193 | FAIL (); | |
194 | ||
d3bf0bad ZW |
195 | bzero (buf + 8, 2); |
196 | if (memcmp (buf, "aabcdabc\0\0", 10)) | |
197 | FAIL (); | |
198 | ||
ea1bd74d ZW |
199 | explicit_bzero (buf + 6, 4); |
200 | if (memcmp (buf, "aabcda\0\0\0\0", 10)) | |
201 | FAIL (); | |
202 | ||
b5cc329c UD |
203 | strcpy (buf + 4, "EDCBA"); |
204 | if (memcmp (buf, "aabcEDCBA", 10)) | |
205 | FAIL (); | |
206 | ||
207 | if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10)) | |
208 | FAIL (); | |
209 | ||
210 | strncpy (buf + 6, "X", 4); | |
211 | if (memcmp (buf, "aabcEDX\0\0", 10)) | |
212 | FAIL (); | |
213 | ||
214 | if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10)) | |
215 | FAIL (); | |
216 | ||
217 | if (snprintf (buf + 7, 3, "%s", "987654") != 6 | |
218 | || memcmp (buf, "aabcEDX98", 10)) | |
219 | FAIL (); | |
220 | ||
221 | /* These ops need runtime checking, but shouldn't __chk_fail. */ | |
222 | memcpy (buf, "abcdefghij", l0 + 10); | |
223 | memmove (buf + 1, buf, l0 + 9); | |
224 | if (memcmp (buf, "aabcdefghi", 10)) | |
225 | FAIL (); | |
226 | ||
d3bf0bad ZW |
227 | memcpy (buf, "abcdefghij", l0 + 10); |
228 | bcopy (buf, buf + 1, l0 + 9); | |
229 | if (memcmp (buf, "aabcdefghi", 10)) | |
230 | FAIL (); | |
231 | ||
fcfc9086 | 232 | if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde") |
8215c9ec | 233 | || memcmp (buf, "aabcdabcde", 10)) |
b5cc329c UD |
234 | FAIL (); |
235 | ||
236 | memset (buf + 8, 'j', l0 + 2); | |
237 | if (memcmp (buf, "aabcdabcjj", 10)) | |
238 | FAIL (); | |
239 | ||
d3bf0bad ZW |
240 | bzero (buf + 8, l0 + 2); |
241 | if (memcmp (buf, "aabcdabc\0\0", 10)) | |
242 | FAIL (); | |
243 | ||
ea1bd74d ZW |
244 | explicit_bzero (buf + 6, l0 + 4); |
245 | if (memcmp (buf, "aabcda\0\0\0\0", 10)) | |
246 | FAIL (); | |
247 | ||
b5cc329c UD |
248 | strcpy (buf + 4, str1 + 5); |
249 | if (memcmp (buf, "aabcEDCBA", 10)) | |
250 | FAIL (); | |
251 | ||
252 | if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10)) | |
253 | FAIL (); | |
254 | ||
255 | strncpy (buf + 6, "X", l0 + 4); | |
256 | if (memcmp (buf, "aabcEDX\0\0", 10)) | |
257 | FAIL (); | |
258 | ||
8215c9ec UD |
259 | if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7 |
260 | || memcmp (buf, "aabcEcd\0\0", 10)) | |
b5cc329c UD |
261 | FAIL (); |
262 | ||
8215c9ec UD |
263 | if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10)) |
264 | FAIL (); | |
265 | ||
266 | if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10)) | |
b5cc329c UD |
267 | FAIL (); |
268 | ||
269 | buf[l0 + 8] = '\0'; | |
270 | strcat (buf, "A"); | |
8215c9ec | 271 | if (memcmp (buf, "aabcEcd9A", 10)) |
b5cc329c UD |
272 | FAIL (); |
273 | ||
274 | buf[l0 + 7] = '\0'; | |
275 | strncat (buf, "ZYXWV", l0 + 2); | |
8215c9ec | 276 | if (memcmp (buf, "aabcEcdZY", 10)) |
b5cc329c UD |
277 | FAIL (); |
278 | ||
d3bf0bad ZW |
279 | /* The following tests are supposed to succeed at all fortify |
280 | levels, even though they overflow a.buf1 into a.buf2. */ | |
b5cc329c UD |
281 | memcpy (a.buf1, "abcdefghij", l0 + 10); |
282 | memmove (a.buf1 + 1, a.buf1, l0 + 9); | |
283 | if (memcmp (a.buf1, "aabcdefghi", 10)) | |
284 | FAIL (); | |
285 | ||
d3bf0bad ZW |
286 | memcpy (a.buf1, "abcdefghij", l0 + 10); |
287 | bcopy (a.buf1, a.buf1 + 1, l0 + 9); | |
288 | if (memcmp (a.buf1, "aabcdefghi", 10)) | |
289 | FAIL (); | |
290 | ||
fcfc9086 SP |
291 | if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5) |
292 | != a.buf1 + 5 + MEMPCPY_RET ("abcde") | |
b5cc329c UD |
293 | || memcmp (a.buf1, "aabcdabcde", 10)) |
294 | FAIL (); | |
295 | ||
296 | memset (a.buf1 + 8, 'j', l0 + 2); | |
297 | if (memcmp (a.buf1, "aabcdabcjj", 10)) | |
298 | FAIL (); | |
299 | ||
d3bf0bad ZW |
300 | bzero (a.buf1 + 8, l0 + 2); |
301 | if (memcmp (a.buf1, "aabcdabc\0\0", 10)) | |
302 | FAIL (); | |
303 | ||
ea1bd74d ZW |
304 | explicit_bzero (a.buf1 + 6, l0 + 4); |
305 | if (memcmp (a.buf1, "aabcda\0\0\0\0", 10)) | |
306 | FAIL (); | |
307 | ||
a334319f | 308 | #if __USE_FORTIFY_LEVEL < 2 |
b5cc329c UD |
309 | /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2 |
310 | and sufficient GCC support, as the string operations overflow | |
311 | from a.buf1 into a.buf2. */ | |
312 | strcpy (a.buf1 + 4, str1 + 5); | |
313 | if (memcmp (a.buf1, "aabcEDCBA", 10)) | |
314 | FAIL (); | |
315 | ||
8215c9ec UD |
316 | if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9 |
317 | || memcmp (a.buf1, "aabcEDCBF", 10)) | |
b5cc329c UD |
318 | FAIL (); |
319 | ||
320 | strncpy (a.buf1 + 6, "X", l0 + 4); | |
321 | if (memcmp (a.buf1, "aabcEDX\0\0", 10)) | |
322 | FAIL (); | |
323 | ||
8215c9ec UD |
324 | if (sprintf (a.buf1 + 7, "%d", num1) != 2 |
325 | || memcmp (a.buf1, "aabcEDX67", 10)) | |
b5cc329c UD |
326 | FAIL (); |
327 | ||
328 | if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6 | |
329 | || memcmp (a.buf1, "aabcEDX98", 10)) | |
330 | FAIL (); | |
331 | ||
332 | a.buf1[l0 + 8] = '\0'; | |
333 | strcat (a.buf1, "A"); | |
334 | if (memcmp (a.buf1, "aabcEDX9A", 10)) | |
335 | FAIL (); | |
336 | ||
337 | a.buf1[l0 + 7] = '\0'; | |
338 | strncat (a.buf1, "ZYXWV", l0 + 2); | |
339 | if (memcmp (a.buf1, "aabcEDXZY", 10)) | |
340 | FAIL (); | |
341 | ||
342 | #endif | |
343 | ||
344 | #if __USE_FORTIFY_LEVEL >= 1 | |
8ff5e0ec ZW |
345 | /* Now check if all buffer overflows are caught at runtime. |
346 | N.B. All tests involving a length parameter need to be done | |
347 | twice: once with the length a compile-time constant, once without. */ | |
348 | ||
349 | CHK_FAIL_START | |
350 | memcpy (buf + 1, "abcdefghij", 10); | |
351 | CHK_FAIL_END | |
b5cc329c UD |
352 | |
353 | CHK_FAIL_START | |
354 | memcpy (buf + 1, "abcdefghij", l0 + 10); | |
355 | CHK_FAIL_END | |
356 | ||
8ff5e0ec ZW |
357 | CHK_FAIL_START |
358 | memmove (buf + 2, buf + 1, 9); | |
359 | CHK_FAIL_END | |
360 | ||
b5cc329c UD |
361 | CHK_FAIL_START |
362 | memmove (buf + 2, buf + 1, l0 + 9); | |
363 | CHK_FAIL_END | |
364 | ||
d3bf0bad ZW |
365 | CHK_FAIL_START |
366 | bcopy (buf + 1, buf + 2, 9); | |
367 | CHK_FAIL_END | |
368 | ||
369 | CHK_FAIL_START | |
370 | bcopy (buf + 1, buf + 2, l0 + 9); | |
371 | CHK_FAIL_END | |
372 | ||
fcfc9086 | 373 | #ifdef _GNU_SOURCE |
8ff5e0ec ZW |
374 | CHK_FAIL_START |
375 | p = (char *) mempcpy (buf + 6, "abcde", 5); | |
376 | CHK_FAIL_END | |
377 | ||
b5cc329c | 378 | CHK_FAIL_START |
de1c3ebb | 379 | p = (char *) mempcpy (buf + 6, "abcde", l0 + 5); |
b5cc329c | 380 | CHK_FAIL_END |
fcfc9086 | 381 | #endif |
b5cc329c | 382 | |
8ff5e0ec ZW |
383 | CHK_FAIL_START |
384 | memset (buf + 9, 'j', 2); | |
385 | CHK_FAIL_END | |
386 | ||
b5cc329c UD |
387 | CHK_FAIL_START |
388 | memset (buf + 9, 'j', l0 + 2); | |
389 | CHK_FAIL_END | |
390 | ||
d3bf0bad ZW |
391 | CHK_FAIL_START |
392 | bzero (buf + 9, 2); | |
393 | CHK_FAIL_END | |
394 | ||
395 | CHK_FAIL_START | |
396 | bzero (buf + 9, l0 + 2); | |
397 | CHK_FAIL_END | |
398 | ||
ea1bd74d ZW |
399 | CHK_FAIL_START |
400 | explicit_bzero (buf + 9, 2); | |
401 | CHK_FAIL_END | |
402 | ||
403 | CHK_FAIL_START | |
404 | explicit_bzero (buf + 9, l0 + 2); | |
405 | CHK_FAIL_END | |
406 | ||
b5cc329c UD |
407 | CHK_FAIL_START |
408 | strcpy (buf + 5, str1 + 5); | |
409 | CHK_FAIL_END | |
410 | ||
411 | CHK_FAIL_START | |
412 | p = stpcpy (buf + 9, str2); | |
413 | CHK_FAIL_END | |
414 | ||
8ff5e0ec ZW |
415 | CHK_FAIL_START |
416 | strncpy (buf + 7, "X", 4); | |
417 | CHK_FAIL_END | |
418 | ||
b5cc329c UD |
419 | CHK_FAIL_START |
420 | strncpy (buf + 7, "X", l0 + 4); | |
421 | CHK_FAIL_END | |
422 | ||
8ff5e0ec ZW |
423 | CHK_FAIL_START |
424 | stpncpy (buf + 6, "cd", 5); | |
425 | CHK_FAIL_END | |
426 | ||
8215c9ec UD |
427 | CHK_FAIL_START |
428 | stpncpy (buf + 6, "cd", l0 + 5); | |
429 | CHK_FAIL_END | |
430 | ||
b5cc329c UD |
431 | CHK_FAIL_START |
432 | sprintf (buf + 8, "%d", num1); | |
433 | CHK_FAIL_END | |
434 | ||
8ff5e0ec ZW |
435 | CHK_FAIL_START |
436 | snprintf (buf + 8, 3, "%d", num2); | |
437 | CHK_FAIL_END | |
438 | ||
b5cc329c UD |
439 | CHK_FAIL_START |
440 | snprintf (buf + 8, l0 + 3, "%d", num2); | |
441 | CHK_FAIL_END | |
d6cd6bf4 UD |
442 | |
443 | CHK_FAIL_START | |
444 | swprintf (wbuf + 8, 3, L"%d", num1); | |
445 | CHK_FAIL_END | |
446 | ||
447 | CHK_FAIL_START | |
448 | swprintf (wbuf + 8, l0 + 3, L"%d", num1); | |
449 | CHK_FAIL_END | |
b5cc329c | 450 | |
8ff5e0ec | 451 | memcpy (buf, str1 + 2, 9); |
b5cc329c UD |
452 | CHK_FAIL_START |
453 | strcat (buf, "AB"); | |
454 | CHK_FAIL_END | |
455 | ||
8ff5e0ec ZW |
456 | memcpy (buf, str1 + 3, 8); |
457 | CHK_FAIL_START | |
458 | strncat (buf, "ZYXWV", 3); | |
459 | CHK_FAIL_END | |
460 | ||
461 | memcpy (buf, str1 + 3, 8); | |
b5cc329c UD |
462 | CHK_FAIL_START |
463 | strncat (buf, "ZYXWV", l0 + 3); | |
464 | CHK_FAIL_END | |
465 | ||
8ff5e0ec ZW |
466 | CHK_FAIL_START |
467 | memcpy (a.buf1 + 1, "abcdefghij", 10); | |
468 | CHK_FAIL_END | |
469 | ||
b5cc329c UD |
470 | CHK_FAIL_START |
471 | memcpy (a.buf1 + 1, "abcdefghij", l0 + 10); | |
472 | CHK_FAIL_END | |
473 | ||
8ff5e0ec ZW |
474 | CHK_FAIL_START |
475 | memmove (a.buf1 + 2, a.buf1 + 1, 9); | |
476 | CHK_FAIL_END | |
477 | ||
b5cc329c UD |
478 | CHK_FAIL_START |
479 | memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9); | |
480 | CHK_FAIL_END | |
481 | ||
d3bf0bad ZW |
482 | CHK_FAIL_START |
483 | bcopy (a.buf1 + 1, a.buf1 + 2, 9); | |
484 | CHK_FAIL_END | |
485 | ||
486 | CHK_FAIL_START | |
487 | bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9); | |
488 | CHK_FAIL_END | |
489 | ||
fcfc9086 | 490 | #ifdef _GNU_SOURCE |
8ff5e0ec ZW |
491 | CHK_FAIL_START |
492 | p = (char *) mempcpy (a.buf1 + 6, "abcde", 5); | |
493 | CHK_FAIL_END | |
494 | ||
b5cc329c | 495 | CHK_FAIL_START |
de1c3ebb | 496 | p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5); |
b5cc329c | 497 | CHK_FAIL_END |
fcfc9086 | 498 | #endif |
b5cc329c | 499 | |
8ff5e0ec ZW |
500 | CHK_FAIL_START |
501 | memset (a.buf1 + 9, 'j', 2); | |
502 | CHK_FAIL_END | |
503 | ||
b5cc329c UD |
504 | CHK_FAIL_START |
505 | memset (a.buf1 + 9, 'j', l0 + 2); | |
506 | CHK_FAIL_END | |
507 | ||
d3bf0bad ZW |
508 | CHK_FAIL_START |
509 | bzero (a.buf1 + 9, 2); | |
510 | CHK_FAIL_END | |
511 | ||
512 | CHK_FAIL_START | |
513 | bzero (a.buf1 + 9, l0 + 2); | |
514 | CHK_FAIL_END | |
515 | ||
ea1bd74d ZW |
516 | CHK_FAIL_START |
517 | explicit_bzero (a.buf1 + 9, 2); | |
518 | CHK_FAIL_END | |
519 | ||
520 | CHK_FAIL_START | |
521 | explicit_bzero (a.buf1 + 9, l0 + 2); | |
522 | CHK_FAIL_END | |
523 | ||
de1c3ebb UD |
524 | # if __USE_FORTIFY_LEVEL >= 2 |
525 | # define O 0 | |
526 | # else | |
527 | # define O 1 | |
528 | # endif | |
b5cc329c UD |
529 | |
530 | CHK_FAIL_START | |
531 | strcpy (a.buf1 + (O + 4), str1 + 5); | |
532 | CHK_FAIL_END | |
533 | ||
534 | CHK_FAIL_START | |
535 | p = stpcpy (a.buf1 + (O + 8), str2); | |
536 | CHK_FAIL_END | |
537 | ||
8ff5e0ec ZW |
538 | CHK_FAIL_START |
539 | strncpy (a.buf1 + (O + 6), "X", 4); | |
540 | CHK_FAIL_END | |
541 | ||
b5cc329c UD |
542 | CHK_FAIL_START |
543 | strncpy (a.buf1 + (O + 6), "X", l0 + 4); | |
544 | CHK_FAIL_END | |
545 | ||
454a20c8 FW |
546 | CHK_FAIL_START |
547 | strlcpy (a.buf1 + (O + 6), "X", 4); | |
548 | CHK_FAIL_END | |
549 | ||
550 | CHK_FAIL_START | |
551 | strlcpy (a.buf1 + (O + 6), "X", l0 + 4); | |
552 | CHK_FAIL_END | |
553 | ||
554 | { | |
555 | char *volatile buf2 = buf; | |
556 | if (strlcpy (buf2, "a", sizeof (buf) + 1) != 1) | |
557 | FAIL (); | |
558 | } | |
559 | ||
b5cc329c UD |
560 | CHK_FAIL_START |
561 | sprintf (a.buf1 + (O + 7), "%d", num1); | |
562 | CHK_FAIL_END | |
563 | ||
8ff5e0ec ZW |
564 | CHK_FAIL_START |
565 | snprintf (a.buf1 + (O + 7), 3, "%d", num2); | |
566 | CHK_FAIL_END | |
567 | ||
b5cc329c UD |
568 | CHK_FAIL_START |
569 | snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2); | |
570 | CHK_FAIL_END | |
571 | ||
8ff5e0ec | 572 | memcpy (a.buf1, str1 + (3 - O), 8 + O); |
b5cc329c UD |
573 | CHK_FAIL_START |
574 | strcat (a.buf1, "AB"); | |
575 | CHK_FAIL_END | |
576 | ||
8ff5e0ec | 577 | memcpy (a.buf1, str1 + (4 - O), 7 + O); |
b5cc329c UD |
578 | CHK_FAIL_START |
579 | strncat (a.buf1, "ZYXWV", l0 + 3); | |
580 | CHK_FAIL_END | |
454a20c8 FW |
581 | |
582 | memset (a.buf1, 0, sizeof (a.buf1)); | |
583 | CHK_FAIL_START | |
584 | strlcat (a.buf1 + (O + 6), "X", 4); | |
585 | CHK_FAIL_END | |
586 | ||
587 | memset (a.buf1, 0, sizeof (a.buf1)); | |
588 | CHK_FAIL_START | |
589 | strlcat (a.buf1 + (O + 6), "X", l0 + 4); | |
590 | CHK_FAIL_END | |
591 | ||
592 | { | |
593 | buf[0] = '\0'; | |
594 | char *volatile buf2 = buf; | |
595 | if (strlcat (buf2, "a", sizeof (buf) + 1) != 1) | |
596 | FAIL (); | |
597 | } | |
b5cc329c UD |
598 | #endif |
599 | ||
8215c9ec UD |
600 | |
601 | /* These ops can be done without runtime checking of object size. */ | |
602 | wmemcpy (wbuf, L"abcdefghij", 10); | |
603 | wmemmove (wbuf + 1, wbuf, 9); | |
604 | if (wmemcmp (wbuf, L"aabcdefghi", 10)) | |
605 | FAIL (); | |
606 | ||
fcfc9086 | 607 | if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde") |
8215c9ec UD |
608 | || wmemcmp (wbuf, L"aabcdabcde", 10)) |
609 | FAIL (); | |
610 | ||
611 | wmemset (wbuf + 8, L'j', 2); | |
612 | if (wmemcmp (wbuf, L"aabcdabcjj", 10)) | |
613 | FAIL (); | |
614 | ||
615 | wcscpy (wbuf + 4, L"EDCBA"); | |
616 | if (wmemcmp (wbuf, L"aabcEDCBA", 10)) | |
617 | FAIL (); | |
618 | ||
619 | if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10)) | |
620 | FAIL (); | |
621 | ||
622 | wcsncpy (wbuf + 6, L"X", 4); | |
623 | if (wmemcmp (wbuf, L"aabcEDX\0\0", 10)) | |
624 | FAIL (); | |
625 | ||
626 | if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0 | |
627 | || wmemcmp (wbuf, L"aabcEDX98", 10)) | |
628 | FAIL (); | |
629 | ||
757beee1 UD |
630 | if (swprintf (wbuf + 7, 3, L"64") != 2 |
631 | || wmemcmp (wbuf, L"aabcEDX64", 10)) | |
632 | FAIL (); | |
633 | ||
8215c9ec UD |
634 | /* These ops need runtime checking, but shouldn't __chk_fail. */ |
635 | wmemcpy (wbuf, L"abcdefghij", l0 + 10); | |
636 | wmemmove (wbuf + 1, wbuf, l0 + 9); | |
637 | if (wmemcmp (wbuf, L"aabcdefghi", 10)) | |
638 | FAIL (); | |
639 | ||
fcfc9086 SP |
640 | if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5) |
641 | != wbuf + 5 + WMEMPCPY_RET (L"abcde") | |
8215c9ec UD |
642 | || wmemcmp (wbuf, L"aabcdabcde", 10)) |
643 | FAIL (); | |
644 | ||
645 | wmemset (wbuf + 8, L'j', l0 + 2); | |
646 | if (wmemcmp (wbuf, L"aabcdabcjj", 10)) | |
647 | FAIL (); | |
648 | ||
649 | wcscpy (wbuf + 4, wstr1 + 5); | |
650 | if (wmemcmp (wbuf, L"aabcEDCBA", 10)) | |
651 | FAIL (); | |
652 | ||
653 | if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10)) | |
654 | FAIL (); | |
655 | ||
656 | wcsncpy (wbuf + 6, L"X", l0 + 4); | |
657 | if (wmemcmp (wbuf, L"aabcEDX\0\0", 10)) | |
658 | FAIL (); | |
659 | ||
660 | if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7 | |
661 | || wmemcmp (wbuf, L"aabcEcd\0\0", 10)) | |
662 | FAIL (); | |
663 | ||
664 | if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0 | |
665 | || wmemcmp (wbuf, L"aabcEcd98", 10)) | |
666 | FAIL (); | |
667 | ||
668 | wbuf[l0 + 8] = L'\0'; | |
669 | wcscat (wbuf, L"A"); | |
670 | if (wmemcmp (wbuf, L"aabcEcd9A", 10)) | |
671 | FAIL (); | |
672 | ||
673 | wbuf[l0 + 7] = L'\0'; | |
674 | wcsncat (wbuf, L"ZYXWV", l0 + 2); | |
675 | if (wmemcmp (wbuf, L"aabcEcdZY", 10)) | |
676 | FAIL (); | |
677 | ||
678 | wmemcpy (wa.buf1, L"abcdefghij", l0 + 10); | |
679 | wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9); | |
680 | if (wmemcmp (wa.buf1, L"aabcdefghi", 10)) | |
681 | FAIL (); | |
682 | ||
fcfc9086 SP |
683 | if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5) |
684 | != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde") | |
8215c9ec UD |
685 | || wmemcmp (wa.buf1, L"aabcdabcde", 10)) |
686 | FAIL (); | |
687 | ||
688 | wmemset (wa.buf1 + 8, L'j', l0 + 2); | |
689 | if (wmemcmp (wa.buf1, L"aabcdabcjj", 10)) | |
690 | FAIL (); | |
691 | ||
692 | #if __USE_FORTIFY_LEVEL < 2 | |
693 | /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2 | |
694 | and sufficient GCC support, as the string operations overflow | |
695 | from a.buf1 into a.buf2. */ | |
696 | wcscpy (wa.buf1 + 4, wstr1 + 5); | |
697 | if (wmemcmp (wa.buf1, L"aabcEDCBA", 10)) | |
698 | FAIL (); | |
699 | ||
700 | if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9 | |
701 | || wmemcmp (wa.buf1, L"aabcEDCBF", 10)) | |
702 | FAIL (); | |
703 | ||
704 | wcsncpy (wa.buf1 + 6, L"X", l0 + 4); | |
705 | if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10)) | |
706 | FAIL (); | |
707 | ||
708 | if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0 | |
709 | || wmemcmp (wa.buf1, L"aabcEDX98", 10)) | |
710 | FAIL (); | |
711 | ||
712 | wa.buf1[l0 + 8] = L'\0'; | |
713 | wcscat (wa.buf1, L"A"); | |
714 | if (wmemcmp (wa.buf1, L"aabcEDX9A", 10)) | |
715 | FAIL (); | |
716 | ||
717 | wa.buf1[l0 + 7] = L'\0'; | |
718 | wcsncat (wa.buf1, L"ZYXWV", l0 + 2); | |
719 | if (wmemcmp (wa.buf1, L"aabcEDXZY", 10)) | |
720 | FAIL (); | |
721 | ||
722 | #endif | |
723 | ||
724 | #if __USE_FORTIFY_LEVEL >= 1 | |
8ff5e0ec ZW |
725 | /* Now check if all buffer overflows are caught at runtime. |
726 | N.B. All tests involving a length parameter need to be done | |
727 | twice: once with the length a compile-time constant, once without. */ | |
728 | ||
729 | CHK_FAIL_START | |
730 | wmemcpy (wbuf + 1, L"abcdefghij", 10); | |
731 | CHK_FAIL_END | |
8215c9ec UD |
732 | |
733 | CHK_FAIL_START | |
734 | wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10); | |
735 | CHK_FAIL_END | |
736 | ||
8ff5e0ec ZW |
737 | CHK_FAIL_START |
738 | wmemcpy (wbuf + 9, L"abcdefghij", 10); | |
739 | CHK_FAIL_END | |
740 | ||
d6cd6bf4 UD |
741 | CHK_FAIL_START |
742 | wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10); | |
743 | CHK_FAIL_END | |
744 | ||
8ff5e0ec ZW |
745 | CHK_FAIL_START |
746 | wmemmove (wbuf + 2, wbuf + 1, 9); | |
747 | CHK_FAIL_END | |
748 | ||
8215c9ec UD |
749 | CHK_FAIL_START |
750 | wmemmove (wbuf + 2, wbuf + 1, l0 + 9); | |
751 | CHK_FAIL_END | |
752 | ||
fcfc9086 | 753 | #ifdef _GNU_SOURCE |
8ff5e0ec ZW |
754 | CHK_FAIL_START |
755 | wp = wmempcpy (wbuf + 6, L"abcde", 5); | |
756 | CHK_FAIL_END | |
757 | ||
8215c9ec | 758 | CHK_FAIL_START |
d6cd6bf4 | 759 | wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5); |
8215c9ec | 760 | CHK_FAIL_END |
fcfc9086 | 761 | #endif |
8215c9ec | 762 | |
8ff5e0ec ZW |
763 | CHK_FAIL_START |
764 | wmemset (wbuf + 9, L'j', 2); | |
765 | CHK_FAIL_END | |
766 | ||
8215c9ec UD |
767 | CHK_FAIL_START |
768 | wmemset (wbuf + 9, L'j', l0 + 2); | |
769 | CHK_FAIL_END | |
770 | ||
771 | CHK_FAIL_START | |
772 | wcscpy (wbuf + 5, wstr1 + 5); | |
773 | CHK_FAIL_END | |
774 | ||
775 | CHK_FAIL_START | |
df6f8969 | 776 | wp = wcpcpy (wbuf + 9, wstr2); |
8215c9ec UD |
777 | CHK_FAIL_END |
778 | ||
8ff5e0ec ZW |
779 | CHK_FAIL_START |
780 | wcsncpy (wbuf + 7, L"X", 4); | |
781 | CHK_FAIL_END | |
782 | ||
8215c9ec UD |
783 | CHK_FAIL_START |
784 | wcsncpy (wbuf + 7, L"X", l0 + 4); | |
785 | CHK_FAIL_END | |
786 | ||
d6cd6bf4 UD |
787 | CHK_FAIL_START |
788 | wcsncpy (wbuf + 9, L"XABCDEFGH", 8); | |
789 | CHK_FAIL_END | |
790 | ||
b54e5d1c FW |
791 | CHK_FAIL_START |
792 | wcslcpy (wbuf + 7, L"X", 4); | |
793 | CHK_FAIL_END | |
794 | ||
795 | CHK_FAIL_START | |
796 | wcslcpy (wbuf + 7, L"X", l0 + 4); | |
797 | CHK_FAIL_END | |
798 | ||
799 | CHK_FAIL_START | |
800 | wcslcpy (wbuf + 9, L"XABCDEFGH", 8); | |
801 | CHK_FAIL_END | |
802 | ||
d6cd6bf4 UD |
803 | CHK_FAIL_START |
804 | wcpncpy (wbuf + 9, L"XABCDEFGH", 8); | |
805 | CHK_FAIL_END | |
806 | ||
8ff5e0ec ZW |
807 | CHK_FAIL_START |
808 | wcpncpy (wbuf + 6, L"cd", 5); | |
809 | CHK_FAIL_END | |
810 | ||
8215c9ec UD |
811 | CHK_FAIL_START |
812 | wcpncpy (wbuf + 6, L"cd", l0 + 5); | |
813 | CHK_FAIL_END | |
814 | ||
8ff5e0ec | 815 | wmemcpy (wbuf, wstr1 + 2, 9); |
8215c9ec UD |
816 | CHK_FAIL_START |
817 | wcscat (wbuf, L"AB"); | |
818 | CHK_FAIL_END | |
819 | ||
8ff5e0ec | 820 | wmemcpy (wbuf, wstr1 + 3, 8); |
8215c9ec UD |
821 | CHK_FAIL_START |
822 | wcsncat (wbuf, L"ZYXWV", l0 + 3); | |
823 | CHK_FAIL_END | |
824 | ||
b54e5d1c FW |
825 | wmemcpy (wbuf, wstr1 + 4, 7); |
826 | CHK_FAIL_START | |
827 | wcslcat (wbuf, L"ZYXWV", l0 + 11); | |
828 | CHK_FAIL_END | |
829 | ||
8ff5e0ec ZW |
830 | CHK_FAIL_START |
831 | wmemcpy (wa.buf1 + 1, L"abcdefghij", 10); | |
832 | CHK_FAIL_END | |
833 | ||
8215c9ec UD |
834 | CHK_FAIL_START |
835 | wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10); | |
836 | CHK_FAIL_END | |
837 | ||
8ff5e0ec ZW |
838 | CHK_FAIL_START |
839 | wmemmove (wa.buf1 + 2, wa.buf1 + 1, 9); | |
840 | CHK_FAIL_END | |
841 | ||
8215c9ec UD |
842 | CHK_FAIL_START |
843 | wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9); | |
844 | CHK_FAIL_END | |
845 | ||
fcfc9086 | 846 | #ifdef _GNU_SOURCE |
8ff5e0ec ZW |
847 | CHK_FAIL_START |
848 | wp = wmempcpy (wa.buf1 + 6, L"abcde", 5); | |
849 | CHK_FAIL_END | |
850 | ||
8215c9ec | 851 | CHK_FAIL_START |
df6f8969 | 852 | wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5); |
8215c9ec | 853 | CHK_FAIL_END |
fcfc9086 | 854 | #endif |
8215c9ec | 855 | |
8ff5e0ec ZW |
856 | CHK_FAIL_START |
857 | wmemset (wa.buf1 + 9, L'j', 2); | |
858 | CHK_FAIL_END | |
859 | ||
8215c9ec UD |
860 | CHK_FAIL_START |
861 | wmemset (wa.buf1 + 9, L'j', l0 + 2); | |
862 | CHK_FAIL_END | |
863 | ||
864 | #if __USE_FORTIFY_LEVEL >= 2 | |
865 | # define O 0 | |
866 | #else | |
867 | # define O 1 | |
868 | #endif | |
869 | ||
870 | CHK_FAIL_START | |
871 | wcscpy (wa.buf1 + (O + 4), wstr1 + 5); | |
872 | CHK_FAIL_END | |
873 | ||
874 | CHK_FAIL_START | |
df6f8969 | 875 | wp = wcpcpy (wa.buf1 + (O + 8), wstr2); |
8215c9ec UD |
876 | CHK_FAIL_END |
877 | ||
8ff5e0ec ZW |
878 | CHK_FAIL_START |
879 | wcsncpy (wa.buf1 + (O + 6), L"X", 4); | |
880 | CHK_FAIL_END | |
881 | ||
8215c9ec UD |
882 | CHK_FAIL_START |
883 | wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4); | |
884 | CHK_FAIL_END | |
885 | ||
8ff5e0ec | 886 | wmemcpy (wa.buf1, wstr1 + (3 - O), 8 + O); |
8215c9ec UD |
887 | CHK_FAIL_START |
888 | wcscat (wa.buf1, L"AB"); | |
889 | CHK_FAIL_END | |
890 | ||
8ff5e0ec | 891 | wmemcpy (wa.buf1, wstr1 + (4 - O), 7 + O); |
8215c9ec UD |
892 | CHK_FAIL_START |
893 | wcsncat (wa.buf1, L"ZYXWV", l0 + 3); | |
894 | CHK_FAIL_END | |
895 | #endif | |
896 | ||
897 | ||
b5cc329c UD |
898 | /* Now checks for %n protection. */ |
899 | ||
900 | /* Constant literals passed directly are always ok | |
901 | (even with warnings about possible bugs from GCC). */ | |
902 | int n1, n2; | |
903 | if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2 | |
904 | || n1 != 1 || n2 != 2) | |
905 | FAIL (); | |
906 | ||
907 | /* In this case the format string is not known at compile time, | |
908 | but resides in read-only memory, so is ok. */ | |
909 | if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2 | |
910 | || n1 != 1 || n2 != 2) | |
911 | FAIL (); | |
912 | ||
121aad59 AZ |
913 | if (dprintf (temp_fd_dprintf, "%s%n%s%n", str2, &n1, str2, &n2) != 2 |
914 | || n1 != 1 || n2 != 2) | |
915 | FAIL (); | |
916 | ||
b5cc329c UD |
917 | strcpy (buf2 + 2, "%n%s%n"); |
918 | /* When the format string is writable and contains %n, | |
919 | with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */ | |
920 | CHK_FAIL2_START | |
921 | if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2) | |
922 | FAIL (); | |
923 | CHK_FAIL2_END | |
924 | ||
925 | CHK_FAIL2_START | |
926 | if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2) | |
927 | FAIL (); | |
928 | CHK_FAIL2_END | |
929 | ||
121aad59 AZ |
930 | CHK_FAIL2_START |
931 | if (dprintf (temp_fd_dprintf, buf2, str2, &n1, str2, &n1) != 2) | |
932 | FAIL (); | |
933 | CHK_FAIL2_END | |
934 | ||
b5cc329c UD |
935 | /* But if there is no %n, even writable format string |
936 | should work. */ | |
937 | buf2[6] = '\0'; | |
938 | if (sprintf (buf, buf2 + 4, str2) != 1) | |
939 | FAIL (); | |
940 | ||
941 | /* Constant literals passed directly are always ok | |
942 | (even with warnings about possible bugs from GCC). */ | |
943 | if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14 | |
944 | || n1 != 7 || n2 != 14) | |
945 | FAIL (); | |
946 | ||
947 | /* In this case the format string is not known at compile time, | |
948 | but resides in read-only memory, so is ok. */ | |
949 | if (printf (str3, str4, &n1, str5, &n2) != 14 | |
950 | || n1 != 7 || n2 != 14) | |
951 | FAIL (); | |
952 | ||
953 | strcpy (buf2 + 2, "%n%s%n"); | |
954 | /* When the format string is writable and contains %n, | |
955 | with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */ | |
956 | CHK_FAIL2_START | |
957 | if (printf (buf2, str4, &n1, str5, &n1) != 14) | |
958 | FAIL (); | |
959 | CHK_FAIL2_END | |
960 | ||
961 | /* But if there is no %n, even writable format string | |
962 | should work. */ | |
963 | buf2[6] = '\0'; | |
964 | if (printf (buf2 + 4, str5) != 7) | |
965 | FAIL (); | |
966 | ||
967 | FILE *fp = stdout; | |
968 | ||
969 | /* Constant literals passed directly are always ok | |
970 | (even with warnings about possible bugs from GCC). */ | |
971 | if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14 | |
972 | || n1 != 7 || n2 != 14) | |
973 | FAIL (); | |
974 | ||
975 | /* In this case the format string is not known at compile time, | |
976 | but resides in read-only memory, so is ok. */ | |
977 | if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14 | |
978 | || n1 != 7 || n2 != 14) | |
979 | FAIL (); | |
980 | ||
981 | strcpy (buf2 + 2, "%n%s%n"); | |
982 | /* When the format string is writable and contains %n, | |
983 | with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */ | |
984 | CHK_FAIL2_START | |
985 | if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14) | |
986 | FAIL (); | |
987 | CHK_FAIL2_END | |
988 | ||
989 | /* But if there is no %n, even writable format string | |
990 | should work. */ | |
991 | buf2[6] = '\0'; | |
992 | if (fprintf (fp, buf2 + 4, str5) != 7) | |
993 | FAIL (); | |
994 | ||
fcfc9086 | 995 | #ifdef _GNU_SOURCE |
dff37515 UD |
996 | char *my_ptr = NULL; |
997 | strcpy (buf2 + 2, "%n%s%n"); | |
998 | /* When the format string is writable and contains %n, | |
999 | with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */ | |
1000 | CHK_FAIL2_START | |
1001 | if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14) | |
1002 | FAIL (); | |
1003 | else | |
1004 | free (my_ptr); | |
1005 | CHK_FAIL2_END | |
1006 | ||
1007 | struct obstack obs; | |
1008 | obstack_init (&obs); | |
1009 | CHK_FAIL2_START | |
1010 | if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14) | |
1011 | FAIL (); | |
1012 | CHK_FAIL2_END | |
1013 | obstack_free (&obs, NULL); | |
1014 | ||
1015 | my_ptr = NULL; | |
1016 | if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14) | |
1017 | FAIL (); | |
1018 | else | |
1019 | free (my_ptr); | |
1020 | ||
1021 | obstack_init (&obs); | |
1022 | if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14) | |
1023 | FAIL (); | |
1024 | obstack_free (&obs, NULL); | |
fcfc9086 | 1025 | #endif |
dff37515 | 1026 | |
b5cc329c UD |
1027 | if (freopen (temp_filename, "r", stdin) == NULL) |
1028 | { | |
1029 | puts ("could not open temporary file"); | |
1030 | exit (1); | |
1031 | } | |
1032 | ||
1033 | if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9)) | |
1034 | FAIL (); | |
1035 | if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10)) | |
1036 | FAIL (); | |
1037 | ||
1038 | #if __USE_FORTIFY_LEVEL >= 1 | |
1039 | CHK_FAIL_START | |
1040 | if (gets (buf) != buf) | |
1041 | FAIL (); | |
1042 | CHK_FAIL_END | |
1043 | #endif | |
1044 | ||
553cc5f9 UD |
1045 | rewind (stdin); |
1046 | ||
ad6f2a01 | 1047 | if (fgets (buf, buf_size, stdin) != buf |
553cc5f9 UD |
1048 | || memcmp (buf, "abcdefgh\n", 10)) |
1049 | FAIL (); | |
ad6f2a01 | 1050 | if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10)) |
553cc5f9 UD |
1051 | FAIL (); |
1052 | ||
88764ae2 UD |
1053 | rewind (stdin); |
1054 | ||
ad6f2a01 | 1055 | if (fgets (buf, l0 + buf_size, stdin) != buf |
88764ae2 UD |
1056 | || memcmp (buf, "abcdefgh\n", 10)) |
1057 | FAIL (); | |
1058 | ||
553cc5f9 UD |
1059 | #if __USE_FORTIFY_LEVEL >= 1 |
1060 | CHK_FAIL_START | |
ad6f2a01 | 1061 | if (fgets (buf, buf_size + 1, stdin) != buf) |
553cc5f9 UD |
1062 | FAIL (); |
1063 | CHK_FAIL_END | |
88764ae2 UD |
1064 | |
1065 | CHK_FAIL_START | |
ad6f2a01 | 1066 | if (fgets (buf, l0 + buf_size + 1, stdin) != buf) |
88764ae2 UD |
1067 | FAIL (); |
1068 | CHK_FAIL_END | |
553cc5f9 UD |
1069 | #endif |
1070 | ||
1071 | rewind (stdin); | |
1072 | ||
fcfc9086 | 1073 | #ifdef _GNU_SOURCE |
ad6f2a01 | 1074 | if (fgets_unlocked (buf, buf_size, stdin) != buf |
553cc5f9 UD |
1075 | || memcmp (buf, "abcdefgh\n", 10)) |
1076 | FAIL (); | |
ad6f2a01 | 1077 | if (fgets_unlocked (buf, buf_size, stdin) != buf |
553cc5f9 UD |
1078 | || memcmp (buf, "ABCDEFGHI", 10)) |
1079 | FAIL (); | |
1080 | ||
88764ae2 UD |
1081 | rewind (stdin); |
1082 | ||
ad6f2a01 | 1083 | if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf |
88764ae2 UD |
1084 | || memcmp (buf, "abcdefgh\n", 10)) |
1085 | FAIL (); | |
1086 | ||
553cc5f9 UD |
1087 | #if __USE_FORTIFY_LEVEL >= 1 |
1088 | CHK_FAIL_START | |
ad6f2a01 | 1089 | if (fgets_unlocked (buf, buf_size + 1, stdin) != buf) |
553cc5f9 UD |
1090 | FAIL (); |
1091 | CHK_FAIL_END | |
88764ae2 UD |
1092 | |
1093 | CHK_FAIL_START | |
ad6f2a01 | 1094 | if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf) |
88764ae2 UD |
1095 | FAIL (); |
1096 | CHK_FAIL_END | |
553cc5f9 UD |
1097 | #endif |
1098 | ||
3586b2b6 | 1099 | rewind (stdin); |
fcfc9086 | 1100 | #endif |
3586b2b6 | 1101 | |
ad6f2a01 | 1102 | if (fread (buf, 1, buf_size, stdin) != buf_size |
3586b2b6 UD |
1103 | || memcmp (buf, "abcdefgh\nA", 10)) |
1104 | FAIL (); | |
ad6f2a01 | 1105 | if (fread (buf, buf_size, 1, stdin) != 1 |
3586b2b6 UD |
1106 | || memcmp (buf, "BCDEFGHI\na", 10)) |
1107 | FAIL (); | |
1108 | ||
1109 | rewind (stdin); | |
1110 | ||
ad6f2a01 | 1111 | if (fread (buf, l0 + 1, buf_size, stdin) != buf_size |
3586b2b6 UD |
1112 | || memcmp (buf, "abcdefgh\nA", 10)) |
1113 | FAIL (); | |
ad6f2a01 | 1114 | if (fread (buf, buf_size, l0 + 1, stdin) != 1 |
3586b2b6 UD |
1115 | || memcmp (buf, "BCDEFGHI\na", 10)) |
1116 | FAIL (); | |
1117 | ||
1118 | #if __USE_FORTIFY_LEVEL >= 1 | |
1119 | CHK_FAIL_START | |
ad6f2a01 | 1120 | if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1) |
3586b2b6 UD |
1121 | FAIL (); |
1122 | CHK_FAIL_END | |
1123 | ||
1124 | CHK_FAIL_START | |
ad6f2a01 | 1125 | if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1) |
3586b2b6 UD |
1126 | FAIL (); |
1127 | CHK_FAIL_END | |
1128 | #endif | |
1129 | ||
1130 | rewind (stdin); | |
1131 | ||
ad6f2a01 | 1132 | if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size |
3586b2b6 UD |
1133 | || memcmp (buf, "abcdefgh\nA", 10)) |
1134 | FAIL (); | |
ad6f2a01 | 1135 | if (fread_unlocked (buf, buf_size, 1, stdin) != 1 |
3586b2b6 UD |
1136 | || memcmp (buf, "BCDEFGHI\na", 10)) |
1137 | FAIL (); | |
1138 | ||
1139 | rewind (stdin); | |
1140 | ||
1141 | if (fread_unlocked (buf, 1, 4, stdin) != 4 | |
1142 | || memcmp (buf, "abcdFGHI\na", 10)) | |
1143 | FAIL (); | |
1144 | if (fread_unlocked (buf, 4, 1, stdin) != 1 | |
1145 | || memcmp (buf, "efghFGHI\na", 10)) | |
1146 | FAIL (); | |
1147 | ||
1148 | rewind (stdin); | |
1149 | ||
ad6f2a01 | 1150 | if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size |
3586b2b6 UD |
1151 | || memcmp (buf, "abcdefgh\nA", 10)) |
1152 | FAIL (); | |
ad6f2a01 | 1153 | if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1 |
3586b2b6 UD |
1154 | || memcmp (buf, "BCDEFGHI\na", 10)) |
1155 | FAIL (); | |
1156 | ||
1157 | #if __USE_FORTIFY_LEVEL >= 1 | |
1158 | CHK_FAIL_START | |
ad6f2a01 | 1159 | if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1) |
3586b2b6 UD |
1160 | FAIL (); |
1161 | CHK_FAIL_END | |
1162 | ||
1163 | CHK_FAIL_START | |
ad6f2a01 | 1164 | if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1) |
3586b2b6 UD |
1165 | FAIL (); |
1166 | CHK_FAIL_END | |
1167 | #endif | |
1168 | ||
553cc5f9 UD |
1169 | lseek (fileno (stdin), 0, SEEK_SET); |
1170 | ||
ad6f2a01 | 1171 | if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1 |
553cc5f9 UD |
1172 | || memcmp (buf, "abcdefgh\n", 9)) |
1173 | FAIL (); | |
ad6f2a01 | 1174 | if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1 |
553cc5f9 UD |
1175 | || memcmp (buf, "ABCDEFGHI", 9)) |
1176 | FAIL (); | |
1177 | ||
88764ae2 UD |
1178 | lseek (fileno (stdin), 0, SEEK_SET); |
1179 | ||
ad6f2a01 | 1180 | if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1 |
88764ae2 UD |
1181 | || memcmp (buf, "abcdefgh\n", 9)) |
1182 | FAIL (); | |
1183 | ||
553cc5f9 UD |
1184 | #if __USE_FORTIFY_LEVEL >= 1 |
1185 | CHK_FAIL_START | |
ad6f2a01 | 1186 | if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1) |
553cc5f9 UD |
1187 | FAIL (); |
1188 | CHK_FAIL_END | |
8ff5e0ec ZW |
1189 | |
1190 | CHK_FAIL_START | |
ad6f2a01 | 1191 | if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1) |
8ff5e0ec ZW |
1192 | FAIL (); |
1193 | CHK_FAIL_END | |
553cc5f9 UD |
1194 | #endif |
1195 | ||
ad6f2a01 SP |
1196 | if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2) |
1197 | != buf_size - 1 | |
88764ae2 UD |
1198 | || memcmp (buf, "\nABCDEFGH", 9)) |
1199 | FAIL (); | |
ad6f2a01 | 1200 | if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1 |
553cc5f9 UD |
1201 | || memcmp (buf, "abcdefgh\n", 9)) |
1202 | FAIL (); | |
ad6f2a01 SP |
1203 | if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3) |
1204 | != buf_size - 1 | |
88764ae2 | 1205 | || memcmp (buf, "h\nABCDEFG", 9)) |
553cc5f9 UD |
1206 | FAIL (); |
1207 | ||
1208 | #if __USE_FORTIFY_LEVEL >= 1 | |
1209 | CHK_FAIL_START | |
ad6f2a01 SP |
1210 | if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size) |
1211 | != buf_size + 1) | |
553cc5f9 UD |
1212 | FAIL (); |
1213 | CHK_FAIL_END | |
8ff5e0ec ZW |
1214 | |
1215 | CHK_FAIL_START | |
ad6f2a01 SP |
1216 | if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size) |
1217 | != buf_size + 1) | |
8ff5e0ec ZW |
1218 | FAIL (); |
1219 | CHK_FAIL_END | |
553cc5f9 UD |
1220 | #endif |
1221 | ||
ad6f2a01 SP |
1222 | if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2) |
1223 | != buf_size - 1 | |
88764ae2 UD |
1224 | || memcmp (buf, "\nABCDEFGH", 9)) |
1225 | FAIL (); | |
ad6f2a01 | 1226 | if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1 |
553cc5f9 UD |
1227 | || memcmp (buf, "abcdefgh\n", 9)) |
1228 | FAIL (); | |
ad6f2a01 SP |
1229 | if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3) |
1230 | != buf_size - 1 | |
88764ae2 | 1231 | || memcmp (buf, "h\nABCDEFG", 9)) |
553cc5f9 UD |
1232 | FAIL (); |
1233 | ||
1234 | #if __USE_FORTIFY_LEVEL >= 1 | |
1235 | CHK_FAIL_START | |
ad6f2a01 SP |
1236 | if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size) |
1237 | != buf_size + 1) | |
553cc5f9 UD |
1238 | FAIL (); |
1239 | CHK_FAIL_END | |
8ff5e0ec ZW |
1240 | |
1241 | CHK_FAIL_START | |
ad6f2a01 SP |
1242 | if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size) |
1243 | != buf_size + 1) | |
8ff5e0ec ZW |
1244 | FAIL (); |
1245 | CHK_FAIL_END | |
553cc5f9 UD |
1246 | #endif |
1247 | ||
b5cc329c UD |
1248 | if (freopen (temp_filename, "r", stdin) == NULL) |
1249 | { | |
1250 | puts ("could not open temporary file"); | |
1251 | exit (1); | |
1252 | } | |
1253 | ||
1254 | if (fseek (stdin, 9 + 10 + 11, SEEK_SET)) | |
1255 | { | |
1256 | puts ("could not seek in test file"); | |
1257 | exit (1); | |
1258 | } | |
1259 | ||
1260 | #if __USE_FORTIFY_LEVEL >= 1 | |
1261 | CHK_FAIL_START | |
1262 | if (gets (buf) != buf) | |
1263 | FAIL (); | |
1264 | CHK_FAIL_END | |
1265 | #endif | |
1266 | ||
1b1d3679 UD |
1267 | /* Check whether missing N$ formats are detected. */ |
1268 | CHK_FAIL2_START | |
1269 | printf ("%3$d\n", 1, 2, 3, 4); | |
1270 | CHK_FAIL2_END | |
1271 | ||
1272 | CHK_FAIL2_START | |
1273 | fprintf (stdout, "%3$d\n", 1, 2, 3, 4); | |
1274 | CHK_FAIL2_END | |
1275 | ||
1276 | CHK_FAIL2_START | |
1277 | sprintf (buf, "%3$d\n", 1, 2, 3, 4); | |
1278 | CHK_FAIL2_END | |
1279 | ||
1280 | CHK_FAIL2_START | |
ad6f2a01 | 1281 | snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4); |
1b1d3679 UD |
1282 | CHK_FAIL2_END |
1283 | ||
121aad59 AZ |
1284 | CHK_FAIL2_START |
1285 | dprintf (temp_fd_dprintf, "%3$d\n", 1, 2, 3, 4); | |
1286 | CHK_FAIL2_END | |
1287 | ||
88764ae2 UD |
1288 | int sp[2]; |
1289 | if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp)) | |
1290 | FAIL (); | |
1291 | else | |
1292 | { | |
1293 | const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n"; | |
de1c3ebb UD |
1294 | if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0) |
1295 | != strlen (sendstr)) | |
88764ae2 UD |
1296 | FAIL (); |
1297 | ||
1298 | char recvbuf[12]; | |
1299 | if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK) | |
1300 | != sizeof recvbuf | |
1301 | || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0) | |
1302 | FAIL (); | |
1303 | ||
1304 | if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK) | |
1305 | != sizeof recvbuf - 7 | |
1306 | || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0) | |
1307 | FAIL (); | |
1308 | ||
1309 | #if __USE_FORTIFY_LEVEL >= 1 | |
1310 | CHK_FAIL_START | |
1311 | if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK) | |
1312 | != sizeof recvbuf) | |
1313 | FAIL (); | |
1314 | CHK_FAIL_END | |
1315 | ||
1316 | CHK_FAIL_START | |
1317 | if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK) | |
1318 | != sizeof recvbuf - 3) | |
1319 | FAIL (); | |
1320 | CHK_FAIL_END | |
1321 | #endif | |
1322 | ||
1323 | socklen_t sl; | |
1324 | struct sockaddr_un sa_un; | |
1325 | ||
1326 | sl = sizeof (sa_un); | |
de1c3ebb UD |
1327 | if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK, |
1328 | (struct sockaddr *) &sa_un, &sl) | |
88764ae2 UD |
1329 | != sizeof recvbuf |
1330 | || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0) | |
1331 | FAIL (); | |
1332 | ||
1333 | sl = sizeof (sa_un); | |
1334 | if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK, | |
de1c3ebb | 1335 | (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7 |
88764ae2 UD |
1336 | || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0) |
1337 | FAIL (); | |
1338 | ||
1339 | #if __USE_FORTIFY_LEVEL >= 1 | |
1340 | CHK_FAIL_START | |
1341 | sl = sizeof (sa_un); | |
de1c3ebb UD |
1342 | if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK, |
1343 | (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf) | |
88764ae2 UD |
1344 | FAIL (); |
1345 | CHK_FAIL_END | |
1346 | ||
1347 | CHK_FAIL_START | |
1348 | sl = sizeof (sa_un); | |
1349 | if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK, | |
de1c3ebb | 1350 | (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3) |
88764ae2 UD |
1351 | FAIL (); |
1352 | CHK_FAIL_END | |
1353 | #endif | |
1354 | ||
1355 | close (sp[0]); | |
1356 | close (sp[1]); | |
1357 | } | |
1358 | ||
1359 | char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo"; | |
1360 | char *enddir = strchr (fname, '\0'); | |
1361 | if (mkdtemp (fname) == NULL) | |
1362 | { | |
1363 | printf ("mkdtemp failed: %m\n"); | |
1364 | return 1; | |
1365 | } | |
1366 | *enddir = '/'; | |
1367 | if (symlink ("bar", fname) != 0) | |
1368 | FAIL (); | |
1369 | ||
1370 | char readlinkbuf[4]; | |
1371 | if (readlink (fname, readlinkbuf, 4) != 3 | |
1372 | || memcmp (readlinkbuf, "bar", 3) != 0) | |
1373 | FAIL (); | |
1374 | if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3 | |
1375 | || memcmp (readlinkbuf, "bbar", 4) != 0) | |
1376 | FAIL (); | |
1377 | ||
1378 | #if __USE_FORTIFY_LEVEL >= 1 | |
1379 | CHK_FAIL_START | |
1380 | if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3) | |
1381 | FAIL (); | |
1382 | CHK_FAIL_END | |
1383 | ||
1384 | CHK_FAIL_START | |
1385 | if (readlink (fname, readlinkbuf + 3, 4) != 3) | |
1386 | FAIL (); | |
1387 | CHK_FAIL_END | |
1388 | #endif | |
1389 | ||
a346370d UD |
1390 | int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY); |
1391 | if (tmpfd < 0) | |
1392 | FAIL (); | |
1393 | ||
1394 | if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3 | |
1395 | || memcmp (readlinkbuf, "bar", 3) != 0) | |
1396 | FAIL (); | |
1397 | if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1, | |
1398 | l0 + 3) != 3 | |
1399 | || memcmp (readlinkbuf, "bbar", 4) != 0) | |
1400 | FAIL (); | |
1401 | ||
1402 | #if __USE_FORTIFY_LEVEL >= 1 | |
1403 | CHK_FAIL_START | |
1404 | if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2, | |
1405 | l0 + 3) != 3) | |
1406 | FAIL (); | |
1407 | CHK_FAIL_END | |
1408 | ||
1409 | CHK_FAIL_START | |
1410 | if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3, | |
1411 | 4) != 3) | |
1412 | FAIL (); | |
1413 | CHK_FAIL_END | |
1414 | #endif | |
1415 | ||
1416 | close (tmpfd); | |
1417 | ||
88764ae2 UD |
1418 | char *cwd1 = getcwd (NULL, 0); |
1419 | if (cwd1 == NULL) | |
1420 | FAIL (); | |
1421 | ||
1422 | char *cwd2 = getcwd (NULL, 250); | |
1423 | if (cwd2 == NULL) | |
1424 | FAIL (); | |
1425 | ||
1426 | if (cwd1 && cwd2) | |
1427 | { | |
1428 | if (strcmp (cwd1, cwd2) != 0) | |
1429 | FAIL (); | |
1430 | ||
1431 | *enddir = '\0'; | |
1432 | if (chdir (fname)) | |
1433 | FAIL (); | |
1434 | ||
1435 | char *cwd3 = getcwd (NULL, 0); | |
1436 | if (cwd3 == NULL) | |
1437 | FAIL (); | |
1438 | if (strcmp (fname, cwd3) != 0) | |
1439 | printf ("getcwd after chdir is '%s' != '%s'," | |
1440 | "get{c,}wd tests skipped\n", cwd3, fname); | |
1441 | else | |
1442 | { | |
1443 | char getcwdbuf[sizeof fname - 3]; | |
1444 | ||
1445 | char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf); | |
1446 | if (cwd4 != getcwdbuf | |
1447 | || strcmp (getcwdbuf, fname) != 0) | |
1448 | FAIL (); | |
1449 | ||
1450 | cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1); | |
1451 | if (cwd4 != getcwdbuf + 1 | |
1452 | || getcwdbuf[0] != fname[0] | |
1453 | || strcmp (getcwdbuf + 1, fname) != 0) | |
1454 | FAIL (); | |
1455 | ||
1456 | #if __USE_FORTIFY_LEVEL >= 1 | |
1457 | CHK_FAIL_START | |
1458 | if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf) | |
1459 | != getcwdbuf + 2) | |
1460 | FAIL (); | |
1461 | CHK_FAIL_END | |
1462 | ||
1463 | CHK_FAIL_START | |
1464 | if (getcwd (getcwdbuf + 2, sizeof getcwdbuf) | |
1465 | != getcwdbuf + 2) | |
1466 | FAIL (); | |
1467 | CHK_FAIL_END | |
1468 | #endif | |
1469 | ||
1470 | if (getwd (getcwdbuf) != getcwdbuf | |
1471 | || strcmp (getcwdbuf, fname) != 0) | |
1472 | FAIL (); | |
1473 | ||
1474 | if (getwd (getcwdbuf + 1) != getcwdbuf + 1 | |
1475 | || strcmp (getcwdbuf + 1, fname) != 0) | |
1476 | FAIL (); | |
1477 | ||
1478 | #if __USE_FORTIFY_LEVEL >= 1 | |
1479 | CHK_FAIL_START | |
1480 | if (getwd (getcwdbuf + 2) != getcwdbuf + 2) | |
1481 | FAIL (); | |
1482 | CHK_FAIL_END | |
1483 | #endif | |
1484 | } | |
1485 | ||
1486 | if (chdir (cwd1) != 0) | |
1487 | FAIL (); | |
1488 | free (cwd3); | |
1489 | } | |
1490 | ||
1491 | free (cwd1); | |
1492 | free (cwd2); | |
1493 | *enddir = '/'; | |
1494 | if (unlink (fname) != 0) | |
1495 | FAIL (); | |
1496 | ||
1497 | *enddir = '\0'; | |
1498 | if (rmdir (fname) != 0) | |
1499 | FAIL (); | |
1500 | ||
b799f91d UD |
1501 | |
1502 | #if PATH_MAX > 0 | |
1503 | char largebuf[PATH_MAX]; | |
1504 | char *realres = realpath (".", largebuf); | |
f9a906e7 UD |
1505 | if (realres != largebuf) |
1506 | FAIL (); | |
1507 | ||
1508 | # if __USE_FORTIFY_LEVEL >= 1 | |
b799f91d UD |
1509 | CHK_FAIL_START |
1510 | char realbuf[1]; | |
1511 | realres = realpath (".", realbuf); | |
f9a906e7 UD |
1512 | if (realres != realbuf) |
1513 | FAIL (); | |
b799f91d | 1514 | CHK_FAIL_END |
f9a906e7 | 1515 | # endif |
b799f91d UD |
1516 | #endif |
1517 | ||
1518 | if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL) | |
1519 | { | |
f9a906e7 UD |
1520 | assert (MB_CUR_MAX <= 10); |
1521 | ||
b799f91d | 1522 | /* First a simple test. */ |
f9a906e7 | 1523 | char enough[10]; |
b799f91d | 1524 | if (wctomb (enough, L'A') != 1) |
f9a906e7 | 1525 | FAIL (); |
b799f91d UD |
1526 | |
1527 | #if __USE_FORTIFY_LEVEL >= 1 | |
1528 | /* We know the wchar_t encoding is ISO 10646. So pick a | |
1529 | character which has a multibyte representation which does not | |
1530 | fit. */ | |
1531 | CHK_FAIL_START | |
1532 | char smallbuf[2]; | |
1533 | if (wctomb (smallbuf, L'\x100') != 2) | |
f9a906e7 | 1534 | FAIL (); |
b799f91d UD |
1535 | CHK_FAIL_END |
1536 | #endif | |
df6f8969 UD |
1537 | |
1538 | mbstate_t s; | |
1539 | memset (&s, '\0', sizeof (s)); | |
f9a906e7 UD |
1540 | if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D') |
1541 | FAIL (); | |
df6f8969 UD |
1542 | |
1543 | #if __USE_FORTIFY_LEVEL >= 1 | |
1544 | /* We know the wchar_t encoding is ISO 10646. So pick a | |
1545 | character which has a multibyte representation which does not | |
1546 | fit. */ | |
1547 | CHK_FAIL_START | |
9bcd12d2 | 1548 | char smallbuf[1]; |
df6f8969 | 1549 | if (wcrtomb (smallbuf, L'\x100', &s) != 2) |
f9a906e7 | 1550 | FAIL (); |
df6f8969 | 1551 | CHK_FAIL_END |
9bcd12d2 SP |
1552 | |
1553 | /* Same input with a large enough buffer and we're good. */ | |
1554 | char bigenoughbuf[2]; | |
1555 | if (wcrtomb (bigenoughbuf, L'\x100', &s) != 2) | |
1556 | FAIL (); | |
df6f8969 UD |
1557 | #endif |
1558 | ||
1559 | wchar_t wenough[10]; | |
1560 | memset (&s, '\0', sizeof (s)); | |
1561 | const char *cp = "A"; | |
f9a906e7 UD |
1562 | if (mbsrtowcs (wenough, &cp, 10, &s) != 1 |
1563 | || wcscmp (wenough, L"A") != 0) | |
1564 | FAIL (); | |
1565 | ||
1566 | cp = "BC"; | |
1567 | if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2 | |
1568 | || wcscmp (wenough, L"BC") != 0) | |
1569 | FAIL (); | |
df6f8969 UD |
1570 | |
1571 | #if __USE_FORTIFY_LEVEL >= 1 | |
df6f8969 UD |
1572 | CHK_FAIL_START |
1573 | wchar_t wsmallbuf[2]; | |
1574 | cp = "ABC"; | |
1575 | mbsrtowcs (wsmallbuf, &cp, 10, &s); | |
1576 | CHK_FAIL_END | |
1577 | #endif | |
1578 | ||
7f0d9e61 | 1579 | /* Bug 29030 regression check */ |
33e03f9c JB |
1580 | cp = "HelloWorld"; |
1581 | if (mbsrtowcs (NULL, &cp, (size_t)-1, &s) != 10) | |
1582 | FAIL (); | |
1583 | ||
2a81f178 | 1584 | cp = "A"; |
f9a906e7 UD |
1585 | if (mbstowcs (wenough, cp, 10) != 1 |
1586 | || wcscmp (wenough, L"A") != 0) | |
1587 | FAIL (); | |
1588 | ||
1589 | cp = "DEF"; | |
1590 | if (mbstowcs (wenough, cp, l0 + 10) != 3 | |
1591 | || wcscmp (wenough, L"DEF") != 0) | |
1592 | FAIL (); | |
2a81f178 UD |
1593 | |
1594 | #if __USE_FORTIFY_LEVEL >= 1 | |
2a81f178 UD |
1595 | CHK_FAIL_START |
1596 | wchar_t wsmallbuf[2]; | |
1597 | cp = "ABC"; | |
1598 | mbstowcs (wsmallbuf, cp, 10); | |
1599 | CHK_FAIL_END | |
1600 | #endif | |
1601 | ||
df6f8969 | 1602 | memset (&s, '\0', sizeof (s)); |
f9a906e7 UD |
1603 | cp = "ABC"; |
1604 | wcscpy (wenough, L"DEF"); | |
1605 | if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1 | |
1606 | || wcscmp (wenough, L"AEF") != 0) | |
1607 | FAIL (); | |
1608 | ||
1609 | cp = "IJ"; | |
1610 | if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1 | |
1611 | || wcscmp (wenough, L"IEF") != 0) | |
1612 | FAIL (); | |
df6f8969 UD |
1613 | |
1614 | #if __USE_FORTIFY_LEVEL >= 1 | |
df6f8969 UD |
1615 | CHK_FAIL_START |
1616 | wchar_t wsmallbuf[2]; | |
1617 | cp = "ABC"; | |
1618 | mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s); | |
1619 | CHK_FAIL_END | |
1620 | #endif | |
1621 | ||
1622 | memset (&s, '\0', sizeof (s)); | |
1623 | const wchar_t *wcp = L"A"; | |
f9a906e7 UD |
1624 | if (wcsrtombs (enough, &wcp, 10, &s) != 1 |
1625 | || strcmp (enough, "A") != 0) | |
1626 | FAIL (); | |
1627 | ||
1628 | wcp = L"BC"; | |
1629 | if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2 | |
1630 | || strcmp (enough, "BC") != 0) | |
1631 | FAIL (); | |
df6f8969 UD |
1632 | |
1633 | #if __USE_FORTIFY_LEVEL >= 1 | |
df6f8969 UD |
1634 | CHK_FAIL_START |
1635 | char smallbuf[2]; | |
1636 | wcp = L"ABC"; | |
1637 | wcsrtombs (smallbuf, &wcp, 10, &s); | |
1638 | CHK_FAIL_END | |
1639 | #endif | |
1640 | ||
f9a906e7 UD |
1641 | memset (enough, 'Z', sizeof (enough)); |
1642 | wcp = L"EF"; | |
1643 | if (wcstombs (enough, wcp, 10) != 2 | |
1644 | || strcmp (enough, "EF") != 0) | |
1645 | FAIL (); | |
1646 | ||
1647 | wcp = L"G"; | |
1648 | if (wcstombs (enough, wcp, l0 + 10) != 1 | |
1649 | || strcmp (enough, "G") != 0) | |
1650 | FAIL (); | |
2a81f178 UD |
1651 | |
1652 | #if __USE_FORTIFY_LEVEL >= 1 | |
2a81f178 UD |
1653 | CHK_FAIL_START |
1654 | char smallbuf[2]; | |
1655 | wcp = L"ABC"; | |
1656 | wcstombs (smallbuf, wcp, 10); | |
1657 | CHK_FAIL_END | |
1658 | #endif | |
1659 | ||
df6f8969 | 1660 | memset (&s, '\0', sizeof (s)); |
f9a906e7 UD |
1661 | wcp = L"AB"; |
1662 | if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1 | |
1663 | || strcmp (enough, "A") != 0) | |
1664 | FAIL (); | |
1665 | ||
1666 | wcp = L"BCD"; | |
1667 | if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1 | |
1668 | || strcmp (enough, "B") != 0) | |
1669 | FAIL (); | |
df6f8969 UD |
1670 | |
1671 | #if __USE_FORTIFY_LEVEL >= 1 | |
df6f8969 UD |
1672 | CHK_FAIL_START |
1673 | char smallbuf[2]; | |
1674 | wcp = L"ABC"; | |
1675 | wcsnrtombs (smallbuf, &wcp, 3, 10, &s); | |
1676 | CHK_FAIL_END | |
1677 | #endif | |
b799f91d UD |
1678 | } |
1679 | else | |
1680 | { | |
1681 | puts ("cannot set locale"); | |
1682 | ret = 1; | |
1683 | } | |
1684 | ||
fcfc9086 SP |
1685 | int fd; |
1686 | ||
1687 | #ifdef _GNU_SOURCE | |
1688 | fd = posix_openpt (O_RDWR); | |
b799f91d UD |
1689 | if (fd != -1) |
1690 | { | |
1691 | char enough[1000]; | |
1692 | if (ptsname_r (fd, enough, sizeof (enough)) != 0) | |
f9a906e7 | 1693 | FAIL (); |
b799f91d UD |
1694 | |
1695 | #if __USE_FORTIFY_LEVEL >= 1 | |
1696 | CHK_FAIL_START | |
1697 | char smallbuf[2]; | |
1698 | if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0) | |
f9a906e7 | 1699 | FAIL (); |
b799f91d UD |
1700 | CHK_FAIL_END |
1701 | #endif | |
9f3731cf UD |
1702 | close (fd); |
1703 | } | |
fcfc9086 | 1704 | #endif |
9f3731cf | 1705 | |
f9a906e7 | 1706 | #if PATH_MAX > 0 |
9f3731cf | 1707 | confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf)); |
f9a906e7 | 1708 | # if __USE_FORTIFY_LEVEL >= 1 |
9f3731cf UD |
1709 | CHK_FAIL_START |
1710 | char smallbuf[1]; | |
1711 | confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf)); | |
1712 | CHK_FAIL_END | |
f9a906e7 | 1713 | # endif |
9f3731cf UD |
1714 | #endif |
1715 | ||
1716 | gid_t grpslarge[5]; | |
1717 | int ngr = getgroups (5, grpslarge); | |
f9a906e7 | 1718 | asm volatile ("" : : "r" (ngr)); |
9f3731cf UD |
1719 | #if __USE_FORTIFY_LEVEL >= 1 |
1720 | CHK_FAIL_START | |
1721 | char smallbuf[1]; | |
1722 | ngr = getgroups (5, (gid_t *) smallbuf); | |
f9a906e7 | 1723 | asm volatile ("" : : "r" (ngr)); |
9f3731cf UD |
1724 | CHK_FAIL_END |
1725 | #endif | |
1726 | ||
1727 | fd = open (_PATH_TTY, O_RDONLY); | |
1728 | if (fd != -1) | |
1729 | { | |
1730 | char enough[1000]; | |
1731 | if (ttyname_r (fd, enough, sizeof (enough)) != 0) | |
f9a906e7 | 1732 | FAIL (); |
9f3731cf UD |
1733 | |
1734 | #if __USE_FORTIFY_LEVEL >= 1 | |
1735 | CHK_FAIL_START | |
1736 | char smallbuf[2]; | |
1737 | if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0) | |
f9a906e7 | 1738 | FAIL (); |
9f3731cf UD |
1739 | CHK_FAIL_END |
1740 | #endif | |
1741 | close (fd); | |
b799f91d UD |
1742 | } |
1743 | ||
9f3731cf UD |
1744 | char hostnamelarge[1000]; |
1745 | gethostname (hostnamelarge, sizeof (hostnamelarge)); | |
1746 | #if __USE_FORTIFY_LEVEL >= 1 | |
1747 | CHK_FAIL_START | |
1748 | char smallbuf[1]; | |
1749 | gethostname (smallbuf, sizeof (hostnamelarge)); | |
1750 | CHK_FAIL_END | |
1751 | #endif | |
1752 | ||
1753 | char loginlarge[1000]; | |
1754 | getlogin_r (loginlarge, sizeof (hostnamelarge)); | |
1755 | #if __USE_FORTIFY_LEVEL >= 1 | |
1756 | CHK_FAIL_START | |
1757 | char smallbuf[1]; | |
1758 | getlogin_r (smallbuf, sizeof (loginlarge)); | |
1759 | CHK_FAIL_END | |
1760 | #endif | |
1761 | ||
1762 | char domainnamelarge[1000]; | |
1763 | int res = getdomainname (domainnamelarge, sizeof (domainnamelarge)); | |
f9a906e7 | 1764 | asm volatile ("" : : "r" (res)); |
9f3731cf UD |
1765 | #if __USE_FORTIFY_LEVEL >= 1 |
1766 | CHK_FAIL_START | |
1767 | char smallbuf[1]; | |
1768 | res = getdomainname (smallbuf, sizeof (domainnamelarge)); | |
f9a906e7 | 1769 | asm volatile ("" : : "r" (res)); |
9f3731cf UD |
1770 | CHK_FAIL_END |
1771 | #endif | |
1772 | ||
a0f33f99 UD |
1773 | fd_set s; |
1774 | FD_ZERO (&s); | |
8ff5e0ec | 1775 | |
a0f33f99 UD |
1776 | FD_SET (FD_SETSIZE - 1, &s); |
1777 | #if __USE_FORTIFY_LEVEL >= 1 | |
1778 | CHK_FAIL_START | |
1779 | FD_SET (FD_SETSIZE, &s); | |
1780 | CHK_FAIL_END | |
8ff5e0ec ZW |
1781 | |
1782 | CHK_FAIL_START | |
1783 | FD_SET (l0 + FD_SETSIZE, &s); | |
1784 | CHK_FAIL_END | |
a0f33f99 | 1785 | #endif |
8ff5e0ec | 1786 | |
a0f33f99 UD |
1787 | FD_CLR (FD_SETSIZE - 1, &s); |
1788 | #if __USE_FORTIFY_LEVEL >= 1 | |
1789 | CHK_FAIL_START | |
1790 | FD_CLR (FD_SETSIZE, &s); | |
1791 | CHK_FAIL_END | |
8ff5e0ec ZW |
1792 | |
1793 | CHK_FAIL_START | |
1794 | FD_SET (l0 + FD_SETSIZE, &s); | |
1795 | CHK_FAIL_END | |
a0f33f99 | 1796 | #endif |
8ff5e0ec | 1797 | |
a0f33f99 UD |
1798 | FD_ISSET (FD_SETSIZE - 1, &s); |
1799 | #if __USE_FORTIFY_LEVEL >= 1 | |
1800 | CHK_FAIL_START | |
1801 | FD_ISSET (FD_SETSIZE, &s); | |
1802 | CHK_FAIL_END | |
8ff5e0ec ZW |
1803 | |
1804 | CHK_FAIL_START | |
1805 | FD_ISSET (l0 + FD_SETSIZE, &s); | |
1806 | CHK_FAIL_END | |
a0f33f99 UD |
1807 | #endif |
1808 | ||
d9a216c0 UD |
1809 | struct pollfd fds[1]; |
1810 | fds[0].fd = STDOUT_FILENO; | |
1811 | fds[0].events = POLLOUT; | |
1812 | poll (fds, 1, 0); | |
1813 | #if __USE_FORTIFY_LEVEL >= 1 | |
1814 | CHK_FAIL_START | |
1815 | poll (fds, 2, 0); | |
1816 | CHK_FAIL_END | |
8ff5e0ec ZW |
1817 | |
1818 | CHK_FAIL_START | |
1819 | poll (fds, l0 + 2, 0); | |
1820 | CHK_FAIL_END | |
d9a216c0 | 1821 | #endif |
fcfc9086 | 1822 | #ifdef _GNU_SOURCE |
d9a216c0 | 1823 | ppoll (fds, 1, NULL, NULL); |
fcfc9086 | 1824 | # if __USE_FORTIFY_LEVEL >= 1 |
d9a216c0 UD |
1825 | CHK_FAIL_START |
1826 | ppoll (fds, 2, NULL, NULL); | |
1827 | CHK_FAIL_END | |
8ff5e0ec ZW |
1828 | |
1829 | CHK_FAIL_START | |
1830 | ppoll (fds, l0 + 2, NULL, NULL); | |
1831 | CHK_FAIL_END | |
fcfc9086 | 1832 | # endif |
d9a216c0 UD |
1833 | #endif |
1834 | ||
b5cc329c UD |
1835 | return ret; |
1836 | } | |
9556acd2 AZ |
1837 | |
1838 | #include <support/test-driver.c> |