1 /* Copyright (C) 2004-2016 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 memcpy (buf
, "abcdefghij", 10);
147 bcopy (buf
, buf
+ 1, 9);
148 if (memcmp (buf
, "aabcdefghi", 10))
151 if (mempcpy (buf
+ 5, "abcde", 5) != buf
+ 10
152 || memcmp (buf
, "aabcdabcde", 10))
155 memset (buf
+ 8, 'j', 2);
156 if (memcmp (buf
, "aabcdabcjj", 10))
160 if (memcmp (buf
, "aabcdabc\0\0", 10))
163 strcpy (buf
+ 4, "EDCBA");
164 if (memcmp (buf
, "aabcEDCBA", 10))
167 if (stpcpy (buf
+ 8, "F") != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
170 strncpy (buf
+ 6, "X", 4);
171 if (memcmp (buf
, "aabcEDX\0\0", 10))
174 if (sprintf (buf
+ 7, "%s", "67") != 2 || memcmp (buf
, "aabcEDX67", 10))
177 if (snprintf (buf
+ 7, 3, "%s", "987654") != 6
178 || memcmp (buf
, "aabcEDX98", 10))
181 /* These ops need runtime checking, but shouldn't __chk_fail. */
182 memcpy (buf
, "abcdefghij", l0
+ 10);
183 memmove (buf
+ 1, buf
, l0
+ 9);
184 if (memcmp (buf
, "aabcdefghi", 10))
187 memcpy (buf
, "abcdefghij", l0
+ 10);
188 bcopy (buf
, buf
+ 1, l0
+ 9);
189 if (memcmp (buf
, "aabcdefghi", 10))
192 if (mempcpy (buf
+ 5, "abcde", l0
+ 5) != buf
+ 10
193 || memcmp (buf
, "aabcdabcde", 10))
196 memset (buf
+ 8, 'j', l0
+ 2);
197 if (memcmp (buf
, "aabcdabcjj", 10))
200 bzero (buf
+ 8, l0
+ 2);
201 if (memcmp (buf
, "aabcdabc\0\0", 10))
204 strcpy (buf
+ 4, str1
+ 5);
205 if (memcmp (buf
, "aabcEDCBA", 10))
208 if (stpcpy (buf
+ 8, str2
) != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
211 strncpy (buf
+ 6, "X", l0
+ 4);
212 if (memcmp (buf
, "aabcEDX\0\0", 10))
215 if (stpncpy (buf
+ 5, "cd", l0
+ 5) != buf
+ 7
216 || memcmp (buf
, "aabcEcd\0\0", 10))
219 if (sprintf (buf
+ 7, "%d", num1
) != 2 || memcmp (buf
, "aabcEcd67", 10))
222 if (snprintf (buf
+ 7, 3, "%d", num2
) != 6 || memcmp (buf
, "aabcEcd98", 10))
227 if (memcmp (buf
, "aabcEcd9A", 10))
231 strncat (buf
, "ZYXWV", l0
+ 2);
232 if (memcmp (buf
, "aabcEcdZY", 10))
235 /* The following tests are supposed to succeed at all fortify
236 levels, even though they overflow a.buf1 into a.buf2. */
237 memcpy (a
.buf1
, "abcdefghij", l0
+ 10);
238 memmove (a
.buf1
+ 1, a
.buf1
, l0
+ 9);
239 if (memcmp (a
.buf1
, "aabcdefghi", 10))
242 memcpy (a
.buf1
, "abcdefghij", l0
+ 10);
243 bcopy (a
.buf1
, a
.buf1
+ 1, l0
+ 9);
244 if (memcmp (a
.buf1
, "aabcdefghi", 10))
247 if (mempcpy (a
.buf1
+ 5, "abcde", l0
+ 5) != a
.buf1
+ 10
248 || memcmp (a
.buf1
, "aabcdabcde", 10))
251 memset (a
.buf1
+ 8, 'j', l0
+ 2);
252 if (memcmp (a
.buf1
, "aabcdabcjj", 10))
255 bzero (a
.buf1
+ 8, l0
+ 2);
256 if (memcmp (a
.buf1
, "aabcdabc\0\0", 10))
259 #if __USE_FORTIFY_LEVEL < 2
260 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
261 and sufficient GCC support, as the string operations overflow
262 from a.buf1 into a.buf2. */
263 strcpy (a
.buf1
+ 4, str1
+ 5);
264 if (memcmp (a
.buf1
, "aabcEDCBA", 10))
267 if (stpcpy (a
.buf1
+ 8, str2
) != a
.buf1
+ 9
268 || memcmp (a
.buf1
, "aabcEDCBF", 10))
271 strncpy (a
.buf1
+ 6, "X", l0
+ 4);
272 if (memcmp (a
.buf1
, "aabcEDX\0\0", 10))
275 if (sprintf (a
.buf1
+ 7, "%d", num1
) != 2
276 || memcmp (a
.buf1
, "aabcEDX67", 10))
279 if (snprintf (a
.buf1
+ 7, 3, "%d", num2
) != 6
280 || memcmp (a
.buf1
, "aabcEDX98", 10))
283 a
.buf1
[l0
+ 8] = '\0';
284 strcat (a
.buf1
, "A");
285 if (memcmp (a
.buf1
, "aabcEDX9A", 10))
288 a
.buf1
[l0
+ 7] = '\0';
289 strncat (a
.buf1
, "ZYXWV", l0
+ 2);
290 if (memcmp (a
.buf1
, "aabcEDXZY", 10))
295 #if __USE_FORTIFY_LEVEL >= 1
296 /* Now check if all buffer overflows are caught at runtime.
297 N.B. All tests involving a length parameter need to be done
298 twice: once with the length a compile-time constant, once without. */
301 memcpy (buf
+ 1, "abcdefghij", 10);
305 memcpy (buf
+ 1, "abcdefghij", l0
+ 10);
309 memmove (buf
+ 2, buf
+ 1, 9);
313 memmove (buf
+ 2, buf
+ 1, l0
+ 9);
317 bcopy (buf
+ 1, buf
+ 2, 9);
321 bcopy (buf
+ 1, buf
+ 2, l0
+ 9);
325 p
= (char *) mempcpy (buf
+ 6, "abcde", 5);
329 p
= (char *) mempcpy (buf
+ 6, "abcde", l0
+ 5);
333 memset (buf
+ 9, 'j', 2);
337 memset (buf
+ 9, 'j', l0
+ 2);
345 bzero (buf
+ 9, l0
+ 2);
349 strcpy (buf
+ 5, str1
+ 5);
353 p
= stpcpy (buf
+ 9, str2
);
357 strncpy (buf
+ 7, "X", 4);
361 strncpy (buf
+ 7, "X", l0
+ 4);
365 stpncpy (buf
+ 6, "cd", 5);
369 stpncpy (buf
+ 6, "cd", l0
+ 5);
372 # if !defined __cplusplus || defined __va_arg_pack
374 sprintf (buf
+ 8, "%d", num1
);
378 snprintf (buf
+ 8, 3, "%d", num2
);
382 snprintf (buf
+ 8, l0
+ 3, "%d", num2
);
386 swprintf (wbuf
+ 8, 3, L
"%d", num1
);
390 swprintf (wbuf
+ 8, l0
+ 3, L
"%d", num1
);
394 memcpy (buf
, str1
+ 2, 9);
399 memcpy (buf
, str1
+ 3, 8);
401 strncat (buf
, "ZYXWV", 3);
404 memcpy (buf
, str1
+ 3, 8);
406 strncat (buf
, "ZYXWV", l0
+ 3);
410 memcpy (a
.buf1
+ 1, "abcdefghij", 10);
414 memcpy (a
.buf1
+ 1, "abcdefghij", l0
+ 10);
418 memmove (a
.buf1
+ 2, a
.buf1
+ 1, 9);
422 memmove (a
.buf1
+ 2, a
.buf1
+ 1, l0
+ 9);
426 bcopy (a
.buf1
+ 1, a
.buf1
+ 2, 9);
430 bcopy (a
.buf1
+ 1, a
.buf1
+ 2, l0
+ 9);
434 p
= (char *) mempcpy (a
.buf1
+ 6, "abcde", 5);
438 p
= (char *) mempcpy (a
.buf1
+ 6, "abcde", l0
+ 5);
442 memset (a
.buf1
+ 9, 'j', 2);
446 memset (a
.buf1
+ 9, 'j', l0
+ 2);
450 bzero (a
.buf1
+ 9, 2);
454 bzero (a
.buf1
+ 9, l0
+ 2);
457 # if __USE_FORTIFY_LEVEL >= 2
464 strcpy (a
.buf1
+ (O
+ 4), str1
+ 5);
468 p
= stpcpy (a
.buf1
+ (O
+ 8), str2
);
472 strncpy (a
.buf1
+ (O
+ 6), "X", 4);
476 strncpy (a
.buf1
+ (O
+ 6), "X", l0
+ 4);
479 # if !defined __cplusplus || defined __va_arg_pack
481 sprintf (a
.buf1
+ (O
+ 7), "%d", num1
);
485 snprintf (a
.buf1
+ (O
+ 7), 3, "%d", num2
);
489 snprintf (a
.buf1
+ (O
+ 7), l0
+ 3, "%d", num2
);
493 memcpy (a
.buf1
, str1
+ (3 - O
), 8 + O
);
495 strcat (a
.buf1
, "AB");
498 memcpy (a
.buf1
, str1
+ (4 - O
), 7 + O
);
500 strncat (a
.buf1
, "ZYXWV", l0
+ 3);
505 /* These ops can be done without runtime checking of object size. */
506 wmemcpy (wbuf
, L
"abcdefghij", 10);
507 wmemmove (wbuf
+ 1, wbuf
, 9);
508 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
511 if (wmempcpy (wbuf
+ 5, L
"abcde", 5) != wbuf
+ 10
512 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
515 wmemset (wbuf
+ 8, L
'j', 2);
516 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
519 wcscpy (wbuf
+ 4, L
"EDCBA");
520 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
523 if (wcpcpy (wbuf
+ 8, L
"F") != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
526 wcsncpy (wbuf
+ 6, L
"X", 4);
527 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
530 if (swprintf (wbuf
+ 7, 3, L
"%ls", L
"987654") >= 0
531 || wmemcmp (wbuf
, L
"aabcEDX98", 10))
534 if (swprintf (wbuf
+ 7, 3, L
"64") != 2
535 || wmemcmp (wbuf
, L
"aabcEDX64", 10))
538 /* These ops need runtime checking, but shouldn't __chk_fail. */
539 wmemcpy (wbuf
, L
"abcdefghij", l0
+ 10);
540 wmemmove (wbuf
+ 1, wbuf
, l0
+ 9);
541 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
544 if (wmempcpy (wbuf
+ 5, L
"abcde", l0
+ 5) != wbuf
+ 10
545 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
548 wmemset (wbuf
+ 8, L
'j', l0
+ 2);
549 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
552 wcscpy (wbuf
+ 4, wstr1
+ 5);
553 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
556 if (wcpcpy (wbuf
+ 8, wstr2
) != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
559 wcsncpy (wbuf
+ 6, L
"X", l0
+ 4);
560 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
563 if (wcpncpy (wbuf
+ 5, L
"cd", l0
+ 5) != wbuf
+ 7
564 || wmemcmp (wbuf
, L
"aabcEcd\0\0", 10))
567 if (swprintf (wbuf
+ 7, 3, L
"%d", num2
) >= 0
568 || wmemcmp (wbuf
, L
"aabcEcd98", 10))
571 wbuf
[l0
+ 8] = L
'\0';
573 if (wmemcmp (wbuf
, L
"aabcEcd9A", 10))
576 wbuf
[l0
+ 7] = L
'\0';
577 wcsncat (wbuf
, L
"ZYXWV", l0
+ 2);
578 if (wmemcmp (wbuf
, L
"aabcEcdZY", 10))
581 wmemcpy (wa
.buf1
, L
"abcdefghij", l0
+ 10);
582 wmemmove (wa
.buf1
+ 1, wa
.buf1
, l0
+ 9);
583 if (wmemcmp (wa
.buf1
, L
"aabcdefghi", 10))
586 if (wmempcpy (wa
.buf1
+ 5, L
"abcde", l0
+ 5) != wa
.buf1
+ 10
587 || wmemcmp (wa
.buf1
, L
"aabcdabcde", 10))
590 wmemset (wa
.buf1
+ 8, L
'j', l0
+ 2);
591 if (wmemcmp (wa
.buf1
, L
"aabcdabcjj", 10))
594 #if __USE_FORTIFY_LEVEL < 2
595 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
596 and sufficient GCC support, as the string operations overflow
597 from a.buf1 into a.buf2. */
598 wcscpy (wa
.buf1
+ 4, wstr1
+ 5);
599 if (wmemcmp (wa
.buf1
, L
"aabcEDCBA", 10))
602 if (wcpcpy (wa
.buf1
+ 8, wstr2
) != wa
.buf1
+ 9
603 || wmemcmp (wa
.buf1
, L
"aabcEDCBF", 10))
606 wcsncpy (wa
.buf1
+ 6, L
"X", l0
+ 4);
607 if (wmemcmp (wa
.buf1
, L
"aabcEDX\0\0", 10))
610 if (swprintf (wa
.buf1
+ 7, 3, L
"%d", num2
) >= 0
611 || wmemcmp (wa
.buf1
, L
"aabcEDX98", 10))
614 wa
.buf1
[l0
+ 8] = L
'\0';
615 wcscat (wa
.buf1
, L
"A");
616 if (wmemcmp (wa
.buf1
, L
"aabcEDX9A", 10))
619 wa
.buf1
[l0
+ 7] = L
'\0';
620 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 2);
621 if (wmemcmp (wa
.buf1
, L
"aabcEDXZY", 10))
626 #if __USE_FORTIFY_LEVEL >= 1
627 /* Now check if all buffer overflows are caught at runtime.
628 N.B. All tests involving a length parameter need to be done
629 twice: once with the length a compile-time constant, once without. */
632 wmemcpy (wbuf
+ 1, L
"abcdefghij", 10);
636 wmemcpy (wbuf
+ 1, L
"abcdefghij", l0
+ 10);
640 wmemcpy (wbuf
+ 9, L
"abcdefghij", 10);
644 wmemcpy (wbuf
+ 9, L
"abcdefghij", l0
+ 10);
648 wmemmove (wbuf
+ 2, wbuf
+ 1, 9);
652 wmemmove (wbuf
+ 2, wbuf
+ 1, l0
+ 9);
656 wp
= wmempcpy (wbuf
+ 6, L
"abcde", 5);
660 wp
= wmempcpy (wbuf
+ 6, L
"abcde", l0
+ 5);
664 wmemset (wbuf
+ 9, L
'j', 2);
668 wmemset (wbuf
+ 9, L
'j', l0
+ 2);
672 wcscpy (wbuf
+ 5, wstr1
+ 5);
676 wp
= wcpcpy (wbuf
+ 9, wstr2
);
680 wcsncpy (wbuf
+ 7, L
"X", 4);
684 wcsncpy (wbuf
+ 7, L
"X", l0
+ 4);
688 wcsncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
692 wcpncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
696 wcpncpy (wbuf
+ 6, L
"cd", 5);
700 wcpncpy (wbuf
+ 6, L
"cd", l0
+ 5);
703 wmemcpy (wbuf
, wstr1
+ 2, 9);
705 wcscat (wbuf
, L
"AB");
708 wmemcpy (wbuf
, wstr1
+ 3, 8);
710 wcsncat (wbuf
, L
"ZYXWV", l0
+ 3);
714 wmemcpy (wa
.buf1
+ 1, L
"abcdefghij", 10);
718 wmemcpy (wa
.buf1
+ 1, L
"abcdefghij", l0
+ 10);
722 wmemmove (wa
.buf1
+ 2, wa
.buf1
+ 1, 9);
726 wmemmove (wa
.buf1
+ 2, wa
.buf1
+ 1, l0
+ 9);
730 wp
= wmempcpy (wa
.buf1
+ 6, L
"abcde", 5);
734 wp
= wmempcpy (wa
.buf1
+ 6, L
"abcde", l0
+ 5);
738 wmemset (wa
.buf1
+ 9, L
'j', 2);
742 wmemset (wa
.buf1
+ 9, L
'j', l0
+ 2);
745 #if __USE_FORTIFY_LEVEL >= 2
752 wcscpy (wa
.buf1
+ (O
+ 4), wstr1
+ 5);
756 wp
= wcpcpy (wa
.buf1
+ (O
+ 8), wstr2
);
760 wcsncpy (wa
.buf1
+ (O
+ 6), L
"X", 4);
764 wcsncpy (wa
.buf1
+ (O
+ 6), L
"X", l0
+ 4);
767 wmemcpy (wa
.buf1
, wstr1
+ (3 - O
), 8 + O
);
769 wcscat (wa
.buf1
, L
"AB");
772 wmemcpy (wa
.buf1
, wstr1
+ (4 - O
), 7 + O
);
774 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 3);
779 /* Now checks for %n protection. */
781 /* Constant literals passed directly are always ok
782 (even with warnings about possible bugs from GCC). */
784 if (sprintf (buf
, "%s%n%s%n", str2
, &n1
, str2
, &n2
) != 2
785 || n1
!= 1 || n2
!= 2)
788 /* In this case the format string is not known at compile time,
789 but resides in read-only memory, so is ok. */
790 if (snprintf (buf
, 4, str3
, str2
, &n1
, str2
, &n2
) != 2
791 || n1
!= 1 || n2
!= 2)
794 strcpy (buf2
+ 2, "%n%s%n");
795 /* When the format string is writable and contains %n,
796 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
798 if (sprintf (buf
, buf2
, str2
, &n1
, str2
, &n1
) != 2)
803 if (snprintf (buf
, 3, buf2
, str2
, &n1
, str2
, &n1
) != 2)
807 /* But if there is no %n, even writable format string
810 if (sprintf (buf
, buf2
+ 4, str2
) != 1)
813 /* Constant literals passed directly are always ok
814 (even with warnings about possible bugs from GCC). */
815 if (printf ("%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
816 || n1
!= 7 || n2
!= 14)
819 /* In this case the format string is not known at compile time,
820 but resides in read-only memory, so is ok. */
821 if (printf (str3
, str4
, &n1
, str5
, &n2
) != 14
822 || n1
!= 7 || n2
!= 14)
825 strcpy (buf2
+ 2, "%n%s%n");
826 /* When the format string is writable and contains %n,
827 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
829 if (printf (buf2
, str4
, &n1
, str5
, &n1
) != 14)
833 /* But if there is no %n, even writable format string
836 if (printf (buf2
+ 4, str5
) != 7)
841 /* Constant literals passed directly are always ok
842 (even with warnings about possible bugs from GCC). */
843 if (fprintf (fp
, "%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
844 || n1
!= 7 || n2
!= 14)
847 /* In this case the format string is not known at compile time,
848 but resides in read-only memory, so is ok. */
849 if (fprintf (fp
, str3
, str4
, &n1
, str5
, &n2
) != 14
850 || n1
!= 7 || n2
!= 14)
853 strcpy (buf2
+ 2, "%n%s%n");
854 /* When the format string is writable and contains %n,
855 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
857 if (fprintf (fp
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
861 /* But if there is no %n, even writable format string
864 if (fprintf (fp
, buf2
+ 4, str5
) != 7)
868 strcpy (buf2
+ 2, "%n%s%n");
869 /* When the format string is writable and contains %n,
870 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
872 if (asprintf (&my_ptr
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
881 if (obstack_printf (&obs
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
884 obstack_free (&obs
, NULL
);
887 if (asprintf (&my_ptr
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
893 if (obstack_printf (&obs
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
895 obstack_free (&obs
, NULL
);
897 if (freopen (temp_filename
, "r", stdin
) == NULL
)
899 puts ("could not open temporary file");
903 if (gets (buf
) != buf
|| memcmp (buf
, "abcdefgh", 9))
905 if (gets (buf
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
908 #if __USE_FORTIFY_LEVEL >= 1
910 if (gets (buf
) != buf
)
917 if (fgets (buf
, sizeof (buf
), stdin
) != buf
918 || memcmp (buf
, "abcdefgh\n", 10))
920 if (fgets (buf
, sizeof (buf
), stdin
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
925 if (fgets (buf
, l0
+ sizeof (buf
), stdin
) != buf
926 || memcmp (buf
, "abcdefgh\n", 10))
929 #if __USE_FORTIFY_LEVEL >= 1
931 if (fgets (buf
, sizeof (buf
) + 1, stdin
) != buf
)
936 if (fgets (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
943 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
944 || memcmp (buf
, "abcdefgh\n", 10))
946 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
947 || memcmp (buf
, "ABCDEFGHI", 10))
952 if (fgets_unlocked (buf
, l0
+ sizeof (buf
), stdin
) != buf
953 || memcmp (buf
, "abcdefgh\n", 10))
956 #if __USE_FORTIFY_LEVEL >= 1
958 if (fgets_unlocked (buf
, sizeof (buf
) + 1, stdin
) != buf
)
963 if (fgets_unlocked (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
970 if (fread (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
971 || memcmp (buf
, "abcdefgh\nA", 10))
973 if (fread (buf
, sizeof (buf
), 1, stdin
) != 1
974 || memcmp (buf
, "BCDEFGHI\na", 10))
979 if (fread (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
980 || memcmp (buf
, "abcdefgh\nA", 10))
982 if (fread (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
983 || memcmp (buf
, "BCDEFGHI\na", 10))
986 #if __USE_FORTIFY_LEVEL >= 1
988 if (fread (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
993 if (fread (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
1000 if (fread_unlocked (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
1001 || memcmp (buf
, "abcdefgh\nA", 10))
1003 if (fread_unlocked (buf
, sizeof (buf
), 1, stdin
) != 1
1004 || memcmp (buf
, "BCDEFGHI\na", 10))
1009 if (fread_unlocked (buf
, 1, 4, stdin
) != 4
1010 || memcmp (buf
, "abcdFGHI\na", 10))
1012 if (fread_unlocked (buf
, 4, 1, stdin
) != 1
1013 || memcmp (buf
, "efghFGHI\na", 10))
1018 if (fread_unlocked (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
1019 || memcmp (buf
, "abcdefgh\nA", 10))
1021 if (fread_unlocked (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
1022 || memcmp (buf
, "BCDEFGHI\na", 10))
1025 #if __USE_FORTIFY_LEVEL >= 1
1027 if (fread_unlocked (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
1032 if (fread_unlocked (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
1037 lseek (fileno (stdin
), 0, SEEK_SET
);
1039 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
1040 || memcmp (buf
, "abcdefgh\n", 9))
1042 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
1043 || memcmp (buf
, "ABCDEFGHI", 9))
1046 lseek (fileno (stdin
), 0, SEEK_SET
);
1048 if (read (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1) != sizeof (buf
) - 1
1049 || memcmp (buf
, "abcdefgh\n", 9))
1052 #if __USE_FORTIFY_LEVEL >= 1
1054 if (read (fileno (stdin
), buf
, sizeof (buf
) + 1) != sizeof (buf
) + 1)
1059 if (read (fileno (stdin
), buf
, l0
+ sizeof (buf
) + 1) != sizeof (buf
) + 1)
1064 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
1066 || memcmp (buf
, "\nABCDEFGH", 9))
1068 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
1069 || memcmp (buf
, "abcdefgh\n", 9))
1071 if (pread (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
1073 || memcmp (buf
, "h\nABCDEFG", 9))
1076 #if __USE_FORTIFY_LEVEL >= 1
1078 if (pread (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
1079 != sizeof (buf
) + 1)
1084 if (pread (fileno (stdin
), buf
, l0
+ sizeof (buf
) + 1, 2 * sizeof (buf
))
1085 != sizeof (buf
) + 1)
1090 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
1092 || memcmp (buf
, "\nABCDEFGH", 9))
1094 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
1095 || memcmp (buf
, "abcdefgh\n", 9))
1097 if (pread64 (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
1099 || memcmp (buf
, "h\nABCDEFG", 9))
1102 #if __USE_FORTIFY_LEVEL >= 1
1104 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
1105 != sizeof (buf
) + 1)
1110 if (pread64 (fileno (stdin
), buf
, l0
+ sizeof (buf
) + 1, 2 * sizeof (buf
))
1111 != sizeof (buf
) + 1)
1116 if (freopen (temp_filename
, "r", stdin
) == NULL
)
1118 puts ("could not open temporary file");
1122 if (fseek (stdin
, 9 + 10 + 11, SEEK_SET
))
1124 puts ("could not seek in test file");
1128 #if __USE_FORTIFY_LEVEL >= 1
1130 if (gets (buf
) != buf
)
1135 /* Check whether missing N$ formats are detected. */
1137 printf ("%3$d\n", 1, 2, 3, 4);
1141 fprintf (stdout
, "%3$d\n", 1, 2, 3, 4);
1145 sprintf (buf
, "%3$d\n", 1, 2, 3, 4);
1149 snprintf (buf
, sizeof (buf
), "%3$d\n", 1, 2, 3, 4);
1153 if (socketpair (PF_UNIX
, SOCK_STREAM
, 0, sp
))
1157 const char *sendstr
= "abcdefgh\nABCDEFGH\n0123456789\n";
1158 if ((size_t) send (sp
[0], sendstr
, strlen (sendstr
), 0)
1159 != strlen (sendstr
))
1163 if (recv (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
)
1165 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
1168 if (recv (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
)
1169 != sizeof recvbuf
- 7
1170 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
1173 #if __USE_FORTIFY_LEVEL >= 1
1175 if (recv (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
)
1181 if (recv (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
)
1182 != sizeof recvbuf
- 3)
1188 struct sockaddr_un sa_un
;
1190 sl
= sizeof (sa_un
);
1191 if (recvfrom (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
,
1192 (struct sockaddr
*) &sa_un
, &sl
)
1194 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
1197 sl
= sizeof (sa_un
);
1198 if (recvfrom (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
,
1199 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 7
1200 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
1203 #if __USE_FORTIFY_LEVEL >= 1
1205 sl
= sizeof (sa_un
);
1206 if (recvfrom (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
,
1207 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
)
1212 sl
= sizeof (sa_un
);
1213 if (recvfrom (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
,
1214 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 3)
1223 char fname
[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1224 char *enddir
= strchr (fname
, '\0');
1225 if (mkdtemp (fname
) == NULL
)
1227 printf ("mkdtemp failed: %m\n");
1231 if (symlink ("bar", fname
) != 0)
1234 char readlinkbuf
[4];
1235 if (readlink (fname
, readlinkbuf
, 4) != 3
1236 || memcmp (readlinkbuf
, "bar", 3) != 0)
1238 if (readlink (fname
, readlinkbuf
+ 1, l0
+ 3) != 3
1239 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1242 #if __USE_FORTIFY_LEVEL >= 1
1244 if (readlink (fname
, readlinkbuf
+ 2, l0
+ 3) != 3)
1249 if (readlink (fname
, readlinkbuf
+ 3, 4) != 3)
1254 int tmpfd
= open ("/tmp", O_RDONLY
| O_DIRECTORY
);
1258 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
, 4) != 3
1259 || memcmp (readlinkbuf
, "bar", 3) != 0)
1261 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 1,
1263 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1266 #if __USE_FORTIFY_LEVEL >= 1
1268 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 2,
1274 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 3,
1282 char *cwd1
= getcwd (NULL
, 0);
1286 char *cwd2
= getcwd (NULL
, 250);
1292 if (strcmp (cwd1
, cwd2
) != 0)
1299 char *cwd3
= getcwd (NULL
, 0);
1302 if (strcmp (fname
, cwd3
) != 0)
1303 printf ("getcwd after chdir is '%s' != '%s',"
1304 "get{c,}wd tests skipped\n", cwd3
, fname
);
1307 char getcwdbuf
[sizeof fname
- 3];
1309 char *cwd4
= getcwd (getcwdbuf
, sizeof getcwdbuf
);
1310 if (cwd4
!= getcwdbuf
1311 || strcmp (getcwdbuf
, fname
) != 0)
1314 cwd4
= getcwd (getcwdbuf
+ 1, l0
+ sizeof getcwdbuf
- 1);
1315 if (cwd4
!= getcwdbuf
+ 1
1316 || getcwdbuf
[0] != fname
[0]
1317 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1320 #if __USE_FORTIFY_LEVEL >= 1
1322 if (getcwd (getcwdbuf
+ 2, l0
+ sizeof getcwdbuf
)
1328 if (getcwd (getcwdbuf
+ 2, sizeof getcwdbuf
)
1334 if (getwd (getcwdbuf
) != getcwdbuf
1335 || strcmp (getcwdbuf
, fname
) != 0)
1338 if (getwd (getcwdbuf
+ 1) != getcwdbuf
+ 1
1339 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1342 #if __USE_FORTIFY_LEVEL >= 1
1344 if (getwd (getcwdbuf
+ 2) != getcwdbuf
+ 2)
1350 if (chdir (cwd1
) != 0)
1358 if (unlink (fname
) != 0)
1362 if (rmdir (fname
) != 0)
1367 char largebuf
[PATH_MAX
];
1368 char *realres
= realpath (".", largebuf
);
1369 if (realres
!= largebuf
)
1372 # if __USE_FORTIFY_LEVEL >= 1
1375 realres
= realpath (".", realbuf
);
1376 if (realres
!= realbuf
)
1382 if (setlocale (LC_ALL
, "de_DE.UTF-8") != NULL
)
1384 assert (MB_CUR_MAX
<= 10);
1386 /* First a simple test. */
1388 if (wctomb (enough
, L
'A') != 1)
1391 #if __USE_FORTIFY_LEVEL >= 1
1392 /* We know the wchar_t encoding is ISO 10646. So pick a
1393 character which has a multibyte representation which does not
1397 if (wctomb (smallbuf
, L
'\x100') != 2)
1403 memset (&s
, '\0', sizeof (s
));
1404 if (wcrtomb (enough
, L
'D', &s
) != 1 || enough
[0] != 'D')
1407 #if __USE_FORTIFY_LEVEL >= 1
1408 /* We know the wchar_t encoding is ISO 10646. So pick a
1409 character which has a multibyte representation which does not
1413 if (wcrtomb (smallbuf
, L
'\x100', &s
) != 2)
1418 wchar_t wenough
[10];
1419 memset (&s
, '\0', sizeof (s
));
1420 const char *cp
= "A";
1421 if (mbsrtowcs (wenough
, &cp
, 10, &s
) != 1
1422 || wcscmp (wenough
, L
"A") != 0)
1426 if (mbsrtowcs (wenough
, &cp
, l0
+ 10, &s
) != 2
1427 || wcscmp (wenough
, L
"BC") != 0)
1430 #if __USE_FORTIFY_LEVEL >= 1
1432 wchar_t wsmallbuf
[2];
1434 mbsrtowcs (wsmallbuf
, &cp
, 10, &s
);
1439 if (mbstowcs (wenough
, cp
, 10) != 1
1440 || wcscmp (wenough
, L
"A") != 0)
1444 if (mbstowcs (wenough
, cp
, l0
+ 10) != 3
1445 || wcscmp (wenough
, L
"DEF") != 0)
1448 #if __USE_FORTIFY_LEVEL >= 1
1450 wchar_t wsmallbuf
[2];
1452 mbstowcs (wsmallbuf
, cp
, 10);
1456 memset (&s
, '\0', sizeof (s
));
1458 wcscpy (wenough
, L
"DEF");
1459 if (mbsnrtowcs (wenough
, &cp
, 1, 10, &s
) != 1
1460 || wcscmp (wenough
, L
"AEF") != 0)
1464 if (mbsnrtowcs (wenough
, &cp
, 1, l0
+ 10, &s
) != 1
1465 || wcscmp (wenough
, L
"IEF") != 0)
1468 #if __USE_FORTIFY_LEVEL >= 1
1470 wchar_t wsmallbuf
[2];
1472 mbsnrtowcs (wsmallbuf
, &cp
, 3, 10, &s
);
1476 memset (&s
, '\0', sizeof (s
));
1477 const wchar_t *wcp
= L
"A";
1478 if (wcsrtombs (enough
, &wcp
, 10, &s
) != 1
1479 || strcmp (enough
, "A") != 0)
1483 if (wcsrtombs (enough
, &wcp
, l0
+ 10, &s
) != 2
1484 || strcmp (enough
, "BC") != 0)
1487 #if __USE_FORTIFY_LEVEL >= 1
1491 wcsrtombs (smallbuf
, &wcp
, 10, &s
);
1495 memset (enough
, 'Z', sizeof (enough
));
1497 if (wcstombs (enough
, wcp
, 10) != 2
1498 || strcmp (enough
, "EF") != 0)
1502 if (wcstombs (enough
, wcp
, l0
+ 10) != 1
1503 || strcmp (enough
, "G") != 0)
1506 #if __USE_FORTIFY_LEVEL >= 1
1510 wcstombs (smallbuf
, wcp
, 10);
1514 memset (&s
, '\0', sizeof (s
));
1516 if (wcsnrtombs (enough
, &wcp
, 1, 10, &s
) != 1
1517 || strcmp (enough
, "A") != 0)
1521 if (wcsnrtombs (enough
, &wcp
, 1, l0
+ 10, &s
) != 1
1522 || strcmp (enough
, "B") != 0)
1525 #if __USE_FORTIFY_LEVEL >= 1
1529 wcsnrtombs (smallbuf
, &wcp
, 3, 10, &s
);
1535 puts ("cannot set locale");
1539 int fd
= posix_openpt (O_RDWR
);
1543 if (ptsname_r (fd
, enough
, sizeof (enough
)) != 0)
1546 #if __USE_FORTIFY_LEVEL >= 1
1549 if (ptsname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1557 confstr (_CS_GNU_LIBC_VERSION
, largebuf
, sizeof (largebuf
));
1558 # if __USE_FORTIFY_LEVEL >= 1
1561 confstr (_CS_GNU_LIBC_VERSION
, smallbuf
, sizeof (largebuf
));
1567 int ngr
= getgroups (5, grpslarge
);
1568 asm volatile ("" : : "r" (ngr
));
1569 #if __USE_FORTIFY_LEVEL >= 1
1572 ngr
= getgroups (5, (gid_t
*) smallbuf
);
1573 asm volatile ("" : : "r" (ngr
));
1577 fd
= open (_PATH_TTY
, O_RDONLY
);
1581 if (ttyname_r (fd
, enough
, sizeof (enough
)) != 0)
1584 #if __USE_FORTIFY_LEVEL >= 1
1587 if (ttyname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1594 char hostnamelarge
[1000];
1595 gethostname (hostnamelarge
, sizeof (hostnamelarge
));
1596 #if __USE_FORTIFY_LEVEL >= 1
1599 gethostname (smallbuf
, sizeof (hostnamelarge
));
1603 char loginlarge
[1000];
1604 getlogin_r (loginlarge
, sizeof (hostnamelarge
));
1605 #if __USE_FORTIFY_LEVEL >= 1
1608 getlogin_r (smallbuf
, sizeof (loginlarge
));
1612 char domainnamelarge
[1000];
1613 int res
= getdomainname (domainnamelarge
, sizeof (domainnamelarge
));
1614 asm volatile ("" : : "r" (res
));
1615 #if __USE_FORTIFY_LEVEL >= 1
1618 res
= getdomainname (smallbuf
, sizeof (domainnamelarge
));
1619 asm volatile ("" : : "r" (res
));
1626 FD_SET (FD_SETSIZE
- 1, &s
);
1627 #if __USE_FORTIFY_LEVEL >= 1
1629 FD_SET (FD_SETSIZE
, &s
);
1633 FD_SET (l0
+ FD_SETSIZE
, &s
);
1637 FD_CLR (FD_SETSIZE
- 1, &s
);
1638 #if __USE_FORTIFY_LEVEL >= 1
1640 FD_CLR (FD_SETSIZE
, &s
);
1644 FD_SET (l0
+ FD_SETSIZE
, &s
);
1648 FD_ISSET (FD_SETSIZE
- 1, &s
);
1649 #if __USE_FORTIFY_LEVEL >= 1
1651 FD_ISSET (FD_SETSIZE
, &s
);
1655 FD_ISSET (l0
+ FD_SETSIZE
, &s
);
1659 struct pollfd fds
[1];
1660 fds
[0].fd
= STDOUT_FILENO
;
1661 fds
[0].events
= POLLOUT
;
1663 #if __USE_FORTIFY_LEVEL >= 1
1669 poll (fds
, l0
+ 2, 0);
1672 ppoll (fds
, 1, NULL
, NULL
);
1673 #if __USE_FORTIFY_LEVEL >= 1
1675 ppoll (fds
, 2, NULL
, NULL
);
1679 ppoll (fds
, l0
+ 2, NULL
, NULL
);