debug: Add fortify dprintf tests
[glibc.git] / debug / tst-fortify.c
blob888eae25ad140259ecb9276c2e6a71239e085266
1 /* Copyright (C) 2004-2023 Free Software Foundation, Inc.
2 Copyright The GNU Toolchain Authors.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
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
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <locale.h>
28 #include <obstack.h>
29 #include <setjmp.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <wchar.h>
36 #include <sys/poll.h>
37 #include <sys/select.h>
38 #include <sys/socket.h>
39 #include <sys/un.h>
40 #include <paths.h>
42 #include <support/temp_file.h>
43 #include <support/support.h>
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
57 #define obstack_chunk_alloc malloc
58 #define obstack_chunk_free free
60 static char *temp_filename;
62 static int temp_fd_dprintf;
64 static void
65 do_prepare (int argc, char *argv[])
67 int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
68 if (temp_fd == -1)
70 printf ("cannot create temporary file: %m\n");
71 exit (1);
74 const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
75 if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
77 puts ("could not write test strings into file");
78 unlink (temp_filename);
79 exit (1);
82 temp_fd_dprintf = create_temp_file ("tst-chk2.", NULL);
83 if (temp_fd_dprintf == -1)
85 printf ("cannot create temporary file: %m\n");
86 exit (1);
89 #define PREPARE do_prepare
91 static volatile int chk_fail_ok;
92 static volatile int ret;
93 static jmp_buf chk_fail_buf;
95 static void
96 handler (int sig)
98 if (chk_fail_ok)
100 chk_fail_ok = 0;
101 longjmp (chk_fail_buf, 1);
103 else
104 _exit (127);
107 #if __USE_FORTIFY_LEVEL == 3
108 volatile size_t buf_size = 10;
109 #else
110 char buf[10];
111 wchar_t wbuf[10];
112 #define buf_size sizeof (buf)
113 #endif
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;
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)) \
138 #define CHK_FAIL_END \
139 chk_fail_ok = 0; \
140 FAIL (); \
142 #if __USE_FORTIFY_LEVEL >= 2
143 # define CHK_FAIL2_START CHK_FAIL_START
144 # define CHK_FAIL2_END CHK_FAIL_END
145 #else
146 # define CHK_FAIL2_START
147 # define CHK_FAIL2_END
148 #endif
150 static int
151 do_test (void)
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
157 set_fortify_handler (handler);
159 struct A { char buf1[9]; char buf2[1]; } a;
160 struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
162 printf ("Test checking routines at fortify level %d\n",
163 #ifdef __USE_FORTIFY_LEVEL
164 (int) __USE_FORTIFY_LEVEL
165 #else
167 #endif
170 #if defined __USE_FORTIFY_LEVEL && !defined __fortify_function
171 printf ("Test skipped");
172 if (l0 == 0)
173 return 0;
174 #endif
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 ();
182 memcpy (buf, "abcdefghij", 10);
183 bcopy (buf, buf + 1, 9);
184 if (memcmp (buf, "aabcdefghi", 10))
185 FAIL ();
187 if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
188 || memcmp (buf, "aabcdabcde", 10))
189 FAIL ();
191 memset (buf + 8, 'j', 2);
192 if (memcmp (buf, "aabcdabcjj", 10))
193 FAIL ();
195 bzero (buf + 8, 2);
196 if (memcmp (buf, "aabcdabc\0\0", 10))
197 FAIL ();
199 explicit_bzero (buf + 6, 4);
200 if (memcmp (buf, "aabcda\0\0\0\0", 10))
201 FAIL ();
203 strcpy (buf + 4, "EDCBA");
204 if (memcmp (buf, "aabcEDCBA", 10))
205 FAIL ();
207 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
208 FAIL ();
210 strncpy (buf + 6, "X", 4);
211 if (memcmp (buf, "aabcEDX\0\0", 10))
212 FAIL ();
214 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
215 FAIL ();
217 if (snprintf (buf + 7, 3, "%s", "987654") != 6
218 || memcmp (buf, "aabcEDX98", 10))
219 FAIL ();
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 ();
227 memcpy (buf, "abcdefghij", l0 + 10);
228 bcopy (buf, buf + 1, l0 + 9);
229 if (memcmp (buf, "aabcdefghi", 10))
230 FAIL ();
232 if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
233 || memcmp (buf, "aabcdabcde", 10))
234 FAIL ();
236 memset (buf + 8, 'j', l0 + 2);
237 if (memcmp (buf, "aabcdabcjj", 10))
238 FAIL ();
240 bzero (buf + 8, l0 + 2);
241 if (memcmp (buf, "aabcdabc\0\0", 10))
242 FAIL ();
244 explicit_bzero (buf + 6, l0 + 4);
245 if (memcmp (buf, "aabcda\0\0\0\0", 10))
246 FAIL ();
248 strcpy (buf + 4, str1 + 5);
249 if (memcmp (buf, "aabcEDCBA", 10))
250 FAIL ();
252 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
253 FAIL ();
255 strncpy (buf + 6, "X", l0 + 4);
256 if (memcmp (buf, "aabcEDX\0\0", 10))
257 FAIL ();
259 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
260 || memcmp (buf, "aabcEcd\0\0", 10))
261 FAIL ();
263 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
264 FAIL ();
266 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
267 FAIL ();
269 buf[l0 + 8] = '\0';
270 strcat (buf, "A");
271 if (memcmp (buf, "aabcEcd9A", 10))
272 FAIL ();
274 buf[l0 + 7] = '\0';
275 strncat (buf, "ZYXWV", l0 + 2);
276 if (memcmp (buf, "aabcEcdZY", 10))
277 FAIL ();
279 /* The following tests are supposed to succeed at all fortify
280 levels, even though they overflow a.buf1 into a.buf2. */
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 ();
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 ();
291 if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
292 != a.buf1 + 5 + MEMPCPY_RET ("abcde")
293 || memcmp (a.buf1, "aabcdabcde", 10))
294 FAIL ();
296 memset (a.buf1 + 8, 'j', l0 + 2);
297 if (memcmp (a.buf1, "aabcdabcjj", 10))
298 FAIL ();
300 bzero (a.buf1 + 8, l0 + 2);
301 if (memcmp (a.buf1, "aabcdabc\0\0", 10))
302 FAIL ();
304 explicit_bzero (a.buf1 + 6, l0 + 4);
305 if (memcmp (a.buf1, "aabcda\0\0\0\0", 10))
306 FAIL ();
308 #if __USE_FORTIFY_LEVEL < 2
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 ();
316 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
317 || memcmp (a.buf1, "aabcEDCBF", 10))
318 FAIL ();
320 strncpy (a.buf1 + 6, "X", l0 + 4);
321 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
322 FAIL ();
324 if (sprintf (a.buf1 + 7, "%d", num1) != 2
325 || memcmp (a.buf1, "aabcEDX67", 10))
326 FAIL ();
328 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
329 || memcmp (a.buf1, "aabcEDX98", 10))
330 FAIL ();
332 a.buf1[l0 + 8] = '\0';
333 strcat (a.buf1, "A");
334 if (memcmp (a.buf1, "aabcEDX9A", 10))
335 FAIL ();
337 a.buf1[l0 + 7] = '\0';
338 strncat (a.buf1, "ZYXWV", l0 + 2);
339 if (memcmp (a.buf1, "aabcEDXZY", 10))
340 FAIL ();
342 #endif
344 #if __USE_FORTIFY_LEVEL >= 1
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. */
349 CHK_FAIL_START
350 memcpy (buf + 1, "abcdefghij", 10);
351 CHK_FAIL_END
353 CHK_FAIL_START
354 memcpy (buf + 1, "abcdefghij", l0 + 10);
355 CHK_FAIL_END
357 CHK_FAIL_START
358 memmove (buf + 2, buf + 1, 9);
359 CHK_FAIL_END
361 CHK_FAIL_START
362 memmove (buf + 2, buf + 1, l0 + 9);
363 CHK_FAIL_END
365 CHK_FAIL_START
366 bcopy (buf + 1, buf + 2, 9);
367 CHK_FAIL_END
369 CHK_FAIL_START
370 bcopy (buf + 1, buf + 2, l0 + 9);
371 CHK_FAIL_END
373 #ifdef _GNU_SOURCE
374 CHK_FAIL_START
375 p = (char *) mempcpy (buf + 6, "abcde", 5);
376 CHK_FAIL_END
378 CHK_FAIL_START
379 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
380 CHK_FAIL_END
381 #endif
383 CHK_FAIL_START
384 memset (buf + 9, 'j', 2);
385 CHK_FAIL_END
387 CHK_FAIL_START
388 memset (buf + 9, 'j', l0 + 2);
389 CHK_FAIL_END
391 CHK_FAIL_START
392 bzero (buf + 9, 2);
393 CHK_FAIL_END
395 CHK_FAIL_START
396 bzero (buf + 9, l0 + 2);
397 CHK_FAIL_END
399 CHK_FAIL_START
400 explicit_bzero (buf + 9, 2);
401 CHK_FAIL_END
403 CHK_FAIL_START
404 explicit_bzero (buf + 9, l0 + 2);
405 CHK_FAIL_END
407 CHK_FAIL_START
408 strcpy (buf + 5, str1 + 5);
409 CHK_FAIL_END
411 CHK_FAIL_START
412 p = stpcpy (buf + 9, str2);
413 CHK_FAIL_END
415 CHK_FAIL_START
416 strncpy (buf + 7, "X", 4);
417 CHK_FAIL_END
419 CHK_FAIL_START
420 strncpy (buf + 7, "X", l0 + 4);
421 CHK_FAIL_END
423 CHK_FAIL_START
424 stpncpy (buf + 6, "cd", 5);
425 CHK_FAIL_END
427 CHK_FAIL_START
428 stpncpy (buf + 6, "cd", l0 + 5);
429 CHK_FAIL_END
431 CHK_FAIL_START
432 sprintf (buf + 8, "%d", num1);
433 CHK_FAIL_END
435 CHK_FAIL_START
436 snprintf (buf + 8, 3, "%d", num2);
437 CHK_FAIL_END
439 CHK_FAIL_START
440 snprintf (buf + 8, l0 + 3, "%d", num2);
441 CHK_FAIL_END
443 CHK_FAIL_START
444 swprintf (wbuf + 8, 3, L"%d", num1);
445 CHK_FAIL_END
447 CHK_FAIL_START
448 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
449 CHK_FAIL_END
451 memcpy (buf, str1 + 2, 9);
452 CHK_FAIL_START
453 strcat (buf, "AB");
454 CHK_FAIL_END
456 memcpy (buf, str1 + 3, 8);
457 CHK_FAIL_START
458 strncat (buf, "ZYXWV", 3);
459 CHK_FAIL_END
461 memcpy (buf, str1 + 3, 8);
462 CHK_FAIL_START
463 strncat (buf, "ZYXWV", l0 + 3);
464 CHK_FAIL_END
466 CHK_FAIL_START
467 memcpy (a.buf1 + 1, "abcdefghij", 10);
468 CHK_FAIL_END
470 CHK_FAIL_START
471 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
472 CHK_FAIL_END
474 CHK_FAIL_START
475 memmove (a.buf1 + 2, a.buf1 + 1, 9);
476 CHK_FAIL_END
478 CHK_FAIL_START
479 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
480 CHK_FAIL_END
482 CHK_FAIL_START
483 bcopy (a.buf1 + 1, a.buf1 + 2, 9);
484 CHK_FAIL_END
486 CHK_FAIL_START
487 bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
488 CHK_FAIL_END
490 #ifdef _GNU_SOURCE
491 CHK_FAIL_START
492 p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
493 CHK_FAIL_END
495 CHK_FAIL_START
496 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
497 CHK_FAIL_END
498 #endif
500 CHK_FAIL_START
501 memset (a.buf1 + 9, 'j', 2);
502 CHK_FAIL_END
504 CHK_FAIL_START
505 memset (a.buf1 + 9, 'j', l0 + 2);
506 CHK_FAIL_END
508 CHK_FAIL_START
509 bzero (a.buf1 + 9, 2);
510 CHK_FAIL_END
512 CHK_FAIL_START
513 bzero (a.buf1 + 9, l0 + 2);
514 CHK_FAIL_END
516 CHK_FAIL_START
517 explicit_bzero (a.buf1 + 9, 2);
518 CHK_FAIL_END
520 CHK_FAIL_START
521 explicit_bzero (a.buf1 + 9, l0 + 2);
522 CHK_FAIL_END
524 # if __USE_FORTIFY_LEVEL >= 2
525 # define O 0
526 # else
527 # define O 1
528 # endif
530 CHK_FAIL_START
531 strcpy (a.buf1 + (O + 4), str1 + 5);
532 CHK_FAIL_END
534 CHK_FAIL_START
535 p = stpcpy (a.buf1 + (O + 8), str2);
536 CHK_FAIL_END
538 CHK_FAIL_START
539 strncpy (a.buf1 + (O + 6), "X", 4);
540 CHK_FAIL_END
542 CHK_FAIL_START
543 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
544 CHK_FAIL_END
546 CHK_FAIL_START
547 strlcpy (a.buf1 + (O + 6), "X", 4);
548 CHK_FAIL_END
550 CHK_FAIL_START
551 strlcpy (a.buf1 + (O + 6), "X", l0 + 4);
552 CHK_FAIL_END
555 char *volatile buf2 = buf;
556 if (strlcpy (buf2, "a", sizeof (buf) + 1) != 1)
557 FAIL ();
560 CHK_FAIL_START
561 sprintf (a.buf1 + (O + 7), "%d", num1);
562 CHK_FAIL_END
564 CHK_FAIL_START
565 snprintf (a.buf1 + (O + 7), 3, "%d", num2);
566 CHK_FAIL_END
568 CHK_FAIL_START
569 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
570 CHK_FAIL_END
572 memcpy (a.buf1, str1 + (3 - O), 8 + O);
573 CHK_FAIL_START
574 strcat (a.buf1, "AB");
575 CHK_FAIL_END
577 memcpy (a.buf1, str1 + (4 - O), 7 + O);
578 CHK_FAIL_START
579 strncat (a.buf1, "ZYXWV", l0 + 3);
580 CHK_FAIL_END
582 memset (a.buf1, 0, sizeof (a.buf1));
583 CHK_FAIL_START
584 strlcat (a.buf1 + (O + 6), "X", 4);
585 CHK_FAIL_END
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
593 buf[0] = '\0';
594 char *volatile buf2 = buf;
595 if (strlcat (buf2, "a", sizeof (buf) + 1) != 1)
596 FAIL ();
598 #endif
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 ();
607 if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
608 || wmemcmp (wbuf, L"aabcdabcde", 10))
609 FAIL ();
611 wmemset (wbuf + 8, L'j', 2);
612 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
613 FAIL ();
615 wcscpy (wbuf + 4, L"EDCBA");
616 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
617 FAIL ();
619 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
620 FAIL ();
622 wcsncpy (wbuf + 6, L"X", 4);
623 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
624 FAIL ();
626 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
627 || wmemcmp (wbuf, L"aabcEDX98", 10))
628 FAIL ();
630 if (swprintf (wbuf + 7, 3, L"64") != 2
631 || wmemcmp (wbuf, L"aabcEDX64", 10))
632 FAIL ();
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 ();
640 if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
641 != wbuf + 5 + WMEMPCPY_RET (L"abcde")
642 || wmemcmp (wbuf, L"aabcdabcde", 10))
643 FAIL ();
645 wmemset (wbuf + 8, L'j', l0 + 2);
646 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
647 FAIL ();
649 wcscpy (wbuf + 4, wstr1 + 5);
650 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
651 FAIL ();
653 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
654 FAIL ();
656 wcsncpy (wbuf + 6, L"X", l0 + 4);
657 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
658 FAIL ();
660 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
661 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
662 FAIL ();
664 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
665 || wmemcmp (wbuf, L"aabcEcd98", 10))
666 FAIL ();
668 wbuf[l0 + 8] = L'\0';
669 wcscat (wbuf, L"A");
670 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
671 FAIL ();
673 wbuf[l0 + 7] = L'\0';
674 wcsncat (wbuf, L"ZYXWV", l0 + 2);
675 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
676 FAIL ();
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 ();
683 if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
684 != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
685 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
686 FAIL ();
688 wmemset (wa.buf1 + 8, L'j', l0 + 2);
689 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
690 FAIL ();
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 ();
700 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
701 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
702 FAIL ();
704 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
705 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
706 FAIL ();
708 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
709 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
710 FAIL ();
712 wa.buf1[l0 + 8] = L'\0';
713 wcscat (wa.buf1, L"A");
714 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
715 FAIL ();
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 ();
722 #endif
724 #if __USE_FORTIFY_LEVEL >= 1
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. */
729 CHK_FAIL_START
730 wmemcpy (wbuf + 1, L"abcdefghij", 10);
731 CHK_FAIL_END
733 CHK_FAIL_START
734 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
735 CHK_FAIL_END
737 CHK_FAIL_START
738 wmemcpy (wbuf + 9, L"abcdefghij", 10);
739 CHK_FAIL_END
741 CHK_FAIL_START
742 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
743 CHK_FAIL_END
745 CHK_FAIL_START
746 wmemmove (wbuf + 2, wbuf + 1, 9);
747 CHK_FAIL_END
749 CHK_FAIL_START
750 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
751 CHK_FAIL_END
753 #ifdef _GNU_SOURCE
754 CHK_FAIL_START
755 wp = wmempcpy (wbuf + 6, L"abcde", 5);
756 CHK_FAIL_END
758 CHK_FAIL_START
759 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
760 CHK_FAIL_END
761 #endif
763 CHK_FAIL_START
764 wmemset (wbuf + 9, L'j', 2);
765 CHK_FAIL_END
767 CHK_FAIL_START
768 wmemset (wbuf + 9, L'j', l0 + 2);
769 CHK_FAIL_END
771 CHK_FAIL_START
772 wcscpy (wbuf + 5, wstr1 + 5);
773 CHK_FAIL_END
775 CHK_FAIL_START
776 wp = wcpcpy (wbuf + 9, wstr2);
777 CHK_FAIL_END
779 CHK_FAIL_START
780 wcsncpy (wbuf + 7, L"X", 4);
781 CHK_FAIL_END
783 CHK_FAIL_START
784 wcsncpy (wbuf + 7, L"X", l0 + 4);
785 CHK_FAIL_END
787 CHK_FAIL_START
788 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
789 CHK_FAIL_END
791 CHK_FAIL_START
792 wcslcpy (wbuf + 7, L"X", 4);
793 CHK_FAIL_END
795 CHK_FAIL_START
796 wcslcpy (wbuf + 7, L"X", l0 + 4);
797 CHK_FAIL_END
799 CHK_FAIL_START
800 wcslcpy (wbuf + 9, L"XABCDEFGH", 8);
801 CHK_FAIL_END
803 CHK_FAIL_START
804 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
805 CHK_FAIL_END
807 CHK_FAIL_START
808 wcpncpy (wbuf + 6, L"cd", 5);
809 CHK_FAIL_END
811 CHK_FAIL_START
812 wcpncpy (wbuf + 6, L"cd", l0 + 5);
813 CHK_FAIL_END
815 wmemcpy (wbuf, wstr1 + 2, 9);
816 CHK_FAIL_START
817 wcscat (wbuf, L"AB");
818 CHK_FAIL_END
820 wmemcpy (wbuf, wstr1 + 3, 8);
821 CHK_FAIL_START
822 wcsncat (wbuf, L"ZYXWV", l0 + 3);
823 CHK_FAIL_END
825 wmemcpy (wbuf, wstr1 + 4, 7);
826 CHK_FAIL_START
827 wcslcat (wbuf, L"ZYXWV", l0 + 11);
828 CHK_FAIL_END
830 CHK_FAIL_START
831 wmemcpy (wa.buf1 + 1, L"abcdefghij", 10);
832 CHK_FAIL_END
834 CHK_FAIL_START
835 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
836 CHK_FAIL_END
838 CHK_FAIL_START
839 wmemmove (wa.buf1 + 2, wa.buf1 + 1, 9);
840 CHK_FAIL_END
842 CHK_FAIL_START
843 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
844 CHK_FAIL_END
846 #ifdef _GNU_SOURCE
847 CHK_FAIL_START
848 wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
849 CHK_FAIL_END
851 CHK_FAIL_START
852 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
853 CHK_FAIL_END
854 #endif
856 CHK_FAIL_START
857 wmemset (wa.buf1 + 9, L'j', 2);
858 CHK_FAIL_END
860 CHK_FAIL_START
861 wmemset (wa.buf1 + 9, L'j', l0 + 2);
862 CHK_FAIL_END
864 #if __USE_FORTIFY_LEVEL >= 2
865 # define O 0
866 #else
867 # define O 1
868 #endif
870 CHK_FAIL_START
871 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
872 CHK_FAIL_END
874 CHK_FAIL_START
875 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
876 CHK_FAIL_END
878 CHK_FAIL_START
879 wcsncpy (wa.buf1 + (O + 6), L"X", 4);
880 CHK_FAIL_END
882 CHK_FAIL_START
883 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
884 CHK_FAIL_END
886 wmemcpy (wa.buf1, wstr1 + (3 - O), 8 + O);
887 CHK_FAIL_START
888 wcscat (wa.buf1, L"AB");
889 CHK_FAIL_END
891 wmemcpy (wa.buf1, wstr1 + (4 - O), 7 + O);
892 CHK_FAIL_START
893 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
894 CHK_FAIL_END
895 #endif
898 /* Now checks for %n protection. */
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 ();
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 ();
913 if (dprintf (temp_fd_dprintf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
914 || n1 != 1 || n2 != 2)
915 FAIL ();
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
925 CHK_FAIL2_START
926 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
927 FAIL ();
928 CHK_FAIL2_END
930 CHK_FAIL2_START
931 if (dprintf (temp_fd_dprintf, buf2, str2, &n1, str2, &n1) != 2)
932 FAIL ();
933 CHK_FAIL2_END
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 ();
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 ();
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 ();
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
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 ();
967 FILE *fp = stdout;
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 ();
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 ();
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
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 ();
995 #ifdef _GNU_SOURCE
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
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);
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);
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);
1025 #endif
1027 if (freopen (temp_filename, "r", stdin) == NULL)
1029 puts ("could not open temporary file");
1030 exit (1);
1033 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
1034 FAIL ();
1035 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
1036 FAIL ();
1038 #if __USE_FORTIFY_LEVEL >= 1
1039 CHK_FAIL_START
1040 if (gets (buf) != buf)
1041 FAIL ();
1042 CHK_FAIL_END
1043 #endif
1045 rewind (stdin);
1047 if (fgets (buf, buf_size, stdin) != buf
1048 || memcmp (buf, "abcdefgh\n", 10))
1049 FAIL ();
1050 if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
1051 FAIL ();
1053 rewind (stdin);
1055 if (fgets (buf, l0 + buf_size, stdin) != buf
1056 || memcmp (buf, "abcdefgh\n", 10))
1057 FAIL ();
1059 #if __USE_FORTIFY_LEVEL >= 1
1060 CHK_FAIL_START
1061 if (fgets (buf, buf_size + 1, stdin) != buf)
1062 FAIL ();
1063 CHK_FAIL_END
1065 CHK_FAIL_START
1066 if (fgets (buf, l0 + buf_size + 1, stdin) != buf)
1067 FAIL ();
1068 CHK_FAIL_END
1069 #endif
1071 rewind (stdin);
1073 #ifdef _GNU_SOURCE
1074 if (fgets_unlocked (buf, buf_size, stdin) != buf
1075 || memcmp (buf, "abcdefgh\n", 10))
1076 FAIL ();
1077 if (fgets_unlocked (buf, buf_size, stdin) != buf
1078 || memcmp (buf, "ABCDEFGHI", 10))
1079 FAIL ();
1081 rewind (stdin);
1083 if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf
1084 || memcmp (buf, "abcdefgh\n", 10))
1085 FAIL ();
1087 #if __USE_FORTIFY_LEVEL >= 1
1088 CHK_FAIL_START
1089 if (fgets_unlocked (buf, buf_size + 1, stdin) != buf)
1090 FAIL ();
1091 CHK_FAIL_END
1093 CHK_FAIL_START
1094 if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf)
1095 FAIL ();
1096 CHK_FAIL_END
1097 #endif
1099 rewind (stdin);
1100 #endif
1102 if (fread (buf, 1, buf_size, stdin) != buf_size
1103 || memcmp (buf, "abcdefgh\nA", 10))
1104 FAIL ();
1105 if (fread (buf, buf_size, 1, stdin) != 1
1106 || memcmp (buf, "BCDEFGHI\na", 10))
1107 FAIL ();
1109 rewind (stdin);
1111 if (fread (buf, l0 + 1, buf_size, stdin) != buf_size
1112 || memcmp (buf, "abcdefgh\nA", 10))
1113 FAIL ();
1114 if (fread (buf, buf_size, l0 + 1, stdin) != 1
1115 || memcmp (buf, "BCDEFGHI\na", 10))
1116 FAIL ();
1118 #if __USE_FORTIFY_LEVEL >= 1
1119 CHK_FAIL_START
1120 if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1)
1121 FAIL ();
1122 CHK_FAIL_END
1124 CHK_FAIL_START
1125 if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1)
1126 FAIL ();
1127 CHK_FAIL_END
1128 #endif
1130 rewind (stdin);
1132 if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size
1133 || memcmp (buf, "abcdefgh\nA", 10))
1134 FAIL ();
1135 if (fread_unlocked (buf, buf_size, 1, stdin) != 1
1136 || memcmp (buf, "BCDEFGHI\na", 10))
1137 FAIL ();
1139 rewind (stdin);
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 ();
1148 rewind (stdin);
1150 if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size
1151 || memcmp (buf, "abcdefgh\nA", 10))
1152 FAIL ();
1153 if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1
1154 || memcmp (buf, "BCDEFGHI\na", 10))
1155 FAIL ();
1157 #if __USE_FORTIFY_LEVEL >= 1
1158 CHK_FAIL_START
1159 if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1)
1160 FAIL ();
1161 CHK_FAIL_END
1163 CHK_FAIL_START
1164 if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1)
1165 FAIL ();
1166 CHK_FAIL_END
1167 #endif
1169 lseek (fileno (stdin), 0, SEEK_SET);
1171 if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
1172 || memcmp (buf, "abcdefgh\n", 9))
1173 FAIL ();
1174 if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
1175 || memcmp (buf, "ABCDEFGHI", 9))
1176 FAIL ();
1178 lseek (fileno (stdin), 0, SEEK_SET);
1180 if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1
1181 || memcmp (buf, "abcdefgh\n", 9))
1182 FAIL ();
1184 #if __USE_FORTIFY_LEVEL >= 1
1185 CHK_FAIL_START
1186 if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1)
1187 FAIL ();
1188 CHK_FAIL_END
1190 CHK_FAIL_START
1191 if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1)
1192 FAIL ();
1193 CHK_FAIL_END
1194 #endif
1196 if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2)
1197 != buf_size - 1
1198 || memcmp (buf, "\nABCDEFGH", 9))
1199 FAIL ();
1200 if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
1201 || memcmp (buf, "abcdefgh\n", 9))
1202 FAIL ();
1203 if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
1204 != buf_size - 1
1205 || memcmp (buf, "h\nABCDEFG", 9))
1206 FAIL ();
1208 #if __USE_FORTIFY_LEVEL >= 1
1209 CHK_FAIL_START
1210 if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
1211 != buf_size + 1)
1212 FAIL ();
1213 CHK_FAIL_END
1215 CHK_FAIL_START
1216 if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
1217 != buf_size + 1)
1218 FAIL ();
1219 CHK_FAIL_END
1220 #endif
1222 if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2)
1223 != buf_size - 1
1224 || memcmp (buf, "\nABCDEFGH", 9))
1225 FAIL ();
1226 if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
1227 || memcmp (buf, "abcdefgh\n", 9))
1228 FAIL ();
1229 if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
1230 != buf_size - 1
1231 || memcmp (buf, "h\nABCDEFG", 9))
1232 FAIL ();
1234 #if __USE_FORTIFY_LEVEL >= 1
1235 CHK_FAIL_START
1236 if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
1237 != buf_size + 1)
1238 FAIL ();
1239 CHK_FAIL_END
1241 CHK_FAIL_START
1242 if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
1243 != buf_size + 1)
1244 FAIL ();
1245 CHK_FAIL_END
1246 #endif
1248 if (freopen (temp_filename, "r", stdin) == NULL)
1250 puts ("could not open temporary file");
1251 exit (1);
1254 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
1256 puts ("could not seek in test file");
1257 exit (1);
1260 #if __USE_FORTIFY_LEVEL >= 1
1261 CHK_FAIL_START
1262 if (gets (buf) != buf)
1263 FAIL ();
1264 CHK_FAIL_END
1265 #endif
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
1272 CHK_FAIL2_START
1273 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
1274 CHK_FAIL2_END
1276 CHK_FAIL2_START
1277 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
1278 CHK_FAIL2_END
1280 CHK_FAIL2_START
1281 snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
1282 CHK_FAIL2_END
1284 CHK_FAIL2_START
1285 dprintf (temp_fd_dprintf, "%3$d\n", 1, 2, 3, 4);
1286 CHK_FAIL2_END
1288 int sp[2];
1289 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
1290 FAIL ();
1291 else
1293 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
1294 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
1295 != strlen (sendstr))
1296 FAIL ();
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 ();
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 ();
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
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
1323 socklen_t sl;
1324 struct sockaddr_un sa_un;
1326 sl = sizeof (sa_un);
1327 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
1328 (struct sockaddr *) &sa_un, &sl)
1329 != sizeof recvbuf
1330 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1331 FAIL ();
1333 sl = sizeof (sa_un);
1334 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
1335 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
1336 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1337 FAIL ();
1339 #if __USE_FORTIFY_LEVEL >= 1
1340 CHK_FAIL_START
1341 sl = sizeof (sa_un);
1342 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1343 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1344 FAIL ();
1345 CHK_FAIL_END
1347 CHK_FAIL_START
1348 sl = sizeof (sa_un);
1349 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1350 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1351 FAIL ();
1352 CHK_FAIL_END
1353 #endif
1355 close (sp[0]);
1356 close (sp[1]);
1359 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1360 char *enddir = strchr (fname, '\0');
1361 if (mkdtemp (fname) == NULL)
1363 printf ("mkdtemp failed: %m\n");
1364 return 1;
1366 *enddir = '/';
1367 if (symlink ("bar", fname) != 0)
1368 FAIL ();
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 ();
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
1384 CHK_FAIL_START
1385 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1386 FAIL ();
1387 CHK_FAIL_END
1388 #endif
1390 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1391 if (tmpfd < 0)
1392 FAIL ();
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 ();
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
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
1416 close (tmpfd);
1418 char *cwd1 = getcwd (NULL, 0);
1419 if (cwd1 == NULL)
1420 FAIL ();
1422 char *cwd2 = getcwd (NULL, 250);
1423 if (cwd2 == NULL)
1424 FAIL ();
1426 if (cwd1 && cwd2)
1428 if (strcmp (cwd1, cwd2) != 0)
1429 FAIL ();
1431 *enddir = '\0';
1432 if (chdir (fname))
1433 FAIL ();
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
1443 char getcwdbuf[sizeof fname - 3];
1445 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1446 if (cwd4 != getcwdbuf
1447 || strcmp (getcwdbuf, fname) != 0)
1448 FAIL ();
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 ();
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
1463 CHK_FAIL_START
1464 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1465 != getcwdbuf + 2)
1466 FAIL ();
1467 CHK_FAIL_END
1468 #endif
1470 if (getwd (getcwdbuf) != getcwdbuf
1471 || strcmp (getcwdbuf, fname) != 0)
1472 FAIL ();
1474 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1475 || strcmp (getcwdbuf + 1, fname) != 0)
1476 FAIL ();
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
1486 if (chdir (cwd1) != 0)
1487 FAIL ();
1488 free (cwd3);
1491 free (cwd1);
1492 free (cwd2);
1493 *enddir = '/';
1494 if (unlink (fname) != 0)
1495 FAIL ();
1497 *enddir = '\0';
1498 if (rmdir (fname) != 0)
1499 FAIL ();
1502 #if PATH_MAX > 0
1503 char largebuf[PATH_MAX];
1504 char *realres = realpath (".", largebuf);
1505 if (realres != largebuf)
1506 FAIL ();
1508 # if __USE_FORTIFY_LEVEL >= 1
1509 CHK_FAIL_START
1510 char realbuf[1];
1511 realres = realpath (".", realbuf);
1512 if (realres != realbuf)
1513 FAIL ();
1514 CHK_FAIL_END
1515 # endif
1516 #endif
1518 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1520 assert (MB_CUR_MAX <= 10);
1522 /* First a simple test. */
1523 char enough[10];
1524 if (wctomb (enough, L'A') != 1)
1525 FAIL ();
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)
1534 FAIL ();
1535 CHK_FAIL_END
1536 #endif
1538 mbstate_t s;
1539 memset (&s, '\0', sizeof (s));
1540 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1541 FAIL ();
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
1548 char smallbuf[1];
1549 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1550 FAIL ();
1551 CHK_FAIL_END
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 ();
1557 #endif
1559 wchar_t wenough[10];
1560 memset (&s, '\0', sizeof (s));
1561 const char *cp = "A";
1562 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1563 || wcscmp (wenough, L"A") != 0)
1564 FAIL ();
1566 cp = "BC";
1567 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1568 || wcscmp (wenough, L"BC") != 0)
1569 FAIL ();
1571 #if __USE_FORTIFY_LEVEL >= 1
1572 CHK_FAIL_START
1573 wchar_t wsmallbuf[2];
1574 cp = "ABC";
1575 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1576 CHK_FAIL_END
1577 #endif
1579 /* Bug 29030 regression check */
1580 cp = "HelloWorld";
1581 if (mbsrtowcs (NULL, &cp, (size_t)-1, &s) != 10)
1582 FAIL ();
1584 cp = "A";
1585 if (mbstowcs (wenough, cp, 10) != 1
1586 || wcscmp (wenough, L"A") != 0)
1587 FAIL ();
1589 cp = "DEF";
1590 if (mbstowcs (wenough, cp, l0 + 10) != 3
1591 || wcscmp (wenough, L"DEF") != 0)
1592 FAIL ();
1594 #if __USE_FORTIFY_LEVEL >= 1
1595 CHK_FAIL_START
1596 wchar_t wsmallbuf[2];
1597 cp = "ABC";
1598 mbstowcs (wsmallbuf, cp, 10);
1599 CHK_FAIL_END
1600 #endif
1602 memset (&s, '\0', sizeof (s));
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 ();
1609 cp = "IJ";
1610 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1611 || wcscmp (wenough, L"IEF") != 0)
1612 FAIL ();
1614 #if __USE_FORTIFY_LEVEL >= 1
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
1622 memset (&s, '\0', sizeof (s));
1623 const wchar_t *wcp = L"A";
1624 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1625 || strcmp (enough, "A") != 0)
1626 FAIL ();
1628 wcp = L"BC";
1629 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1630 || strcmp (enough, "BC") != 0)
1631 FAIL ();
1633 #if __USE_FORTIFY_LEVEL >= 1
1634 CHK_FAIL_START
1635 char smallbuf[2];
1636 wcp = L"ABC";
1637 wcsrtombs (smallbuf, &wcp, 10, &s);
1638 CHK_FAIL_END
1639 #endif
1641 memset (enough, 'Z', sizeof (enough));
1642 wcp = L"EF";
1643 if (wcstombs (enough, wcp, 10) != 2
1644 || strcmp (enough, "EF") != 0)
1645 FAIL ();
1647 wcp = L"G";
1648 if (wcstombs (enough, wcp, l0 + 10) != 1
1649 || strcmp (enough, "G") != 0)
1650 FAIL ();
1652 #if __USE_FORTIFY_LEVEL >= 1
1653 CHK_FAIL_START
1654 char smallbuf[2];
1655 wcp = L"ABC";
1656 wcstombs (smallbuf, wcp, 10);
1657 CHK_FAIL_END
1658 #endif
1660 memset (&s, '\0', sizeof (s));
1661 wcp = L"AB";
1662 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1663 || strcmp (enough, "A") != 0)
1664 FAIL ();
1666 wcp = L"BCD";
1667 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1668 || strcmp (enough, "B") != 0)
1669 FAIL ();
1671 #if __USE_FORTIFY_LEVEL >= 1
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
1679 else
1681 puts ("cannot set locale");
1682 ret = 1;
1685 int fd;
1687 #ifdef _GNU_SOURCE
1688 fd = posix_openpt (O_RDWR);
1689 if (fd != -1)
1691 char enough[1000];
1692 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1693 FAIL ();
1695 #if __USE_FORTIFY_LEVEL >= 1
1696 CHK_FAIL_START
1697 char smallbuf[2];
1698 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1699 FAIL ();
1700 CHK_FAIL_END
1701 #endif
1702 close (fd);
1704 #endif
1706 #if PATH_MAX > 0
1707 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1708 # if __USE_FORTIFY_LEVEL >= 1
1709 CHK_FAIL_START
1710 char smallbuf[1];
1711 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1712 CHK_FAIL_END
1713 # endif
1714 #endif
1716 gid_t grpslarge[5];
1717 int ngr = getgroups (5, grpslarge);
1718 asm volatile ("" : : "r" (ngr));
1719 #if __USE_FORTIFY_LEVEL >= 1
1720 CHK_FAIL_START
1721 char smallbuf[1];
1722 ngr = getgroups (5, (gid_t *) smallbuf);
1723 asm volatile ("" : : "r" (ngr));
1724 CHK_FAIL_END
1725 #endif
1727 fd = open (_PATH_TTY, O_RDONLY);
1728 if (fd != -1)
1730 char enough[1000];
1731 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1732 FAIL ();
1734 #if __USE_FORTIFY_LEVEL >= 1
1735 CHK_FAIL_START
1736 char smallbuf[2];
1737 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1738 FAIL ();
1739 CHK_FAIL_END
1740 #endif
1741 close (fd);
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
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
1762 char domainnamelarge[1000];
1763 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1764 asm volatile ("" : : "r" (res));
1765 #if __USE_FORTIFY_LEVEL >= 1
1766 CHK_FAIL_START
1767 char smallbuf[1];
1768 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1769 asm volatile ("" : : "r" (res));
1770 CHK_FAIL_END
1771 #endif
1773 fd_set s;
1774 FD_ZERO (&s);
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
1782 CHK_FAIL_START
1783 FD_SET (l0 + FD_SETSIZE, &s);
1784 CHK_FAIL_END
1785 #endif
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
1793 CHK_FAIL_START
1794 FD_SET (l0 + FD_SETSIZE, &s);
1795 CHK_FAIL_END
1796 #endif
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
1804 CHK_FAIL_START
1805 FD_ISSET (l0 + FD_SETSIZE, &s);
1806 CHK_FAIL_END
1807 #endif
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
1818 CHK_FAIL_START
1819 poll (fds, l0 + 2, 0);
1820 CHK_FAIL_END
1821 #endif
1822 #ifdef _GNU_SOURCE
1823 ppoll (fds, 1, NULL, NULL);
1824 # if __USE_FORTIFY_LEVEL >= 1
1825 CHK_FAIL_START
1826 ppoll (fds, 2, NULL, NULL);
1827 CHK_FAIL_END
1829 CHK_FAIL_START
1830 ppoll (fds, l0 + 2, NULL, NULL);
1831 CHK_FAIL_END
1832 # endif
1833 #endif
1835 return ret;
1838 #include <support/test-driver.c>