1 /* Copyright (C) 2004,2005,2006,2007,2008,2011 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
32 #include <sys/select.h>
33 #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
122 sa
.sa_handler
= handler
;
124 sigemptyset (&sa
.sa_mask
);
126 sigaction (SIGABRT
, &sa
, NULL
);
128 /* Avoid all the buffer overflow messages on stderr. */
129 int fd
= open (_PATH_DEVNULL
, O_WRONLY
);
131 close (STDERR_FILENO
);
134 dup2 (fd
, STDERR_FILENO
);
137 setenv ("LIBC_FATAL_STDERR_", "1", 1);
139 struct A
{ char buf1
[9]; char buf2
[1]; } a
;
140 struct wA
{ wchar_t buf1
[9]; wchar_t buf2
[1]; } wa
;
142 printf ("Test checking routines at fortify level %d\n",
143 #ifdef __USE_FORTIFY_LEVEL
144 (int) __USE_FORTIFY_LEVEL
150 #if defined __USE_FORTIFY_LEVEL && !defined __extern_always_inline
151 printf ("Test skipped");
156 /* These ops can be done without runtime checking of object size. */
157 memcpy (buf
, "abcdefghij", 10);
158 memmove (buf
+ 1, buf
, 9);
159 if (memcmp (buf
, "aabcdefghi", 10))
162 if (mempcpy (buf
+ 5, "abcde", 5) != buf
+ 10
163 || memcmp (buf
, "aabcdabcde", 10))
166 memset (buf
+ 8, 'j', 2);
167 if (memcmp (buf
, "aabcdabcjj", 10))
170 strcpy (buf
+ 4, "EDCBA");
171 if (memcmp (buf
, "aabcEDCBA", 10))
174 if (stpcpy (buf
+ 8, "F") != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
177 strncpy (buf
+ 6, "X", 4);
178 if (memcmp (buf
, "aabcEDX\0\0", 10))
181 if (sprintf (buf
+ 7, "%s", "67") != 2 || memcmp (buf
, "aabcEDX67", 10))
184 if (snprintf (buf
+ 7, 3, "%s", "987654") != 6
185 || memcmp (buf
, "aabcEDX98", 10))
188 /* These ops need runtime checking, but shouldn't __chk_fail. */
189 memcpy (buf
, "abcdefghij", l0
+ 10);
190 memmove (buf
+ 1, buf
, l0
+ 9);
191 if (memcmp (buf
, "aabcdefghi", 10))
194 if (mempcpy (buf
+ 5, "abcde", l0
+ 5) != buf
+ 10
195 || memcmp (buf
, "aabcdabcde", 10))
198 memset (buf
+ 8, 'j', l0
+ 2);
199 if (memcmp (buf
, "aabcdabcjj", 10))
202 strcpy (buf
+ 4, str1
+ 5);
203 if (memcmp (buf
, "aabcEDCBA", 10))
206 if (stpcpy (buf
+ 8, str2
) != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
209 strncpy (buf
+ 6, "X", l0
+ 4);
210 if (memcmp (buf
, "aabcEDX\0\0", 10))
213 if (stpncpy (buf
+ 5, "cd", l0
+ 5) != buf
+ 7
214 || memcmp (buf
, "aabcEcd\0\0", 10))
217 if (sprintf (buf
+ 7, "%d", num1
) != 2 || memcmp (buf
, "aabcEcd67", 10))
220 if (snprintf (buf
+ 7, 3, "%d", num2
) != 6 || memcmp (buf
, "aabcEcd98", 10))
225 if (memcmp (buf
, "aabcEcd9A", 10))
229 strncat (buf
, "ZYXWV", l0
+ 2);
230 if (memcmp (buf
, "aabcEcdZY", 10))
233 memcpy (a
.buf1
, "abcdefghij", l0
+ 10);
234 memmove (a
.buf1
+ 1, a
.buf1
, l0
+ 9);
235 if (memcmp (a
.buf1
, "aabcdefghi", 10))
238 if (mempcpy (a
.buf1
+ 5, "abcde", l0
+ 5) != a
.buf1
+ 10
239 || memcmp (a
.buf1
, "aabcdabcde", 10))
242 memset (a
.buf1
+ 8, 'j', l0
+ 2);
243 if (memcmp (a
.buf1
, "aabcdabcjj", 10))
246 #if __USE_FORTIFY_LEVEL < 2
247 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
248 and sufficient GCC support, as the string operations overflow
249 from a.buf1 into a.buf2. */
250 strcpy (a
.buf1
+ 4, str1
+ 5);
251 if (memcmp (a
.buf1
, "aabcEDCBA", 10))
254 if (stpcpy (a
.buf1
+ 8, str2
) != a
.buf1
+ 9
255 || memcmp (a
.buf1
, "aabcEDCBF", 10))
258 strncpy (a
.buf1
+ 6, "X", l0
+ 4);
259 if (memcmp (a
.buf1
, "aabcEDX\0\0", 10))
262 if (sprintf (a
.buf1
+ 7, "%d", num1
) != 2
263 || memcmp (a
.buf1
, "aabcEDX67", 10))
266 if (snprintf (a
.buf1
+ 7, 3, "%d", num2
) != 6
267 || memcmp (a
.buf1
, "aabcEDX98", 10))
270 a
.buf1
[l0
+ 8] = '\0';
271 strcat (a
.buf1
, "A");
272 if (memcmp (a
.buf1
, "aabcEDX9A", 10))
275 a
.buf1
[l0
+ 7] = '\0';
276 strncat (a
.buf1
, "ZYXWV", l0
+ 2);
277 if (memcmp (a
.buf1
, "aabcEDXZY", 10))
282 #if __USE_FORTIFY_LEVEL >= 1
283 /* Now check if all buffer overflows are caught at runtime. */
286 memcpy (buf
+ 1, "abcdefghij", l0
+ 10);
290 memmove (buf
+ 2, buf
+ 1, l0
+ 9);
294 p
= (char *) mempcpy (buf
+ 6, "abcde", l0
+ 5);
298 memset (buf
+ 9, 'j', l0
+ 2);
302 strcpy (buf
+ 5, str1
+ 5);
306 p
= stpcpy (buf
+ 9, str2
);
310 strncpy (buf
+ 7, "X", l0
+ 4);
314 stpncpy (buf
+ 6, "cd", l0
+ 5);
317 # if !defined __cplusplus || defined __va_arg_pack
319 sprintf (buf
+ 8, "%d", num1
);
323 snprintf (buf
+ 8, l0
+ 3, "%d", num2
);
327 swprintf (wbuf
+ 8, 3, L
"%d", num1
);
331 swprintf (wbuf
+ 8, l0
+ 3, L
"%d", num1
);
335 memcpy (buf
, str1
+ 2, l0
+ 9);
340 memcpy (buf
, str1
+ 3, l0
+ 8);
342 strncat (buf
, "ZYXWV", l0
+ 3);
346 memcpy (a
.buf1
+ 1, "abcdefghij", l0
+ 10);
350 memmove (a
.buf1
+ 2, a
.buf1
+ 1, l0
+ 9);
354 p
= (char *) mempcpy (a
.buf1
+ 6, "abcde", l0
+ 5);
358 memset (a
.buf1
+ 9, 'j', l0
+ 2);
361 # if __USE_FORTIFY_LEVEL >= 2
368 strcpy (a
.buf1
+ (O
+ 4), str1
+ 5);
372 p
= stpcpy (a
.buf1
+ (O
+ 8), str2
);
376 strncpy (a
.buf1
+ (O
+ 6), "X", l0
+ 4);
379 # if !defined __cplusplus || defined __va_arg_pack
381 sprintf (a
.buf1
+ (O
+ 7), "%d", num1
);
385 snprintf (a
.buf1
+ (O
+ 7), l0
+ 3, "%d", num2
);
389 memcpy (a
.buf1
, str1
+ (3 - O
), l0
+ 8 + O
);
391 strcat (a
.buf1
, "AB");
394 memcpy (a
.buf1
, str1
+ (4 - O
), l0
+ 7 + O
);
396 strncat (a
.buf1
, "ZYXWV", l0
+ 3);
401 /* These ops can be done without runtime checking of object size. */
402 wmemcpy (wbuf
, L
"abcdefghij", 10);
403 wmemmove (wbuf
+ 1, wbuf
, 9);
404 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
407 if (wmempcpy (wbuf
+ 5, L
"abcde", 5) != wbuf
+ 10
408 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
411 wmemset (wbuf
+ 8, L
'j', 2);
412 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
415 wcscpy (wbuf
+ 4, L
"EDCBA");
416 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
419 if (wcpcpy (wbuf
+ 8, L
"F") != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
422 wcsncpy (wbuf
+ 6, L
"X", 4);
423 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
426 if (swprintf (wbuf
+ 7, 3, L
"%ls", L
"987654") >= 0
427 || wmemcmp (wbuf
, L
"aabcEDX98", 10))
430 if (swprintf (wbuf
+ 7, 3, L
"64") != 2
431 || wmemcmp (wbuf
, L
"aabcEDX64", 10))
434 /* These ops need runtime checking, but shouldn't __chk_fail. */
435 wmemcpy (wbuf
, L
"abcdefghij", l0
+ 10);
436 wmemmove (wbuf
+ 1, wbuf
, l0
+ 9);
437 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
440 if (wmempcpy (wbuf
+ 5, L
"abcde", l0
+ 5) != wbuf
+ 10
441 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
444 wmemset (wbuf
+ 8, L
'j', l0
+ 2);
445 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
448 wcscpy (wbuf
+ 4, wstr1
+ 5);
449 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
452 if (wcpcpy (wbuf
+ 8, wstr2
) != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
455 wcsncpy (wbuf
+ 6, L
"X", l0
+ 4);
456 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
459 if (wcpncpy (wbuf
+ 5, L
"cd", l0
+ 5) != wbuf
+ 7
460 || wmemcmp (wbuf
, L
"aabcEcd\0\0", 10))
463 if (swprintf (wbuf
+ 7, 3, L
"%d", num2
) >= 0
464 || wmemcmp (wbuf
, L
"aabcEcd98", 10))
467 wbuf
[l0
+ 8] = L
'\0';
469 if (wmemcmp (wbuf
, L
"aabcEcd9A", 10))
472 wbuf
[l0
+ 7] = L
'\0';
473 wcsncat (wbuf
, L
"ZYXWV", l0
+ 2);
474 if (wmemcmp (wbuf
, L
"aabcEcdZY", 10))
477 wmemcpy (wa
.buf1
, L
"abcdefghij", l0
+ 10);
478 wmemmove (wa
.buf1
+ 1, wa
.buf1
, l0
+ 9);
479 if (wmemcmp (wa
.buf1
, L
"aabcdefghi", 10))
482 if (wmempcpy (wa
.buf1
+ 5, L
"abcde", l0
+ 5) != wa
.buf1
+ 10
483 || wmemcmp (wa
.buf1
, L
"aabcdabcde", 10))
486 wmemset (wa
.buf1
+ 8, L
'j', l0
+ 2);
487 if (wmemcmp (wa
.buf1
, L
"aabcdabcjj", 10))
490 #if __USE_FORTIFY_LEVEL < 2
491 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
492 and sufficient GCC support, as the string operations overflow
493 from a.buf1 into a.buf2. */
494 wcscpy (wa
.buf1
+ 4, wstr1
+ 5);
495 if (wmemcmp (wa
.buf1
, L
"aabcEDCBA", 10))
498 if (wcpcpy (wa
.buf1
+ 8, wstr2
) != wa
.buf1
+ 9
499 || wmemcmp (wa
.buf1
, L
"aabcEDCBF", 10))
502 wcsncpy (wa
.buf1
+ 6, L
"X", l0
+ 4);
503 if (wmemcmp (wa
.buf1
, L
"aabcEDX\0\0", 10))
506 if (swprintf (wa
.buf1
+ 7, 3, L
"%d", num2
) >= 0
507 || wmemcmp (wa
.buf1
, L
"aabcEDX98", 10))
510 wa
.buf1
[l0
+ 8] = L
'\0';
511 wcscat (wa
.buf1
, L
"A");
512 if (wmemcmp (wa
.buf1
, L
"aabcEDX9A", 10))
515 wa
.buf1
[l0
+ 7] = L
'\0';
516 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 2);
517 if (wmemcmp (wa
.buf1
, L
"aabcEDXZY", 10))
522 #if __USE_FORTIFY_LEVEL >= 1
523 /* Now check if all buffer overflows are caught at runtime. */
526 wmemcpy (wbuf
+ 1, L
"abcdefghij", l0
+ 10);
530 wmemcpy (wbuf
+ 9, L
"abcdefghij", l0
+ 10);
534 wmemmove (wbuf
+ 2, wbuf
+ 1, l0
+ 9);
538 wp
= wmempcpy (wbuf
+ 6, L
"abcde", l0
+ 5);
542 wmemset (wbuf
+ 9, L
'j', l0
+ 2);
546 wcscpy (wbuf
+ 5, wstr1
+ 5);
550 wp
= wcpcpy (wbuf
+ 9, wstr2
);
554 wcsncpy (wbuf
+ 7, L
"X", l0
+ 4);
558 wcsncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
562 wcpncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
566 wcpncpy (wbuf
+ 6, L
"cd", l0
+ 5);
569 wmemcpy (wbuf
, wstr1
+ 2, l0
+ 9);
571 wcscat (wbuf
, L
"AB");
574 wmemcpy (wbuf
, wstr1
+ 3, l0
+ 8);
576 wcsncat (wbuf
, L
"ZYXWV", l0
+ 3);
580 wmemcpy (wa
.buf1
+ 1, L
"abcdefghij", l0
+ 10);
584 wmemmove (wa
.buf1
+ 2, wa
.buf1
+ 1, l0
+ 9);
588 wp
= wmempcpy (wa
.buf1
+ 6, L
"abcde", l0
+ 5);
592 wmemset (wa
.buf1
+ 9, L
'j', l0
+ 2);
595 #if __USE_FORTIFY_LEVEL >= 2
602 wcscpy (wa
.buf1
+ (O
+ 4), wstr1
+ 5);
606 wp
= wcpcpy (wa
.buf1
+ (O
+ 8), wstr2
);
610 wcsncpy (wa
.buf1
+ (O
+ 6), L
"X", l0
+ 4);
613 wmemcpy (wa
.buf1
, wstr1
+ (3 - O
), l0
+ 8 + O
);
615 wcscat (wa
.buf1
, L
"AB");
618 wmemcpy (wa
.buf1
, wstr1
+ (4 - O
), l0
+ 7 + O
);
620 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 3);
625 /* Now checks for %n protection. */
627 /* Constant literals passed directly are always ok
628 (even with warnings about possible bugs from GCC). */
630 if (sprintf (buf
, "%s%n%s%n", str2
, &n1
, str2
, &n2
) != 2
631 || n1
!= 1 || n2
!= 2)
634 /* In this case the format string is not known at compile time,
635 but resides in read-only memory, so is ok. */
636 if (snprintf (buf
, 4, str3
, str2
, &n1
, str2
, &n2
) != 2
637 || n1
!= 1 || n2
!= 2)
640 strcpy (buf2
+ 2, "%n%s%n");
641 /* When the format string is writable and contains %n,
642 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
644 if (sprintf (buf
, buf2
, str2
, &n1
, str2
, &n1
) != 2)
649 if (snprintf (buf
, 3, buf2
, str2
, &n1
, str2
, &n1
) != 2)
653 /* But if there is no %n, even writable format string
656 if (sprintf (buf
, buf2
+ 4, str2
) != 1)
659 /* Constant literals passed directly are always ok
660 (even with warnings about possible bugs from GCC). */
661 if (printf ("%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
662 || n1
!= 7 || n2
!= 14)
665 /* In this case the format string is not known at compile time,
666 but resides in read-only memory, so is ok. */
667 if (printf (str3
, str4
, &n1
, str5
, &n2
) != 14
668 || n1
!= 7 || n2
!= 14)
671 strcpy (buf2
+ 2, "%n%s%n");
672 /* When the format string is writable and contains %n,
673 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
675 if (printf (buf2
, str4
, &n1
, str5
, &n1
) != 14)
679 /* But if there is no %n, even writable format string
682 if (printf (buf2
+ 4, str5
) != 7)
687 /* Constant literals passed directly are always ok
688 (even with warnings about possible bugs from GCC). */
689 if (fprintf (fp
, "%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
690 || n1
!= 7 || n2
!= 14)
693 /* In this case the format string is not known at compile time,
694 but resides in read-only memory, so is ok. */
695 if (fprintf (fp
, str3
, str4
, &n1
, str5
, &n2
) != 14
696 || n1
!= 7 || n2
!= 14)
699 strcpy (buf2
+ 2, "%n%s%n");
700 /* When the format string is writable and contains %n,
701 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
703 if (fprintf (fp
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
707 /* But if there is no %n, even writable format string
710 if (fprintf (fp
, buf2
+ 4, str5
) != 7)
714 strcpy (buf2
+ 2, "%n%s%n");
715 /* When the format string is writable and contains %n,
716 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
718 if (asprintf (&my_ptr
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
727 if (obstack_printf (&obs
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
730 obstack_free (&obs
, NULL
);
733 if (asprintf (&my_ptr
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
739 if (obstack_printf (&obs
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
741 obstack_free (&obs
, NULL
);
743 if (freopen (temp_filename
, "r", stdin
) == NULL
)
745 puts ("could not open temporary file");
749 if (gets (buf
) != buf
|| memcmp (buf
, "abcdefgh", 9))
751 if (gets (buf
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
754 #if __USE_FORTIFY_LEVEL >= 1
756 if (gets (buf
) != buf
)
763 if (fgets (buf
, sizeof (buf
), stdin
) != buf
764 || memcmp (buf
, "abcdefgh\n", 10))
766 if (fgets (buf
, sizeof (buf
), stdin
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
771 if (fgets (buf
, l0
+ sizeof (buf
), stdin
) != buf
772 || memcmp (buf
, "abcdefgh\n", 10))
775 #if __USE_FORTIFY_LEVEL >= 1
777 if (fgets (buf
, sizeof (buf
) + 1, stdin
) != buf
)
782 if (fgets (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
789 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
790 || memcmp (buf
, "abcdefgh\n", 10))
792 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
793 || memcmp (buf
, "ABCDEFGHI", 10))
798 if (fgets_unlocked (buf
, l0
+ sizeof (buf
), stdin
) != buf
799 || memcmp (buf
, "abcdefgh\n", 10))
802 #if __USE_FORTIFY_LEVEL >= 1
804 if (fgets_unlocked (buf
, sizeof (buf
) + 1, stdin
) != buf
)
809 if (fgets_unlocked (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
816 if (fread (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
817 || memcmp (buf
, "abcdefgh\nA", 10))
819 if (fread (buf
, sizeof (buf
), 1, stdin
) != 1
820 || memcmp (buf
, "BCDEFGHI\na", 10))
825 if (fread (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
826 || memcmp (buf
, "abcdefgh\nA", 10))
828 if (fread (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
829 || memcmp (buf
, "BCDEFGHI\na", 10))
832 #if __USE_FORTIFY_LEVEL >= 1
834 if (fread (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
839 if (fread (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
846 if (fread_unlocked (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
847 || memcmp (buf
, "abcdefgh\nA", 10))
849 if (fread_unlocked (buf
, sizeof (buf
), 1, stdin
) != 1
850 || memcmp (buf
, "BCDEFGHI\na", 10))
855 if (fread_unlocked (buf
, 1, 4, stdin
) != 4
856 || memcmp (buf
, "abcdFGHI\na", 10))
858 if (fread_unlocked (buf
, 4, 1, stdin
) != 1
859 || memcmp (buf
, "efghFGHI\na", 10))
864 if (fread_unlocked (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
865 || memcmp (buf
, "abcdefgh\nA", 10))
867 if (fread_unlocked (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
868 || memcmp (buf
, "BCDEFGHI\na", 10))
871 #if __USE_FORTIFY_LEVEL >= 1
873 if (fread_unlocked (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
878 if (fread_unlocked (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
883 lseek (fileno (stdin
), 0, SEEK_SET
);
885 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
886 || memcmp (buf
, "abcdefgh\n", 9))
888 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
889 || memcmp (buf
, "ABCDEFGHI", 9))
892 lseek (fileno (stdin
), 0, SEEK_SET
);
894 if (read (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1) != sizeof (buf
) - 1
895 || memcmp (buf
, "abcdefgh\n", 9))
898 #if __USE_FORTIFY_LEVEL >= 1
900 if (read (fileno (stdin
), buf
, sizeof (buf
) + 1) != sizeof (buf
) + 1)
905 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
907 || memcmp (buf
, "\nABCDEFGH", 9))
909 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
910 || memcmp (buf
, "abcdefgh\n", 9))
912 if (pread (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
914 || memcmp (buf
, "h\nABCDEFG", 9))
917 #if __USE_FORTIFY_LEVEL >= 1
919 if (pread (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
925 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
927 || memcmp (buf
, "\nABCDEFGH", 9))
929 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
930 || memcmp (buf
, "abcdefgh\n", 9))
932 if (pread64 (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
934 || memcmp (buf
, "h\nABCDEFG", 9))
937 #if __USE_FORTIFY_LEVEL >= 1
939 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
945 if (freopen (temp_filename
, "r", stdin
) == NULL
)
947 puts ("could not open temporary file");
951 if (fseek (stdin
, 9 + 10 + 11, SEEK_SET
))
953 puts ("could not seek in test file");
957 #if __USE_FORTIFY_LEVEL >= 1
959 if (gets (buf
) != buf
)
964 /* Check whether missing N$ formats are detected. */
966 printf ("%3$d\n", 1, 2, 3, 4);
970 fprintf (stdout
, "%3$d\n", 1, 2, 3, 4);
974 sprintf (buf
, "%3$d\n", 1, 2, 3, 4);
978 snprintf (buf
, sizeof (buf
), "%3$d\n", 1, 2, 3, 4);
982 if (socketpair (PF_UNIX
, SOCK_STREAM
, 0, sp
))
986 const char *sendstr
= "abcdefgh\nABCDEFGH\n0123456789\n";
987 if ((size_t) send (sp
[0], sendstr
, strlen (sendstr
), 0)
992 if (recv (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
)
994 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
997 if (recv (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
)
998 != sizeof recvbuf
- 7
999 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
1002 #if __USE_FORTIFY_LEVEL >= 1
1004 if (recv (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
)
1010 if (recv (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
)
1011 != sizeof recvbuf
- 3)
1017 struct sockaddr_un sa_un
;
1019 sl
= sizeof (sa_un
);
1020 if (recvfrom (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
,
1021 (struct sockaddr
*) &sa_un
, &sl
)
1023 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
1026 sl
= sizeof (sa_un
);
1027 if (recvfrom (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
,
1028 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 7
1029 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
1032 #if __USE_FORTIFY_LEVEL >= 1
1034 sl
= sizeof (sa_un
);
1035 if (recvfrom (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
,
1036 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
)
1041 sl
= sizeof (sa_un
);
1042 if (recvfrom (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
,
1043 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 3)
1052 char fname
[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1053 char *enddir
= strchr (fname
, '\0');
1054 if (mkdtemp (fname
) == NULL
)
1056 printf ("mkdtemp failed: %m\n");
1060 if (symlink ("bar", fname
) != 0)
1063 char readlinkbuf
[4];
1064 if (readlink (fname
, readlinkbuf
, 4) != 3
1065 || memcmp (readlinkbuf
, "bar", 3) != 0)
1067 if (readlink (fname
, readlinkbuf
+ 1, l0
+ 3) != 3
1068 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1071 #if __USE_FORTIFY_LEVEL >= 1
1073 if (readlink (fname
, readlinkbuf
+ 2, l0
+ 3) != 3)
1078 if (readlink (fname
, readlinkbuf
+ 3, 4) != 3)
1083 int tmpfd
= open ("/tmp", O_RDONLY
| O_DIRECTORY
);
1087 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
, 4) != 3
1088 || memcmp (readlinkbuf
, "bar", 3) != 0)
1090 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 1,
1092 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1095 #if __USE_FORTIFY_LEVEL >= 1
1097 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 2,
1103 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 3,
1111 char *cwd1
= getcwd (NULL
, 0);
1115 char *cwd2
= getcwd (NULL
, 250);
1121 if (strcmp (cwd1
, cwd2
) != 0)
1128 char *cwd3
= getcwd (NULL
, 0);
1131 if (strcmp (fname
, cwd3
) != 0)
1132 printf ("getcwd after chdir is '%s' != '%s',"
1133 "get{c,}wd tests skipped\n", cwd3
, fname
);
1136 char getcwdbuf
[sizeof fname
- 3];
1138 char *cwd4
= getcwd (getcwdbuf
, sizeof getcwdbuf
);
1139 if (cwd4
!= getcwdbuf
1140 || strcmp (getcwdbuf
, fname
) != 0)
1143 cwd4
= getcwd (getcwdbuf
+ 1, l0
+ sizeof getcwdbuf
- 1);
1144 if (cwd4
!= getcwdbuf
+ 1
1145 || getcwdbuf
[0] != fname
[0]
1146 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1149 #if __USE_FORTIFY_LEVEL >= 1
1151 if (getcwd (getcwdbuf
+ 2, l0
+ sizeof getcwdbuf
)
1157 if (getcwd (getcwdbuf
+ 2, sizeof getcwdbuf
)
1163 if (getwd (getcwdbuf
) != getcwdbuf
1164 || strcmp (getcwdbuf
, fname
) != 0)
1167 if (getwd (getcwdbuf
+ 1) != getcwdbuf
+ 1
1168 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1171 #if __USE_FORTIFY_LEVEL >= 1
1173 if (getwd (getcwdbuf
+ 2) != getcwdbuf
+ 2)
1179 if (chdir (cwd1
) != 0)
1187 if (unlink (fname
) != 0)
1191 if (rmdir (fname
) != 0)
1196 char largebuf
[PATH_MAX
];
1197 char *realres
= realpath (".", largebuf
);
1198 if (realres
!= largebuf
)
1201 # if __USE_FORTIFY_LEVEL >= 1
1204 realres
= realpath (".", realbuf
);
1205 if (realres
!= realbuf
)
1211 if (setlocale (LC_ALL
, "de_DE.UTF-8") != NULL
)
1213 assert (MB_CUR_MAX
<= 10);
1215 /* First a simple test. */
1217 if (wctomb (enough
, L
'A') != 1)
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 (wctomb (smallbuf
, L
'\x100') != 2)
1232 memset (&s
, '\0', sizeof (s
));
1233 if (wcrtomb (enough
, L
'D', &s
) != 1 || enough
[0] != 'D')
1236 #if __USE_FORTIFY_LEVEL >= 1
1237 /* We know the wchar_t encoding is ISO 10646. So pick a
1238 character which has a multibyte representation which does not
1242 if (wcrtomb (smallbuf
, L
'\x100', &s
) != 2)
1247 wchar_t wenough
[10];
1248 memset (&s
, '\0', sizeof (s
));
1249 const char *cp
= "A";
1250 if (mbsrtowcs (wenough
, &cp
, 10, &s
) != 1
1251 || wcscmp (wenough
, L
"A") != 0)
1255 if (mbsrtowcs (wenough
, &cp
, l0
+ 10, &s
) != 2
1256 || wcscmp (wenough
, L
"BC") != 0)
1259 #if __USE_FORTIFY_LEVEL >= 1
1261 wchar_t wsmallbuf
[2];
1263 mbsrtowcs (wsmallbuf
, &cp
, 10, &s
);
1268 if (mbstowcs (wenough
, cp
, 10) != 1
1269 || wcscmp (wenough
, L
"A") != 0)
1273 if (mbstowcs (wenough
, cp
, l0
+ 10) != 3
1274 || wcscmp (wenough
, L
"DEF") != 0)
1277 #if __USE_FORTIFY_LEVEL >= 1
1279 wchar_t wsmallbuf
[2];
1281 mbstowcs (wsmallbuf
, cp
, 10);
1285 memset (&s
, '\0', sizeof (s
));
1287 wcscpy (wenough
, L
"DEF");
1288 if (mbsnrtowcs (wenough
, &cp
, 1, 10, &s
) != 1
1289 || wcscmp (wenough
, L
"AEF") != 0)
1293 if (mbsnrtowcs (wenough
, &cp
, 1, l0
+ 10, &s
) != 1
1294 || wcscmp (wenough
, L
"IEF") != 0)
1297 #if __USE_FORTIFY_LEVEL >= 1
1299 wchar_t wsmallbuf
[2];
1301 mbsnrtowcs (wsmallbuf
, &cp
, 3, 10, &s
);
1305 memset (&s
, '\0', sizeof (s
));
1306 const wchar_t *wcp
= L
"A";
1307 if (wcsrtombs (enough
, &wcp
, 10, &s
) != 1
1308 || strcmp (enough
, "A") != 0)
1312 if (wcsrtombs (enough
, &wcp
, l0
+ 10, &s
) != 2
1313 || strcmp (enough
, "BC") != 0)
1316 #if __USE_FORTIFY_LEVEL >= 1
1320 wcsrtombs (smallbuf
, &wcp
, 10, &s
);
1324 memset (enough
, 'Z', sizeof (enough
));
1326 if (wcstombs (enough
, wcp
, 10) != 2
1327 || strcmp (enough
, "EF") != 0)
1331 if (wcstombs (enough
, wcp
, l0
+ 10) != 1
1332 || strcmp (enough
, "G") != 0)
1335 #if __USE_FORTIFY_LEVEL >= 1
1339 wcstombs (smallbuf
, wcp
, 10);
1343 memset (&s
, '\0', sizeof (s
));
1345 if (wcsnrtombs (enough
, &wcp
, 1, 10, &s
) != 1
1346 || strcmp (enough
, "A") != 0)
1350 if (wcsnrtombs (enough
, &wcp
, 1, l0
+ 10, &s
) != 1
1351 || strcmp (enough
, "B") != 0)
1354 #if __USE_FORTIFY_LEVEL >= 1
1358 wcsnrtombs (smallbuf
, &wcp
, 3, 10, &s
);
1364 puts ("cannot set locale");
1368 fd
= posix_openpt (O_RDWR
);
1372 if (ptsname_r (fd
, enough
, sizeof (enough
)) != 0)
1375 #if __USE_FORTIFY_LEVEL >= 1
1378 if (ptsname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1386 confstr (_CS_GNU_LIBC_VERSION
, largebuf
, sizeof (largebuf
));
1387 # if __USE_FORTIFY_LEVEL >= 1
1390 confstr (_CS_GNU_LIBC_VERSION
, smallbuf
, sizeof (largebuf
));
1396 int ngr
= getgroups (5, grpslarge
);
1397 asm volatile ("" : : "r" (ngr
));
1398 #if __USE_FORTIFY_LEVEL >= 1
1401 ngr
= getgroups (5, (gid_t
*) smallbuf
);
1402 asm volatile ("" : : "r" (ngr
));
1406 fd
= open (_PATH_TTY
, O_RDONLY
);
1410 if (ttyname_r (fd
, enough
, sizeof (enough
)) != 0)
1413 #if __USE_FORTIFY_LEVEL >= 1
1416 if (ttyname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1423 char hostnamelarge
[1000];
1424 gethostname (hostnamelarge
, sizeof (hostnamelarge
));
1425 #if __USE_FORTIFY_LEVEL >= 1
1428 gethostname (smallbuf
, sizeof (hostnamelarge
));
1432 char loginlarge
[1000];
1433 getlogin_r (loginlarge
, sizeof (hostnamelarge
));
1434 #if __USE_FORTIFY_LEVEL >= 1
1437 getlogin_r (smallbuf
, sizeof (loginlarge
));
1441 char domainnamelarge
[1000];
1442 int res
= getdomainname (domainnamelarge
, sizeof (domainnamelarge
));
1443 asm volatile ("" : : "r" (res
));
1444 #if __USE_FORTIFY_LEVEL >= 1
1447 res
= getdomainname (smallbuf
, sizeof (domainnamelarge
));
1448 asm volatile ("" : : "r" (res
));
1454 FD_SET (FD_SETSIZE
- 1, &s
);
1455 #if __USE_FORTIFY_LEVEL >= 1
1457 FD_SET (FD_SETSIZE
, &s
);
1460 FD_CLR (FD_SETSIZE
- 1, &s
);
1461 #if __USE_FORTIFY_LEVEL >= 1
1463 FD_CLR (FD_SETSIZE
, &s
);
1466 FD_ISSET (FD_SETSIZE
- 1, &s
);
1467 #if __USE_FORTIFY_LEVEL >= 1
1469 FD_ISSET (FD_SETSIZE
, &s
);