1 /* Copyright (C) 2004-2014 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/>. */
31 #include <sys/select.h>
32 #include <sys/socket.h>
36 #define obstack_chunk_alloc malloc
37 #define obstack_chunk_free free
40 static void do_prepare (void);
41 static int do_test (void);
42 #define PREPARE(argc, argv) do_prepare ()
43 #define TEST_FUNCTION do_test ()
44 #include "../test-skeleton.c"
49 int temp_fd
= create_temp_file ("tst-chk1.", &temp_filename
);
52 printf ("cannot create temporary file: %m\n");
56 const char *strs
= "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
57 if ((size_t) write (temp_fd
, strs
, strlen (strs
)) != strlen (strs
))
59 puts ("could not write test strings into file");
60 unlink (temp_filename
);
65 volatile int chk_fail_ok
;
75 longjmp (chk_fail_buf
, 1);
86 const char *str1
= "JIHGFEDCBA";
87 const char *str2
= "F";
88 const char *str3
= "%s%n%s%n";
89 const char *str4
= "Hello, ";
90 const char *str5
= "World!\n";
91 const wchar_t *wstr1
= L
"JIHGFEDCBA";
92 const wchar_t *wstr2
= L
"F";
93 const wchar_t *wstr3
= L
"%s%n%s%n";
94 const wchar_t *wstr4
= L
"Hello, ";
95 const wchar_t *wstr5
= L
"World!\n";
101 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
102 #define CHK_FAIL_START \
104 if (! setjmp (chk_fail_buf)) \
106 #define CHK_FAIL_END \
110 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
111 # define CHK_FAIL2_START CHK_FAIL_START
112 # define CHK_FAIL2_END CHK_FAIL_END
114 # define CHK_FAIL2_START
115 # define CHK_FAIL2_END
121 set_fortify_handler (handler
);
123 struct A
{ char buf1
[9]; char buf2
[1]; } a
;
124 struct wA
{ wchar_t buf1
[9]; wchar_t buf2
[1]; } wa
;
126 printf ("Test checking routines at fortify level %d\n",
127 #ifdef __USE_FORTIFY_LEVEL
128 (int) __USE_FORTIFY_LEVEL
134 #if defined __USE_FORTIFY_LEVEL && !defined __fortify_function
135 printf ("Test skipped");
140 /* These ops can be done without runtime checking of object size. */
141 memcpy (buf
, "abcdefghij", 10);
142 memmove (buf
+ 1, buf
, 9);
143 if (memcmp (buf
, "aabcdefghi", 10))
146 if (mempcpy (buf
+ 5, "abcde", 5) != buf
+ 10
147 || memcmp (buf
, "aabcdabcde", 10))
150 memset (buf
+ 8, 'j', 2);
151 if (memcmp (buf
, "aabcdabcjj", 10))
154 strcpy (buf
+ 4, "EDCBA");
155 if (memcmp (buf
, "aabcEDCBA", 10))
158 if (stpcpy (buf
+ 8, "F") != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
161 strncpy (buf
+ 6, "X", 4);
162 if (memcmp (buf
, "aabcEDX\0\0", 10))
165 if (sprintf (buf
+ 7, "%s", "67") != 2 || memcmp (buf
, "aabcEDX67", 10))
168 if (snprintf (buf
+ 7, 3, "%s", "987654") != 6
169 || memcmp (buf
, "aabcEDX98", 10))
172 /* These ops need runtime checking, but shouldn't __chk_fail. */
173 memcpy (buf
, "abcdefghij", l0
+ 10);
174 memmove (buf
+ 1, buf
, l0
+ 9);
175 if (memcmp (buf
, "aabcdefghi", 10))
178 if (mempcpy (buf
+ 5, "abcde", l0
+ 5) != buf
+ 10
179 || memcmp (buf
, "aabcdabcde", 10))
182 memset (buf
+ 8, 'j', l0
+ 2);
183 if (memcmp (buf
, "aabcdabcjj", 10))
186 strcpy (buf
+ 4, str1
+ 5);
187 if (memcmp (buf
, "aabcEDCBA", 10))
190 if (stpcpy (buf
+ 8, str2
) != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
193 strncpy (buf
+ 6, "X", l0
+ 4);
194 if (memcmp (buf
, "aabcEDX\0\0", 10))
197 if (stpncpy (buf
+ 5, "cd", l0
+ 5) != buf
+ 7
198 || memcmp (buf
, "aabcEcd\0\0", 10))
201 if (sprintf (buf
+ 7, "%d", num1
) != 2 || memcmp (buf
, "aabcEcd67", 10))
204 if (snprintf (buf
+ 7, 3, "%d", num2
) != 6 || memcmp (buf
, "aabcEcd98", 10))
209 if (memcmp (buf
, "aabcEcd9A", 10))
213 strncat (buf
, "ZYXWV", l0
+ 2);
214 if (memcmp (buf
, "aabcEcdZY", 10))
217 memcpy (a
.buf1
, "abcdefghij", l0
+ 10);
218 memmove (a
.buf1
+ 1, a
.buf1
, l0
+ 9);
219 if (memcmp (a
.buf1
, "aabcdefghi", 10))
222 if (mempcpy (a
.buf1
+ 5, "abcde", l0
+ 5) != a
.buf1
+ 10
223 || memcmp (a
.buf1
, "aabcdabcde", 10))
226 memset (a
.buf1
+ 8, 'j', l0
+ 2);
227 if (memcmp (a
.buf1
, "aabcdabcjj", 10))
230 #if __USE_FORTIFY_LEVEL < 2
231 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
232 and sufficient GCC support, as the string operations overflow
233 from a.buf1 into a.buf2. */
234 strcpy (a
.buf1
+ 4, str1
+ 5);
235 if (memcmp (a
.buf1
, "aabcEDCBA", 10))
238 if (stpcpy (a
.buf1
+ 8, str2
) != a
.buf1
+ 9
239 || memcmp (a
.buf1
, "aabcEDCBF", 10))
242 strncpy (a
.buf1
+ 6, "X", l0
+ 4);
243 if (memcmp (a
.buf1
, "aabcEDX\0\0", 10))
246 if (sprintf (a
.buf1
+ 7, "%d", num1
) != 2
247 || memcmp (a
.buf1
, "aabcEDX67", 10))
250 if (snprintf (a
.buf1
+ 7, 3, "%d", num2
) != 6
251 || memcmp (a
.buf1
, "aabcEDX98", 10))
254 a
.buf1
[l0
+ 8] = '\0';
255 strcat (a
.buf1
, "A");
256 if (memcmp (a
.buf1
, "aabcEDX9A", 10))
259 a
.buf1
[l0
+ 7] = '\0';
260 strncat (a
.buf1
, "ZYXWV", l0
+ 2);
261 if (memcmp (a
.buf1
, "aabcEDXZY", 10))
266 #if __USE_FORTIFY_LEVEL >= 1
267 /* Now check if all buffer overflows are caught at runtime. */
270 memcpy (buf
+ 1, "abcdefghij", l0
+ 10);
274 memmove (buf
+ 2, buf
+ 1, l0
+ 9);
278 p
= (char *) mempcpy (buf
+ 6, "abcde", l0
+ 5);
282 memset (buf
+ 9, 'j', l0
+ 2);
286 strcpy (buf
+ 5, str1
+ 5);
290 p
= stpcpy (buf
+ 9, str2
);
294 strncpy (buf
+ 7, "X", l0
+ 4);
298 stpncpy (buf
+ 6, "cd", l0
+ 5);
301 # if !defined __cplusplus || defined __va_arg_pack
303 sprintf (buf
+ 8, "%d", num1
);
307 snprintf (buf
+ 8, l0
+ 3, "%d", num2
);
311 swprintf (wbuf
+ 8, 3, L
"%d", num1
);
315 swprintf (wbuf
+ 8, l0
+ 3, L
"%d", num1
);
319 memcpy (buf
, str1
+ 2, l0
+ 9);
324 memcpy (buf
, str1
+ 3, l0
+ 8);
326 strncat (buf
, "ZYXWV", l0
+ 3);
330 memcpy (a
.buf1
+ 1, "abcdefghij", l0
+ 10);
334 memmove (a
.buf1
+ 2, a
.buf1
+ 1, l0
+ 9);
338 p
= (char *) mempcpy (a
.buf1
+ 6, "abcde", l0
+ 5);
342 memset (a
.buf1
+ 9, 'j', l0
+ 2);
345 # if __USE_FORTIFY_LEVEL >= 2
352 strcpy (a
.buf1
+ (O
+ 4), str1
+ 5);
356 p
= stpcpy (a
.buf1
+ (O
+ 8), str2
);
360 strncpy (a
.buf1
+ (O
+ 6), "X", l0
+ 4);
363 # if !defined __cplusplus || defined __va_arg_pack
365 sprintf (a
.buf1
+ (O
+ 7), "%d", num1
);
369 snprintf (a
.buf1
+ (O
+ 7), l0
+ 3, "%d", num2
);
373 memcpy (a
.buf1
, str1
+ (3 - O
), l0
+ 8 + O
);
375 strcat (a
.buf1
, "AB");
378 memcpy (a
.buf1
, str1
+ (4 - O
), l0
+ 7 + O
);
380 strncat (a
.buf1
, "ZYXWV", l0
+ 3);
385 /* These ops can be done without runtime checking of object size. */
386 wmemcpy (wbuf
, L
"abcdefghij", 10);
387 wmemmove (wbuf
+ 1, wbuf
, 9);
388 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
391 if (wmempcpy (wbuf
+ 5, L
"abcde", 5) != wbuf
+ 10
392 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
395 wmemset (wbuf
+ 8, L
'j', 2);
396 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
399 wcscpy (wbuf
+ 4, L
"EDCBA");
400 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
403 if (wcpcpy (wbuf
+ 8, L
"F") != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
406 wcsncpy (wbuf
+ 6, L
"X", 4);
407 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
410 if (swprintf (wbuf
+ 7, 3, L
"%ls", L
"987654") >= 0
411 || wmemcmp (wbuf
, L
"aabcEDX98", 10))
414 if (swprintf (wbuf
+ 7, 3, L
"64") != 2
415 || wmemcmp (wbuf
, L
"aabcEDX64", 10))
418 /* These ops need runtime checking, but shouldn't __chk_fail. */
419 wmemcpy (wbuf
, L
"abcdefghij", l0
+ 10);
420 wmemmove (wbuf
+ 1, wbuf
, l0
+ 9);
421 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
424 if (wmempcpy (wbuf
+ 5, L
"abcde", l0
+ 5) != wbuf
+ 10
425 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
428 wmemset (wbuf
+ 8, L
'j', l0
+ 2);
429 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
432 wcscpy (wbuf
+ 4, wstr1
+ 5);
433 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
436 if (wcpcpy (wbuf
+ 8, wstr2
) != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
439 wcsncpy (wbuf
+ 6, L
"X", l0
+ 4);
440 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
443 if (wcpncpy (wbuf
+ 5, L
"cd", l0
+ 5) != wbuf
+ 7
444 || wmemcmp (wbuf
, L
"aabcEcd\0\0", 10))
447 if (swprintf (wbuf
+ 7, 3, L
"%d", num2
) >= 0
448 || wmemcmp (wbuf
, L
"aabcEcd98", 10))
451 wbuf
[l0
+ 8] = L
'\0';
453 if (wmemcmp (wbuf
, L
"aabcEcd9A", 10))
456 wbuf
[l0
+ 7] = L
'\0';
457 wcsncat (wbuf
, L
"ZYXWV", l0
+ 2);
458 if (wmemcmp (wbuf
, L
"aabcEcdZY", 10))
461 wmemcpy (wa
.buf1
, L
"abcdefghij", l0
+ 10);
462 wmemmove (wa
.buf1
+ 1, wa
.buf1
, l0
+ 9);
463 if (wmemcmp (wa
.buf1
, L
"aabcdefghi", 10))
466 if (wmempcpy (wa
.buf1
+ 5, L
"abcde", l0
+ 5) != wa
.buf1
+ 10
467 || wmemcmp (wa
.buf1
, L
"aabcdabcde", 10))
470 wmemset (wa
.buf1
+ 8, L
'j', l0
+ 2);
471 if (wmemcmp (wa
.buf1
, L
"aabcdabcjj", 10))
474 #if __USE_FORTIFY_LEVEL < 2
475 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
476 and sufficient GCC support, as the string operations overflow
477 from a.buf1 into a.buf2. */
478 wcscpy (wa
.buf1
+ 4, wstr1
+ 5);
479 if (wmemcmp (wa
.buf1
, L
"aabcEDCBA", 10))
482 if (wcpcpy (wa
.buf1
+ 8, wstr2
) != wa
.buf1
+ 9
483 || wmemcmp (wa
.buf1
, L
"aabcEDCBF", 10))
486 wcsncpy (wa
.buf1
+ 6, L
"X", l0
+ 4);
487 if (wmemcmp (wa
.buf1
, L
"aabcEDX\0\0", 10))
490 if (swprintf (wa
.buf1
+ 7, 3, L
"%d", num2
) >= 0
491 || wmemcmp (wa
.buf1
, L
"aabcEDX98", 10))
494 wa
.buf1
[l0
+ 8] = L
'\0';
495 wcscat (wa
.buf1
, L
"A");
496 if (wmemcmp (wa
.buf1
, L
"aabcEDX9A", 10))
499 wa
.buf1
[l0
+ 7] = L
'\0';
500 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 2);
501 if (wmemcmp (wa
.buf1
, L
"aabcEDXZY", 10))
506 #if __USE_FORTIFY_LEVEL >= 1
507 /* Now check if all buffer overflows are caught at runtime. */
510 wmemcpy (wbuf
+ 1, L
"abcdefghij", l0
+ 10);
514 wmemcpy (wbuf
+ 9, L
"abcdefghij", l0
+ 10);
518 wmemmove (wbuf
+ 2, wbuf
+ 1, l0
+ 9);
522 wp
= wmempcpy (wbuf
+ 6, L
"abcde", l0
+ 5);
526 wmemset (wbuf
+ 9, L
'j', l0
+ 2);
530 wcscpy (wbuf
+ 5, wstr1
+ 5);
534 wp
= wcpcpy (wbuf
+ 9, wstr2
);
538 wcsncpy (wbuf
+ 7, L
"X", l0
+ 4);
542 wcsncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
546 wcpncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
550 wcpncpy (wbuf
+ 6, L
"cd", l0
+ 5);
553 wmemcpy (wbuf
, wstr1
+ 2, l0
+ 9);
555 wcscat (wbuf
, L
"AB");
558 wmemcpy (wbuf
, wstr1
+ 3, l0
+ 8);
560 wcsncat (wbuf
, L
"ZYXWV", l0
+ 3);
564 wmemcpy (wa
.buf1
+ 1, L
"abcdefghij", l0
+ 10);
568 wmemmove (wa
.buf1
+ 2, wa
.buf1
+ 1, l0
+ 9);
572 wp
= wmempcpy (wa
.buf1
+ 6, L
"abcde", l0
+ 5);
576 wmemset (wa
.buf1
+ 9, L
'j', l0
+ 2);
579 #if __USE_FORTIFY_LEVEL >= 2
586 wcscpy (wa
.buf1
+ (O
+ 4), wstr1
+ 5);
590 wp
= wcpcpy (wa
.buf1
+ (O
+ 8), wstr2
);
594 wcsncpy (wa
.buf1
+ (O
+ 6), L
"X", l0
+ 4);
597 wmemcpy (wa
.buf1
, wstr1
+ (3 - O
), l0
+ 8 + O
);
599 wcscat (wa
.buf1
, L
"AB");
602 wmemcpy (wa
.buf1
, wstr1
+ (4 - O
), l0
+ 7 + O
);
604 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 3);
609 /* Now checks for %n protection. */
611 /* Constant literals passed directly are always ok
612 (even with warnings about possible bugs from GCC). */
614 if (sprintf (buf
, "%s%n%s%n", str2
, &n1
, str2
, &n2
) != 2
615 || n1
!= 1 || n2
!= 2)
618 /* In this case the format string is not known at compile time,
619 but resides in read-only memory, so is ok. */
620 if (snprintf (buf
, 4, str3
, str2
, &n1
, str2
, &n2
) != 2
621 || n1
!= 1 || n2
!= 2)
624 strcpy (buf2
+ 2, "%n%s%n");
625 /* When the format string is writable and contains %n,
626 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
628 if (sprintf (buf
, buf2
, str2
, &n1
, str2
, &n1
) != 2)
633 if (snprintf (buf
, 3, buf2
, str2
, &n1
, str2
, &n1
) != 2)
637 /* But if there is no %n, even writable format string
640 if (sprintf (buf
, buf2
+ 4, str2
) != 1)
643 /* Constant literals passed directly are always ok
644 (even with warnings about possible bugs from GCC). */
645 if (printf ("%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
646 || n1
!= 7 || n2
!= 14)
649 /* In this case the format string is not known at compile time,
650 but resides in read-only memory, so is ok. */
651 if (printf (str3
, str4
, &n1
, str5
, &n2
) != 14
652 || n1
!= 7 || n2
!= 14)
655 strcpy (buf2
+ 2, "%n%s%n");
656 /* When the format string is writable and contains %n,
657 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
659 if (printf (buf2
, str4
, &n1
, str5
, &n1
) != 14)
663 /* But if there is no %n, even writable format string
666 if (printf (buf2
+ 4, str5
) != 7)
671 /* Constant literals passed directly are always ok
672 (even with warnings about possible bugs from GCC). */
673 if (fprintf (fp
, "%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
674 || n1
!= 7 || n2
!= 14)
677 /* In this case the format string is not known at compile time,
678 but resides in read-only memory, so is ok. */
679 if (fprintf (fp
, str3
, str4
, &n1
, str5
, &n2
) != 14
680 || n1
!= 7 || n2
!= 14)
683 strcpy (buf2
+ 2, "%n%s%n");
684 /* When the format string is writable and contains %n,
685 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
687 if (fprintf (fp
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
691 /* But if there is no %n, even writable format string
694 if (fprintf (fp
, buf2
+ 4, str5
) != 7)
698 strcpy (buf2
+ 2, "%n%s%n");
699 /* When the format string is writable and contains %n,
700 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
702 if (asprintf (&my_ptr
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
711 if (obstack_printf (&obs
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
714 obstack_free (&obs
, NULL
);
717 if (asprintf (&my_ptr
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
723 if (obstack_printf (&obs
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
725 obstack_free (&obs
, NULL
);
727 if (freopen (temp_filename
, "r", stdin
) == NULL
)
729 puts ("could not open temporary file");
733 if (gets (buf
) != buf
|| memcmp (buf
, "abcdefgh", 9))
735 if (gets (buf
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
738 #if __USE_FORTIFY_LEVEL >= 1
740 if (gets (buf
) != buf
)
747 if (fgets (buf
, sizeof (buf
), stdin
) != buf
748 || memcmp (buf
, "abcdefgh\n", 10))
750 if (fgets (buf
, sizeof (buf
), stdin
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
755 if (fgets (buf
, l0
+ sizeof (buf
), stdin
) != buf
756 || memcmp (buf
, "abcdefgh\n", 10))
759 #if __USE_FORTIFY_LEVEL >= 1
761 if (fgets (buf
, sizeof (buf
) + 1, stdin
) != buf
)
766 if (fgets (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
773 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
774 || memcmp (buf
, "abcdefgh\n", 10))
776 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
777 || memcmp (buf
, "ABCDEFGHI", 10))
782 if (fgets_unlocked (buf
, l0
+ sizeof (buf
), stdin
) != buf
783 || memcmp (buf
, "abcdefgh\n", 10))
786 #if __USE_FORTIFY_LEVEL >= 1
788 if (fgets_unlocked (buf
, sizeof (buf
) + 1, stdin
) != buf
)
793 if (fgets_unlocked (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
800 if (fread (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
801 || memcmp (buf
, "abcdefgh\nA", 10))
803 if (fread (buf
, sizeof (buf
), 1, stdin
) != 1
804 || memcmp (buf
, "BCDEFGHI\na", 10))
809 if (fread (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
810 || memcmp (buf
, "abcdefgh\nA", 10))
812 if (fread (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
813 || memcmp (buf
, "BCDEFGHI\na", 10))
816 #if __USE_FORTIFY_LEVEL >= 1
818 if (fread (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
823 if (fread (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
830 if (fread_unlocked (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
831 || memcmp (buf
, "abcdefgh\nA", 10))
833 if (fread_unlocked (buf
, sizeof (buf
), 1, stdin
) != 1
834 || memcmp (buf
, "BCDEFGHI\na", 10))
839 if (fread_unlocked (buf
, 1, 4, stdin
) != 4
840 || memcmp (buf
, "abcdFGHI\na", 10))
842 if (fread_unlocked (buf
, 4, 1, stdin
) != 1
843 || memcmp (buf
, "efghFGHI\na", 10))
848 if (fread_unlocked (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
849 || memcmp (buf
, "abcdefgh\nA", 10))
851 if (fread_unlocked (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
852 || memcmp (buf
, "BCDEFGHI\na", 10))
855 #if __USE_FORTIFY_LEVEL >= 1
857 if (fread_unlocked (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
862 if (fread_unlocked (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
867 lseek (fileno (stdin
), 0, SEEK_SET
);
869 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
870 || memcmp (buf
, "abcdefgh\n", 9))
872 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
873 || memcmp (buf
, "ABCDEFGHI", 9))
876 lseek (fileno (stdin
), 0, SEEK_SET
);
878 if (read (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1) != sizeof (buf
) - 1
879 || memcmp (buf
, "abcdefgh\n", 9))
882 #if __USE_FORTIFY_LEVEL >= 1
884 if (read (fileno (stdin
), buf
, sizeof (buf
) + 1) != sizeof (buf
) + 1)
889 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
891 || memcmp (buf
, "\nABCDEFGH", 9))
893 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
894 || memcmp (buf
, "abcdefgh\n", 9))
896 if (pread (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
898 || memcmp (buf
, "h\nABCDEFG", 9))
901 #if __USE_FORTIFY_LEVEL >= 1
903 if (pread (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
909 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
911 || memcmp (buf
, "\nABCDEFGH", 9))
913 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
914 || memcmp (buf
, "abcdefgh\n", 9))
916 if (pread64 (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
918 || memcmp (buf
, "h\nABCDEFG", 9))
921 #if __USE_FORTIFY_LEVEL >= 1
923 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
929 if (freopen (temp_filename
, "r", stdin
) == NULL
)
931 puts ("could not open temporary file");
935 if (fseek (stdin
, 9 + 10 + 11, SEEK_SET
))
937 puts ("could not seek in test file");
941 #if __USE_FORTIFY_LEVEL >= 1
943 if (gets (buf
) != buf
)
948 /* Check whether missing N$ formats are detected. */
950 printf ("%3$d\n", 1, 2, 3, 4);
954 fprintf (stdout
, "%3$d\n", 1, 2, 3, 4);
958 sprintf (buf
, "%3$d\n", 1, 2, 3, 4);
962 snprintf (buf
, sizeof (buf
), "%3$d\n", 1, 2, 3, 4);
966 if (socketpair (PF_UNIX
, SOCK_STREAM
, 0, sp
))
970 const char *sendstr
= "abcdefgh\nABCDEFGH\n0123456789\n";
971 if ((size_t) send (sp
[0], sendstr
, strlen (sendstr
), 0)
976 if (recv (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
)
978 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
981 if (recv (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
)
982 != sizeof recvbuf
- 7
983 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
986 #if __USE_FORTIFY_LEVEL >= 1
988 if (recv (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
)
994 if (recv (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
)
995 != sizeof recvbuf
- 3)
1001 struct sockaddr_un sa_un
;
1003 sl
= sizeof (sa_un
);
1004 if (recvfrom (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
,
1005 (struct sockaddr
*) &sa_un
, &sl
)
1007 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
1010 sl
= sizeof (sa_un
);
1011 if (recvfrom (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
,
1012 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 7
1013 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
1016 #if __USE_FORTIFY_LEVEL >= 1
1018 sl
= sizeof (sa_un
);
1019 if (recvfrom (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
,
1020 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
)
1025 sl
= sizeof (sa_un
);
1026 if (recvfrom (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
,
1027 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 3)
1036 char fname
[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1037 char *enddir
= strchr (fname
, '\0');
1038 if (mkdtemp (fname
) == NULL
)
1040 printf ("mkdtemp failed: %m\n");
1044 if (symlink ("bar", fname
) != 0)
1047 char readlinkbuf
[4];
1048 if (readlink (fname
, readlinkbuf
, 4) != 3
1049 || memcmp (readlinkbuf
, "bar", 3) != 0)
1051 if (readlink (fname
, readlinkbuf
+ 1, l0
+ 3) != 3
1052 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1055 #if __USE_FORTIFY_LEVEL >= 1
1057 if (readlink (fname
, readlinkbuf
+ 2, l0
+ 3) != 3)
1062 if (readlink (fname
, readlinkbuf
+ 3, 4) != 3)
1067 int tmpfd
= open ("/tmp", O_RDONLY
| O_DIRECTORY
);
1071 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
, 4) != 3
1072 || memcmp (readlinkbuf
, "bar", 3) != 0)
1074 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 1,
1076 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1079 #if __USE_FORTIFY_LEVEL >= 1
1081 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 2,
1087 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 3,
1095 char *cwd1
= getcwd (NULL
, 0);
1099 char *cwd2
= getcwd (NULL
, 250);
1105 if (strcmp (cwd1
, cwd2
) != 0)
1112 char *cwd3
= getcwd (NULL
, 0);
1115 if (strcmp (fname
, cwd3
) != 0)
1116 printf ("getcwd after chdir is '%s' != '%s',"
1117 "get{c,}wd tests skipped\n", cwd3
, fname
);
1120 char getcwdbuf
[sizeof fname
- 3];
1122 char *cwd4
= getcwd (getcwdbuf
, sizeof getcwdbuf
);
1123 if (cwd4
!= getcwdbuf
1124 || strcmp (getcwdbuf
, fname
) != 0)
1127 cwd4
= getcwd (getcwdbuf
+ 1, l0
+ sizeof getcwdbuf
- 1);
1128 if (cwd4
!= getcwdbuf
+ 1
1129 || getcwdbuf
[0] != fname
[0]
1130 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1133 #if __USE_FORTIFY_LEVEL >= 1
1135 if (getcwd (getcwdbuf
+ 2, l0
+ sizeof getcwdbuf
)
1141 if (getcwd (getcwdbuf
+ 2, sizeof getcwdbuf
)
1147 if (getwd (getcwdbuf
) != getcwdbuf
1148 || strcmp (getcwdbuf
, fname
) != 0)
1151 if (getwd (getcwdbuf
+ 1) != getcwdbuf
+ 1
1152 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1155 #if __USE_FORTIFY_LEVEL >= 1
1157 if (getwd (getcwdbuf
+ 2) != getcwdbuf
+ 2)
1163 if (chdir (cwd1
) != 0)
1171 if (unlink (fname
) != 0)
1175 if (rmdir (fname
) != 0)
1180 char largebuf
[PATH_MAX
];
1181 char *realres
= realpath (".", largebuf
);
1182 if (realres
!= largebuf
)
1185 # if __USE_FORTIFY_LEVEL >= 1
1188 realres
= realpath (".", realbuf
);
1189 if (realres
!= realbuf
)
1195 if (setlocale (LC_ALL
, "de_DE.UTF-8") != NULL
)
1197 assert (MB_CUR_MAX
<= 10);
1199 /* First a simple test. */
1201 if (wctomb (enough
, L
'A') != 1)
1204 #if __USE_FORTIFY_LEVEL >= 1
1205 /* We know the wchar_t encoding is ISO 10646. So pick a
1206 character which has a multibyte representation which does not
1210 if (wctomb (smallbuf
, L
'\x100') != 2)
1216 memset (&s
, '\0', sizeof (s
));
1217 if (wcrtomb (enough
, L
'D', &s
) != 1 || enough
[0] != 'D')
1220 #if __USE_FORTIFY_LEVEL >= 1
1221 /* We know the wchar_t encoding is ISO 10646. So pick a
1222 character which has a multibyte representation which does not
1226 if (wcrtomb (smallbuf
, L
'\x100', &s
) != 2)
1231 wchar_t wenough
[10];
1232 memset (&s
, '\0', sizeof (s
));
1233 const char *cp
= "A";
1234 if (mbsrtowcs (wenough
, &cp
, 10, &s
) != 1
1235 || wcscmp (wenough
, L
"A") != 0)
1239 if (mbsrtowcs (wenough
, &cp
, l0
+ 10, &s
) != 2
1240 || wcscmp (wenough
, L
"BC") != 0)
1243 #if __USE_FORTIFY_LEVEL >= 1
1245 wchar_t wsmallbuf
[2];
1247 mbsrtowcs (wsmallbuf
, &cp
, 10, &s
);
1252 if (mbstowcs (wenough
, cp
, 10) != 1
1253 || wcscmp (wenough
, L
"A") != 0)
1257 if (mbstowcs (wenough
, cp
, l0
+ 10) != 3
1258 || wcscmp (wenough
, L
"DEF") != 0)
1261 #if __USE_FORTIFY_LEVEL >= 1
1263 wchar_t wsmallbuf
[2];
1265 mbstowcs (wsmallbuf
, cp
, 10);
1269 memset (&s
, '\0', sizeof (s
));
1271 wcscpy (wenough
, L
"DEF");
1272 if (mbsnrtowcs (wenough
, &cp
, 1, 10, &s
) != 1
1273 || wcscmp (wenough
, L
"AEF") != 0)
1277 if (mbsnrtowcs (wenough
, &cp
, 1, l0
+ 10, &s
) != 1
1278 || wcscmp (wenough
, L
"IEF") != 0)
1281 #if __USE_FORTIFY_LEVEL >= 1
1283 wchar_t wsmallbuf
[2];
1285 mbsnrtowcs (wsmallbuf
, &cp
, 3, 10, &s
);
1289 memset (&s
, '\0', sizeof (s
));
1290 const wchar_t *wcp
= L
"A";
1291 if (wcsrtombs (enough
, &wcp
, 10, &s
) != 1
1292 || strcmp (enough
, "A") != 0)
1296 if (wcsrtombs (enough
, &wcp
, l0
+ 10, &s
) != 2
1297 || strcmp (enough
, "BC") != 0)
1300 #if __USE_FORTIFY_LEVEL >= 1
1304 wcsrtombs (smallbuf
, &wcp
, 10, &s
);
1308 memset (enough
, 'Z', sizeof (enough
));
1310 if (wcstombs (enough
, wcp
, 10) != 2
1311 || strcmp (enough
, "EF") != 0)
1315 if (wcstombs (enough
, wcp
, l0
+ 10) != 1
1316 || strcmp (enough
, "G") != 0)
1319 #if __USE_FORTIFY_LEVEL >= 1
1323 wcstombs (smallbuf
, wcp
, 10);
1327 memset (&s
, '\0', sizeof (s
));
1329 if (wcsnrtombs (enough
, &wcp
, 1, 10, &s
) != 1
1330 || strcmp (enough
, "A") != 0)
1334 if (wcsnrtombs (enough
, &wcp
, 1, l0
+ 10, &s
) != 1
1335 || strcmp (enough
, "B") != 0)
1338 #if __USE_FORTIFY_LEVEL >= 1
1342 wcsnrtombs (smallbuf
, &wcp
, 3, 10, &s
);
1348 puts ("cannot set locale");
1352 int fd
= posix_openpt (O_RDWR
);
1356 if (ptsname_r (fd
, enough
, sizeof (enough
)) != 0)
1359 #if __USE_FORTIFY_LEVEL >= 1
1362 if (ptsname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1370 confstr (_CS_GNU_LIBC_VERSION
, largebuf
, sizeof (largebuf
));
1371 # if __USE_FORTIFY_LEVEL >= 1
1374 confstr (_CS_GNU_LIBC_VERSION
, smallbuf
, sizeof (largebuf
));
1380 int ngr
= getgroups (5, grpslarge
);
1381 asm volatile ("" : : "r" (ngr
));
1382 #if __USE_FORTIFY_LEVEL >= 1
1385 ngr
= getgroups (5, (gid_t
*) smallbuf
);
1386 asm volatile ("" : : "r" (ngr
));
1390 fd
= open (_PATH_TTY
, O_RDONLY
);
1394 if (ttyname_r (fd
, enough
, sizeof (enough
)) != 0)
1397 #if __USE_FORTIFY_LEVEL >= 1
1400 if (ttyname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1407 char hostnamelarge
[1000];
1408 gethostname (hostnamelarge
, sizeof (hostnamelarge
));
1409 #if __USE_FORTIFY_LEVEL >= 1
1412 gethostname (smallbuf
, sizeof (hostnamelarge
));
1416 char loginlarge
[1000];
1417 getlogin_r (loginlarge
, sizeof (hostnamelarge
));
1418 #if __USE_FORTIFY_LEVEL >= 1
1421 getlogin_r (smallbuf
, sizeof (loginlarge
));
1425 char domainnamelarge
[1000];
1426 int res
= getdomainname (domainnamelarge
, sizeof (domainnamelarge
));
1427 asm volatile ("" : : "r" (res
));
1428 #if __USE_FORTIFY_LEVEL >= 1
1431 res
= getdomainname (smallbuf
, sizeof (domainnamelarge
));
1432 asm volatile ("" : : "r" (res
));
1438 FD_SET (FD_SETSIZE
- 1, &s
);
1439 #if __USE_FORTIFY_LEVEL >= 1
1441 FD_SET (FD_SETSIZE
, &s
);
1444 FD_CLR (FD_SETSIZE
- 1, &s
);
1445 #if __USE_FORTIFY_LEVEL >= 1
1447 FD_CLR (FD_SETSIZE
, &s
);
1450 FD_ISSET (FD_SETSIZE
- 1, &s
);
1451 #if __USE_FORTIFY_LEVEL >= 1
1453 FD_ISSET (FD_SETSIZE
, &s
);
1457 struct pollfd fds
[1];
1458 fds
[0].fd
= STDOUT_FILENO
;
1459 fds
[0].events
= POLLOUT
;
1461 #if __USE_FORTIFY_LEVEL >= 1
1466 ppoll (fds
, 1, NULL
, NULL
);
1467 #if __USE_FORTIFY_LEVEL >= 1
1469 ppoll (fds
, 2, NULL
, NULL
);