1 /* Copyright (C) 2004-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
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 <http://www.gnu.org/licenses/>. */
32 #include <sys/select.h>
33 #include <sys/socket.h>
37 #define obstack_chunk_alloc malloc
38 #define obstack_chunk_free free
41 static void do_prepare (void);
42 static int do_test (void);
43 #define PREPARE(argc, argv) do_prepare ()
44 #define TEST_FUNCTION do_test ()
45 #include "../test-skeleton.c"
50 int temp_fd
= create_temp_file ("tst-chk1.", &temp_filename
);
53 printf ("cannot create temporary file: %m\n");
57 const char *strs
= "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
58 if ((size_t) write (temp_fd
, strs
, strlen (strs
)) != strlen (strs
))
60 puts ("could not write test strings into file");
61 unlink (temp_filename
);
66 volatile int chk_fail_ok
;
76 longjmp (chk_fail_buf
, 1);
87 const char *str1
= "JIHGFEDCBA";
88 const char *str2
= "F";
89 const char *str3
= "%s%n%s%n";
90 const char *str4
= "Hello, ";
91 const char *str5
= "World!\n";
92 const wchar_t *wstr1
= L
"JIHGFEDCBA";
93 const wchar_t *wstr2
= L
"F";
94 const wchar_t *wstr3
= L
"%s%n%s%n";
95 const wchar_t *wstr4
= L
"Hello, ";
96 const wchar_t *wstr5
= L
"World!\n";
102 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
103 #define CHK_FAIL_START \
105 if (! setjmp (chk_fail_buf)) \
107 #define CHK_FAIL_END \
111 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
112 # define CHK_FAIL2_START CHK_FAIL_START
113 # define CHK_FAIL2_END CHK_FAIL_END
115 # define CHK_FAIL2_START
116 # define CHK_FAIL2_END
123 sa
.sa_handler
= handler
;
125 sigemptyset (&sa
.sa_mask
);
127 sigaction (SIGABRT
, &sa
, NULL
);
129 /* Avoid all the buffer overflow messages on stderr. */
130 int fd
= open (_PATH_DEVNULL
, O_WRONLY
);
132 close (STDERR_FILENO
);
135 dup2 (fd
, STDERR_FILENO
);
138 setenv ("LIBC_FATAL_STDERR_", "1", 1);
140 struct A
{ char buf1
[9]; char buf2
[1]; } a
;
141 struct wA
{ wchar_t buf1
[9]; wchar_t buf2
[1]; } wa
;
143 printf ("Test checking routines at fortify level %d\n",
144 #ifdef __USE_FORTIFY_LEVEL
145 (int) __USE_FORTIFY_LEVEL
151 #if defined __USE_FORTIFY_LEVEL && !defined __fortify_function
152 printf ("Test skipped");
157 /* These ops can be done without runtime checking of object size. */
158 memcpy (buf
, "abcdefghij", 10);
159 memmove (buf
+ 1, buf
, 9);
160 if (memcmp (buf
, "aabcdefghi", 10))
163 if (mempcpy (buf
+ 5, "abcde", 5) != buf
+ 10
164 || memcmp (buf
, "aabcdabcde", 10))
167 memset (buf
+ 8, 'j', 2);
168 if (memcmp (buf
, "aabcdabcjj", 10))
171 strcpy (buf
+ 4, "EDCBA");
172 if (memcmp (buf
, "aabcEDCBA", 10))
175 if (stpcpy (buf
+ 8, "F") != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
178 strncpy (buf
+ 6, "X", 4);
179 if (memcmp (buf
, "aabcEDX\0\0", 10))
182 if (sprintf (buf
+ 7, "%s", "67") != 2 || memcmp (buf
, "aabcEDX67", 10))
185 if (snprintf (buf
+ 7, 3, "%s", "987654") != 6
186 || memcmp (buf
, "aabcEDX98", 10))
189 /* These ops need runtime checking, but shouldn't __chk_fail. */
190 memcpy (buf
, "abcdefghij", l0
+ 10);
191 memmove (buf
+ 1, buf
, l0
+ 9);
192 if (memcmp (buf
, "aabcdefghi", 10))
195 if (mempcpy (buf
+ 5, "abcde", l0
+ 5) != buf
+ 10
196 || memcmp (buf
, "aabcdabcde", 10))
199 memset (buf
+ 8, 'j', l0
+ 2);
200 if (memcmp (buf
, "aabcdabcjj", 10))
203 strcpy (buf
+ 4, str1
+ 5);
204 if (memcmp (buf
, "aabcEDCBA", 10))
207 if (stpcpy (buf
+ 8, str2
) != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
210 strncpy (buf
+ 6, "X", l0
+ 4);
211 if (memcmp (buf
, "aabcEDX\0\0", 10))
214 if (stpncpy (buf
+ 5, "cd", l0
+ 5) != buf
+ 7
215 || memcmp (buf
, "aabcEcd\0\0", 10))
218 if (sprintf (buf
+ 7, "%d", num1
) != 2 || memcmp (buf
, "aabcEcd67", 10))
221 if (snprintf (buf
+ 7, 3, "%d", num2
) != 6 || memcmp (buf
, "aabcEcd98", 10))
226 if (memcmp (buf
, "aabcEcd9A", 10))
230 strncat (buf
, "ZYXWV", l0
+ 2);
231 if (memcmp (buf
, "aabcEcdZY", 10))
234 memcpy (a
.buf1
, "abcdefghij", l0
+ 10);
235 memmove (a
.buf1
+ 1, a
.buf1
, l0
+ 9);
236 if (memcmp (a
.buf1
, "aabcdefghi", 10))
239 if (mempcpy (a
.buf1
+ 5, "abcde", l0
+ 5) != a
.buf1
+ 10
240 || memcmp (a
.buf1
, "aabcdabcde", 10))
243 memset (a
.buf1
+ 8, 'j', l0
+ 2);
244 if (memcmp (a
.buf1
, "aabcdabcjj", 10))
247 #if __USE_FORTIFY_LEVEL < 2
248 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
249 and sufficient GCC support, as the string operations overflow
250 from a.buf1 into a.buf2. */
251 strcpy (a
.buf1
+ 4, str1
+ 5);
252 if (memcmp (a
.buf1
, "aabcEDCBA", 10))
255 if (stpcpy (a
.buf1
+ 8, str2
) != a
.buf1
+ 9
256 || memcmp (a
.buf1
, "aabcEDCBF", 10))
259 strncpy (a
.buf1
+ 6, "X", l0
+ 4);
260 if (memcmp (a
.buf1
, "aabcEDX\0\0", 10))
263 if (sprintf (a
.buf1
+ 7, "%d", num1
) != 2
264 || memcmp (a
.buf1
, "aabcEDX67", 10))
267 if (snprintf (a
.buf1
+ 7, 3, "%d", num2
) != 6
268 || memcmp (a
.buf1
, "aabcEDX98", 10))
271 a
.buf1
[l0
+ 8] = '\0';
272 strcat (a
.buf1
, "A");
273 if (memcmp (a
.buf1
, "aabcEDX9A", 10))
276 a
.buf1
[l0
+ 7] = '\0';
277 strncat (a
.buf1
, "ZYXWV", l0
+ 2);
278 if (memcmp (a
.buf1
, "aabcEDXZY", 10))
283 #if __USE_FORTIFY_LEVEL >= 1
284 /* Now check if all buffer overflows are caught at runtime. */
287 memcpy (buf
+ 1, "abcdefghij", l0
+ 10);
291 memmove (buf
+ 2, buf
+ 1, l0
+ 9);
295 p
= (char *) mempcpy (buf
+ 6, "abcde", l0
+ 5);
299 memset (buf
+ 9, 'j', l0
+ 2);
303 strcpy (buf
+ 5, str1
+ 5);
307 p
= stpcpy (buf
+ 9, str2
);
311 strncpy (buf
+ 7, "X", l0
+ 4);
315 stpncpy (buf
+ 6, "cd", l0
+ 5);
318 # if !defined __cplusplus || defined __va_arg_pack
320 sprintf (buf
+ 8, "%d", num1
);
324 snprintf (buf
+ 8, l0
+ 3, "%d", num2
);
328 swprintf (wbuf
+ 8, 3, L
"%d", num1
);
332 swprintf (wbuf
+ 8, l0
+ 3, L
"%d", num1
);
336 memcpy (buf
, str1
+ 2, l0
+ 9);
341 memcpy (buf
, str1
+ 3, l0
+ 8);
343 strncat (buf
, "ZYXWV", l0
+ 3);
347 memcpy (a
.buf1
+ 1, "abcdefghij", l0
+ 10);
351 memmove (a
.buf1
+ 2, a
.buf1
+ 1, l0
+ 9);
355 p
= (char *) mempcpy (a
.buf1
+ 6, "abcde", l0
+ 5);
359 memset (a
.buf1
+ 9, 'j', l0
+ 2);
362 # if __USE_FORTIFY_LEVEL >= 2
369 strcpy (a
.buf1
+ (O
+ 4), str1
+ 5);
373 p
= stpcpy (a
.buf1
+ (O
+ 8), str2
);
377 strncpy (a
.buf1
+ (O
+ 6), "X", l0
+ 4);
380 # if !defined __cplusplus || defined __va_arg_pack
382 sprintf (a
.buf1
+ (O
+ 7), "%d", num1
);
386 snprintf (a
.buf1
+ (O
+ 7), l0
+ 3, "%d", num2
);
390 memcpy (a
.buf1
, str1
+ (3 - O
), l0
+ 8 + O
);
392 strcat (a
.buf1
, "AB");
395 memcpy (a
.buf1
, str1
+ (4 - O
), l0
+ 7 + O
);
397 strncat (a
.buf1
, "ZYXWV", l0
+ 3);
402 /* These ops can be done without runtime checking of object size. */
403 wmemcpy (wbuf
, L
"abcdefghij", 10);
404 wmemmove (wbuf
+ 1, wbuf
, 9);
405 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
408 if (wmempcpy (wbuf
+ 5, L
"abcde", 5) != wbuf
+ 10
409 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
412 wmemset (wbuf
+ 8, L
'j', 2);
413 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
416 wcscpy (wbuf
+ 4, L
"EDCBA");
417 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
420 if (wcpcpy (wbuf
+ 8, L
"F") != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
423 wcsncpy (wbuf
+ 6, L
"X", 4);
424 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
427 if (swprintf (wbuf
+ 7, 3, L
"%ls", L
"987654") >= 0
428 || wmemcmp (wbuf
, L
"aabcEDX98", 10))
431 if (swprintf (wbuf
+ 7, 3, L
"64") != 2
432 || wmemcmp (wbuf
, L
"aabcEDX64", 10))
435 /* These ops need runtime checking, but shouldn't __chk_fail. */
436 wmemcpy (wbuf
, L
"abcdefghij", l0
+ 10);
437 wmemmove (wbuf
+ 1, wbuf
, l0
+ 9);
438 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
441 if (wmempcpy (wbuf
+ 5, L
"abcde", l0
+ 5) != wbuf
+ 10
442 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
445 wmemset (wbuf
+ 8, L
'j', l0
+ 2);
446 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
449 wcscpy (wbuf
+ 4, wstr1
+ 5);
450 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
453 if (wcpcpy (wbuf
+ 8, wstr2
) != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
456 wcsncpy (wbuf
+ 6, L
"X", l0
+ 4);
457 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
460 if (wcpncpy (wbuf
+ 5, L
"cd", l0
+ 5) != wbuf
+ 7
461 || wmemcmp (wbuf
, L
"aabcEcd\0\0", 10))
464 if (swprintf (wbuf
+ 7, 3, L
"%d", num2
) >= 0
465 || wmemcmp (wbuf
, L
"aabcEcd98", 10))
468 wbuf
[l0
+ 8] = L
'\0';
470 if (wmemcmp (wbuf
, L
"aabcEcd9A", 10))
473 wbuf
[l0
+ 7] = L
'\0';
474 wcsncat (wbuf
, L
"ZYXWV", l0
+ 2);
475 if (wmemcmp (wbuf
, L
"aabcEcdZY", 10))
478 wmemcpy (wa
.buf1
, L
"abcdefghij", l0
+ 10);
479 wmemmove (wa
.buf1
+ 1, wa
.buf1
, l0
+ 9);
480 if (wmemcmp (wa
.buf1
, L
"aabcdefghi", 10))
483 if (wmempcpy (wa
.buf1
+ 5, L
"abcde", l0
+ 5) != wa
.buf1
+ 10
484 || wmemcmp (wa
.buf1
, L
"aabcdabcde", 10))
487 wmemset (wa
.buf1
+ 8, L
'j', l0
+ 2);
488 if (wmemcmp (wa
.buf1
, L
"aabcdabcjj", 10))
491 #if __USE_FORTIFY_LEVEL < 2
492 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
493 and sufficient GCC support, as the string operations overflow
494 from a.buf1 into a.buf2. */
495 wcscpy (wa
.buf1
+ 4, wstr1
+ 5);
496 if (wmemcmp (wa
.buf1
, L
"aabcEDCBA", 10))
499 if (wcpcpy (wa
.buf1
+ 8, wstr2
) != wa
.buf1
+ 9
500 || wmemcmp (wa
.buf1
, L
"aabcEDCBF", 10))
503 wcsncpy (wa
.buf1
+ 6, L
"X", l0
+ 4);
504 if (wmemcmp (wa
.buf1
, L
"aabcEDX\0\0", 10))
507 if (swprintf (wa
.buf1
+ 7, 3, L
"%d", num2
) >= 0
508 || wmemcmp (wa
.buf1
, L
"aabcEDX98", 10))
511 wa
.buf1
[l0
+ 8] = L
'\0';
512 wcscat (wa
.buf1
, L
"A");
513 if (wmemcmp (wa
.buf1
, L
"aabcEDX9A", 10))
516 wa
.buf1
[l0
+ 7] = L
'\0';
517 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 2);
518 if (wmemcmp (wa
.buf1
, L
"aabcEDXZY", 10))
523 #if __USE_FORTIFY_LEVEL >= 1
524 /* Now check if all buffer overflows are caught at runtime. */
527 wmemcpy (wbuf
+ 1, L
"abcdefghij", l0
+ 10);
531 wmemcpy (wbuf
+ 9, L
"abcdefghij", l0
+ 10);
535 wmemmove (wbuf
+ 2, wbuf
+ 1, l0
+ 9);
539 wp
= wmempcpy (wbuf
+ 6, L
"abcde", l0
+ 5);
543 wmemset (wbuf
+ 9, L
'j', l0
+ 2);
547 wcscpy (wbuf
+ 5, wstr1
+ 5);
551 wp
= wcpcpy (wbuf
+ 9, wstr2
);
555 wcsncpy (wbuf
+ 7, L
"X", l0
+ 4);
559 wcsncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
563 wcpncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
567 wcpncpy (wbuf
+ 6, L
"cd", l0
+ 5);
570 wmemcpy (wbuf
, wstr1
+ 2, l0
+ 9);
572 wcscat (wbuf
, L
"AB");
575 wmemcpy (wbuf
, wstr1
+ 3, l0
+ 8);
577 wcsncat (wbuf
, L
"ZYXWV", l0
+ 3);
581 wmemcpy (wa
.buf1
+ 1, L
"abcdefghij", l0
+ 10);
585 wmemmove (wa
.buf1
+ 2, wa
.buf1
+ 1, l0
+ 9);
589 wp
= wmempcpy (wa
.buf1
+ 6, L
"abcde", l0
+ 5);
593 wmemset (wa
.buf1
+ 9, L
'j', l0
+ 2);
596 #if __USE_FORTIFY_LEVEL >= 2
603 wcscpy (wa
.buf1
+ (O
+ 4), wstr1
+ 5);
607 wp
= wcpcpy (wa
.buf1
+ (O
+ 8), wstr2
);
611 wcsncpy (wa
.buf1
+ (O
+ 6), L
"X", l0
+ 4);
614 wmemcpy (wa
.buf1
, wstr1
+ (3 - O
), l0
+ 8 + O
);
616 wcscat (wa
.buf1
, L
"AB");
619 wmemcpy (wa
.buf1
, wstr1
+ (4 - O
), l0
+ 7 + O
);
621 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 3);
626 /* Now checks for %n protection. */
628 /* Constant literals passed directly are always ok
629 (even with warnings about possible bugs from GCC). */
631 if (sprintf (buf
, "%s%n%s%n", str2
, &n1
, str2
, &n2
) != 2
632 || n1
!= 1 || n2
!= 2)
635 /* In this case the format string is not known at compile time,
636 but resides in read-only memory, so is ok. */
637 if (snprintf (buf
, 4, str3
, str2
, &n1
, str2
, &n2
) != 2
638 || n1
!= 1 || n2
!= 2)
641 strcpy (buf2
+ 2, "%n%s%n");
642 /* When the format string is writable and contains %n,
643 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
645 if (sprintf (buf
, buf2
, str2
, &n1
, str2
, &n1
) != 2)
650 if (snprintf (buf
, 3, buf2
, str2
, &n1
, str2
, &n1
) != 2)
654 /* But if there is no %n, even writable format string
657 if (sprintf (buf
, buf2
+ 4, str2
) != 1)
660 /* Constant literals passed directly are always ok
661 (even with warnings about possible bugs from GCC). */
662 if (printf ("%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
663 || n1
!= 7 || n2
!= 14)
666 /* In this case the format string is not known at compile time,
667 but resides in read-only memory, so is ok. */
668 if (printf (str3
, str4
, &n1
, str5
, &n2
) != 14
669 || n1
!= 7 || n2
!= 14)
672 strcpy (buf2
+ 2, "%n%s%n");
673 /* When the format string is writable and contains %n,
674 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
676 if (printf (buf2
, str4
, &n1
, str5
, &n1
) != 14)
680 /* But if there is no %n, even writable format string
683 if (printf (buf2
+ 4, str5
) != 7)
688 /* Constant literals passed directly are always ok
689 (even with warnings about possible bugs from GCC). */
690 if (fprintf (fp
, "%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
691 || n1
!= 7 || n2
!= 14)
694 /* In this case the format string is not known at compile time,
695 but resides in read-only memory, so is ok. */
696 if (fprintf (fp
, str3
, str4
, &n1
, str5
, &n2
) != 14
697 || n1
!= 7 || n2
!= 14)
700 strcpy (buf2
+ 2, "%n%s%n");
701 /* When the format string is writable and contains %n,
702 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
704 if (fprintf (fp
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
708 /* But if there is no %n, even writable format string
711 if (fprintf (fp
, buf2
+ 4, str5
) != 7)
715 strcpy (buf2
+ 2, "%n%s%n");
716 /* When the format string is writable and contains %n,
717 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
719 if (asprintf (&my_ptr
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
728 if (obstack_printf (&obs
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
731 obstack_free (&obs
, NULL
);
734 if (asprintf (&my_ptr
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
740 if (obstack_printf (&obs
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
742 obstack_free (&obs
, NULL
);
744 if (freopen (temp_filename
, "r", stdin
) == NULL
)
746 puts ("could not open temporary file");
750 if (gets (buf
) != buf
|| memcmp (buf
, "abcdefgh", 9))
752 if (gets (buf
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
755 #if __USE_FORTIFY_LEVEL >= 1
757 if (gets (buf
) != buf
)
764 if (fgets (buf
, sizeof (buf
), stdin
) != buf
765 || memcmp (buf
, "abcdefgh\n", 10))
767 if (fgets (buf
, sizeof (buf
), stdin
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
772 if (fgets (buf
, l0
+ sizeof (buf
), stdin
) != buf
773 || memcmp (buf
, "abcdefgh\n", 10))
776 #if __USE_FORTIFY_LEVEL >= 1
778 if (fgets (buf
, sizeof (buf
) + 1, stdin
) != buf
)
783 if (fgets (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
790 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
791 || memcmp (buf
, "abcdefgh\n", 10))
793 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
794 || memcmp (buf
, "ABCDEFGHI", 10))
799 if (fgets_unlocked (buf
, l0
+ sizeof (buf
), stdin
) != buf
800 || memcmp (buf
, "abcdefgh\n", 10))
803 #if __USE_FORTIFY_LEVEL >= 1
805 if (fgets_unlocked (buf
, sizeof (buf
) + 1, stdin
) != buf
)
810 if (fgets_unlocked (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
817 if (fread (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
818 || memcmp (buf
, "abcdefgh\nA", 10))
820 if (fread (buf
, sizeof (buf
), 1, stdin
) != 1
821 || memcmp (buf
, "BCDEFGHI\na", 10))
826 if (fread (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
827 || memcmp (buf
, "abcdefgh\nA", 10))
829 if (fread (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
830 || memcmp (buf
, "BCDEFGHI\na", 10))
833 #if __USE_FORTIFY_LEVEL >= 1
835 if (fread (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
840 if (fread (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
847 if (fread_unlocked (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
848 || memcmp (buf
, "abcdefgh\nA", 10))
850 if (fread_unlocked (buf
, sizeof (buf
), 1, stdin
) != 1
851 || memcmp (buf
, "BCDEFGHI\na", 10))
856 if (fread_unlocked (buf
, 1, 4, stdin
) != 4
857 || memcmp (buf
, "abcdFGHI\na", 10))
859 if (fread_unlocked (buf
, 4, 1, stdin
) != 1
860 || memcmp (buf
, "efghFGHI\na", 10))
865 if (fread_unlocked (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
866 || memcmp (buf
, "abcdefgh\nA", 10))
868 if (fread_unlocked (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
869 || memcmp (buf
, "BCDEFGHI\na", 10))
872 #if __USE_FORTIFY_LEVEL >= 1
874 if (fread_unlocked (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
879 if (fread_unlocked (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
884 lseek (fileno (stdin
), 0, SEEK_SET
);
886 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
887 || memcmp (buf
, "abcdefgh\n", 9))
889 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
890 || memcmp (buf
, "ABCDEFGHI", 9))
893 lseek (fileno (stdin
), 0, SEEK_SET
);
895 if (read (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1) != sizeof (buf
) - 1
896 || memcmp (buf
, "abcdefgh\n", 9))
899 #if __USE_FORTIFY_LEVEL >= 1
901 if (read (fileno (stdin
), buf
, sizeof (buf
) + 1) != sizeof (buf
) + 1)
906 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
908 || memcmp (buf
, "\nABCDEFGH", 9))
910 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
911 || memcmp (buf
, "abcdefgh\n", 9))
913 if (pread (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
915 || memcmp (buf
, "h\nABCDEFG", 9))
918 #if __USE_FORTIFY_LEVEL >= 1
920 if (pread (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
926 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
928 || memcmp (buf
, "\nABCDEFGH", 9))
930 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
931 || memcmp (buf
, "abcdefgh\n", 9))
933 if (pread64 (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
935 || memcmp (buf
, "h\nABCDEFG", 9))
938 #if __USE_FORTIFY_LEVEL >= 1
940 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
946 if (freopen (temp_filename
, "r", stdin
) == NULL
)
948 puts ("could not open temporary file");
952 if (fseek (stdin
, 9 + 10 + 11, SEEK_SET
))
954 puts ("could not seek in test file");
958 #if __USE_FORTIFY_LEVEL >= 1
960 if (gets (buf
) != buf
)
965 /* Check whether missing N$ formats are detected. */
967 printf ("%3$d\n", 1, 2, 3, 4);
971 fprintf (stdout
, "%3$d\n", 1, 2, 3, 4);
975 sprintf (buf
, "%3$d\n", 1, 2, 3, 4);
979 snprintf (buf
, sizeof (buf
), "%3$d\n", 1, 2, 3, 4);
983 if (socketpair (PF_UNIX
, SOCK_STREAM
, 0, sp
))
987 const char *sendstr
= "abcdefgh\nABCDEFGH\n0123456789\n";
988 if ((size_t) send (sp
[0], sendstr
, strlen (sendstr
), 0)
993 if (recv (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
)
995 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
998 if (recv (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
)
999 != sizeof recvbuf
- 7
1000 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
1003 #if __USE_FORTIFY_LEVEL >= 1
1005 if (recv (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
)
1011 if (recv (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
)
1012 != sizeof recvbuf
- 3)
1018 struct sockaddr_un sa_un
;
1020 sl
= sizeof (sa_un
);
1021 if (recvfrom (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
,
1022 (struct sockaddr
*) &sa_un
, &sl
)
1024 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
1027 sl
= sizeof (sa_un
);
1028 if (recvfrom (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
,
1029 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 7
1030 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
1033 #if __USE_FORTIFY_LEVEL >= 1
1035 sl
= sizeof (sa_un
);
1036 if (recvfrom (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
,
1037 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
)
1042 sl
= sizeof (sa_un
);
1043 if (recvfrom (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
,
1044 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 3)
1053 char fname
[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1054 char *enddir
= strchr (fname
, '\0');
1055 if (mkdtemp (fname
) == NULL
)
1057 printf ("mkdtemp failed: %m\n");
1061 if (symlink ("bar", fname
) != 0)
1064 char readlinkbuf
[4];
1065 if (readlink (fname
, readlinkbuf
, 4) != 3
1066 || memcmp (readlinkbuf
, "bar", 3) != 0)
1068 if (readlink (fname
, readlinkbuf
+ 1, l0
+ 3) != 3
1069 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1072 #if __USE_FORTIFY_LEVEL >= 1
1074 if (readlink (fname
, readlinkbuf
+ 2, l0
+ 3) != 3)
1079 if (readlink (fname
, readlinkbuf
+ 3, 4) != 3)
1084 int tmpfd
= open ("/tmp", O_RDONLY
| O_DIRECTORY
);
1088 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
, 4) != 3
1089 || memcmp (readlinkbuf
, "bar", 3) != 0)
1091 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 1,
1093 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1096 #if __USE_FORTIFY_LEVEL >= 1
1098 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 2,
1104 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 3,
1112 char *cwd1
= getcwd (NULL
, 0);
1116 char *cwd2
= getcwd (NULL
, 250);
1122 if (strcmp (cwd1
, cwd2
) != 0)
1129 char *cwd3
= getcwd (NULL
, 0);
1132 if (strcmp (fname
, cwd3
) != 0)
1133 printf ("getcwd after chdir is '%s' != '%s',"
1134 "get{c,}wd tests skipped\n", cwd3
, fname
);
1137 char getcwdbuf
[sizeof fname
- 3];
1139 char *cwd4
= getcwd (getcwdbuf
, sizeof getcwdbuf
);
1140 if (cwd4
!= getcwdbuf
1141 || strcmp (getcwdbuf
, fname
) != 0)
1144 cwd4
= getcwd (getcwdbuf
+ 1, l0
+ sizeof getcwdbuf
- 1);
1145 if (cwd4
!= getcwdbuf
+ 1
1146 || getcwdbuf
[0] != fname
[0]
1147 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1150 #if __USE_FORTIFY_LEVEL >= 1
1152 if (getcwd (getcwdbuf
+ 2, l0
+ sizeof getcwdbuf
)
1158 if (getcwd (getcwdbuf
+ 2, sizeof getcwdbuf
)
1164 if (getwd (getcwdbuf
) != getcwdbuf
1165 || strcmp (getcwdbuf
, fname
) != 0)
1168 if (getwd (getcwdbuf
+ 1) != getcwdbuf
+ 1
1169 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1172 #if __USE_FORTIFY_LEVEL >= 1
1174 if (getwd (getcwdbuf
+ 2) != getcwdbuf
+ 2)
1180 if (chdir (cwd1
) != 0)
1188 if (unlink (fname
) != 0)
1192 if (rmdir (fname
) != 0)
1197 char largebuf
[PATH_MAX
];
1198 char *realres
= realpath (".", largebuf
);
1199 if (realres
!= largebuf
)
1202 # if __USE_FORTIFY_LEVEL >= 1
1205 realres
= realpath (".", realbuf
);
1206 if (realres
!= realbuf
)
1212 if (setlocale (LC_ALL
, "de_DE.UTF-8") != NULL
)
1214 assert (MB_CUR_MAX
<= 10);
1216 /* First a simple test. */
1218 if (wctomb (enough
, L
'A') != 1)
1221 #if __USE_FORTIFY_LEVEL >= 1
1222 /* We know the wchar_t encoding is ISO 10646. So pick a
1223 character which has a multibyte representation which does not
1227 if (wctomb (smallbuf
, L
'\x100') != 2)
1233 memset (&s
, '\0', sizeof (s
));
1234 if (wcrtomb (enough
, L
'D', &s
) != 1 || enough
[0] != 'D')
1237 #if __USE_FORTIFY_LEVEL >= 1
1238 /* We know the wchar_t encoding is ISO 10646. So pick a
1239 character which has a multibyte representation which does not
1243 if (wcrtomb (smallbuf
, L
'\x100', &s
) != 2)
1248 wchar_t wenough
[10];
1249 memset (&s
, '\0', sizeof (s
));
1250 const char *cp
= "A";
1251 if (mbsrtowcs (wenough
, &cp
, 10, &s
) != 1
1252 || wcscmp (wenough
, L
"A") != 0)
1256 if (mbsrtowcs (wenough
, &cp
, l0
+ 10, &s
) != 2
1257 || wcscmp (wenough
, L
"BC") != 0)
1260 #if __USE_FORTIFY_LEVEL >= 1
1262 wchar_t wsmallbuf
[2];
1264 mbsrtowcs (wsmallbuf
, &cp
, 10, &s
);
1269 if (mbstowcs (wenough
, cp
, 10) != 1
1270 || wcscmp (wenough
, L
"A") != 0)
1274 if (mbstowcs (wenough
, cp
, l0
+ 10) != 3
1275 || wcscmp (wenough
, L
"DEF") != 0)
1278 #if __USE_FORTIFY_LEVEL >= 1
1280 wchar_t wsmallbuf
[2];
1282 mbstowcs (wsmallbuf
, cp
, 10);
1286 memset (&s
, '\0', sizeof (s
));
1288 wcscpy (wenough
, L
"DEF");
1289 if (mbsnrtowcs (wenough
, &cp
, 1, 10, &s
) != 1
1290 || wcscmp (wenough
, L
"AEF") != 0)
1294 if (mbsnrtowcs (wenough
, &cp
, 1, l0
+ 10, &s
) != 1
1295 || wcscmp (wenough
, L
"IEF") != 0)
1298 #if __USE_FORTIFY_LEVEL >= 1
1300 wchar_t wsmallbuf
[2];
1302 mbsnrtowcs (wsmallbuf
, &cp
, 3, 10, &s
);
1306 memset (&s
, '\0', sizeof (s
));
1307 const wchar_t *wcp
= L
"A";
1308 if (wcsrtombs (enough
, &wcp
, 10, &s
) != 1
1309 || strcmp (enough
, "A") != 0)
1313 if (wcsrtombs (enough
, &wcp
, l0
+ 10, &s
) != 2
1314 || strcmp (enough
, "BC") != 0)
1317 #if __USE_FORTIFY_LEVEL >= 1
1321 wcsrtombs (smallbuf
, &wcp
, 10, &s
);
1325 memset (enough
, 'Z', sizeof (enough
));
1327 if (wcstombs (enough
, wcp
, 10) != 2
1328 || strcmp (enough
, "EF") != 0)
1332 if (wcstombs (enough
, wcp
, l0
+ 10) != 1
1333 || strcmp (enough
, "G") != 0)
1336 #if __USE_FORTIFY_LEVEL >= 1
1340 wcstombs (smallbuf
, wcp
, 10);
1344 memset (&s
, '\0', sizeof (s
));
1346 if (wcsnrtombs (enough
, &wcp
, 1, 10, &s
) != 1
1347 || strcmp (enough
, "A") != 0)
1351 if (wcsnrtombs (enough
, &wcp
, 1, l0
+ 10, &s
) != 1
1352 || strcmp (enough
, "B") != 0)
1355 #if __USE_FORTIFY_LEVEL >= 1
1359 wcsnrtombs (smallbuf
, &wcp
, 3, 10, &s
);
1365 puts ("cannot set locale");
1369 fd
= posix_openpt (O_RDWR
);
1373 if (ptsname_r (fd
, enough
, sizeof (enough
)) != 0)
1376 #if __USE_FORTIFY_LEVEL >= 1
1379 if (ptsname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1387 confstr (_CS_GNU_LIBC_VERSION
, largebuf
, sizeof (largebuf
));
1388 # if __USE_FORTIFY_LEVEL >= 1
1391 confstr (_CS_GNU_LIBC_VERSION
, smallbuf
, sizeof (largebuf
));
1397 int ngr
= getgroups (5, grpslarge
);
1398 asm volatile ("" : : "r" (ngr
));
1399 #if __USE_FORTIFY_LEVEL >= 1
1402 ngr
= getgroups (5, (gid_t
*) smallbuf
);
1403 asm volatile ("" : : "r" (ngr
));
1407 fd
= open (_PATH_TTY
, O_RDONLY
);
1411 if (ttyname_r (fd
, enough
, sizeof (enough
)) != 0)
1414 #if __USE_FORTIFY_LEVEL >= 1
1417 if (ttyname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1424 char hostnamelarge
[1000];
1425 gethostname (hostnamelarge
, sizeof (hostnamelarge
));
1426 #if __USE_FORTIFY_LEVEL >= 1
1429 gethostname (smallbuf
, sizeof (hostnamelarge
));
1433 char loginlarge
[1000];
1434 getlogin_r (loginlarge
, sizeof (hostnamelarge
));
1435 #if __USE_FORTIFY_LEVEL >= 1
1438 getlogin_r (smallbuf
, sizeof (loginlarge
));
1442 char domainnamelarge
[1000];
1443 int res
= getdomainname (domainnamelarge
, sizeof (domainnamelarge
));
1444 asm volatile ("" : : "r" (res
));
1445 #if __USE_FORTIFY_LEVEL >= 1
1448 res
= getdomainname (smallbuf
, sizeof (domainnamelarge
));
1449 asm volatile ("" : : "r" (res
));
1455 FD_SET (FD_SETSIZE
- 1, &s
);
1456 #if __USE_FORTIFY_LEVEL >= 1
1458 FD_SET (FD_SETSIZE
, &s
);
1461 FD_CLR (FD_SETSIZE
- 1, &s
);
1462 #if __USE_FORTIFY_LEVEL >= 1
1464 FD_CLR (FD_SETSIZE
, &s
);
1467 FD_ISSET (FD_SETSIZE
- 1, &s
);
1468 #if __USE_FORTIFY_LEVEL >= 1
1470 FD_ISSET (FD_SETSIZE
, &s
);
1474 struct pollfd fds
[1];
1475 fds
[0].fd
= STDOUT_FILENO
;
1476 fds
[0].events
= POLLOUT
;
1478 #if __USE_FORTIFY_LEVEL >= 1
1483 ppoll (fds
, 1, NULL
, NULL
);
1484 #if __USE_FORTIFY_LEVEL >= 1
1486 ppoll (fds
, 2, NULL
, NULL
);