1 /* Copyright (C) 2004, 2005, 2006, 2007, 2008 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/socket.h>
35 #define obstack_chunk_alloc malloc
36 #define obstack_chunk_free free
39 static void do_prepare (void);
40 static int do_test (void);
41 #define PREPARE(argc, argv) do_prepare ()
42 #define TEST_FUNCTION do_test ()
43 #include "../test-skeleton.c"
48 int temp_fd
= create_temp_file ("tst-chk1.", &temp_filename
);
51 printf ("cannot create temporary file: %m\n");
55 const char *strs
= "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
56 if ((size_t) write (temp_fd
, strs
, strlen (strs
)) != strlen (strs
))
58 puts ("could not write test strings into file");
59 unlink (temp_filename
);
64 volatile int chk_fail_ok
;
74 longjmp (chk_fail_buf
, 1);
85 const char *str1
= "JIHGFEDCBA";
86 const char *str2
= "F";
87 const char *str3
= "%s%n%s%n";
88 const char *str4
= "Hello, ";
89 const char *str5
= "World!\n";
90 const wchar_t *wstr1
= L
"JIHGFEDCBA";
91 const wchar_t *wstr2
= L
"F";
92 const wchar_t *wstr3
= L
"%s%n%s%n";
93 const wchar_t *wstr4
= L
"Hello, ";
94 const wchar_t *wstr5
= L
"World!\n";
100 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
101 #define CHK_FAIL_START \
103 if (! setjmp (chk_fail_buf)) \
105 #define CHK_FAIL_END \
109 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
110 #define CHK_FAIL2_START CHK_FAIL_START
111 #define CHK_FAIL2_END CHK_FAIL_END
113 #define CHK_FAIL2_START
114 #define CHK_FAIL2_END
121 sa
.sa_handler
= handler
;
123 sigemptyset (&sa
.sa_mask
);
125 sigaction (SIGABRT
, &sa
, NULL
);
127 /* Avoid all the buffer overflow messages on stderr. */
128 int fd
= open (_PATH_DEVNULL
, O_WRONLY
);
130 close (STDERR_FILENO
);
133 dup2 (fd
, STDERR_FILENO
);
136 setenv ("LIBC_FATAL_STDERR_", "1", 1);
138 struct A
{ char buf1
[9]; char buf2
[1]; } a
;
139 struct wA
{ wchar_t buf1
[9]; wchar_t buf2
[1]; } wa
;
141 printf ("Test checking routines at fortify level %d\n",
142 #ifdef __USE_FORTIFY_LEVEL
143 (int) __USE_FORTIFY_LEVEL
149 #if defined __USE_FORTIFY_LEVEL && !defined __extern_always_inline
150 printf ("Test skipped");
155 /* These ops can be done without runtime checking of object size. */
156 memcpy (buf
, "abcdefghij", 10);
157 memmove (buf
+ 1, buf
, 9);
158 if (memcmp (buf
, "aabcdefghi", 10))
161 if (mempcpy (buf
+ 5, "abcde", 5) != buf
+ 10
162 || memcmp (buf
, "aabcdabcde", 10))
165 memset (buf
+ 8, 'j', 2);
166 if (memcmp (buf
, "aabcdabcjj", 10))
169 strcpy (buf
+ 4, "EDCBA");
170 if (memcmp (buf
, "aabcEDCBA", 10))
173 if (stpcpy (buf
+ 8, "F") != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
176 strncpy (buf
+ 6, "X", 4);
177 if (memcmp (buf
, "aabcEDX\0\0", 10))
180 if (sprintf (buf
+ 7, "%s", "67") != 2 || memcmp (buf
, "aabcEDX67", 10))
183 if (snprintf (buf
+ 7, 3, "%s", "987654") != 6
184 || memcmp (buf
, "aabcEDX98", 10))
187 /* These ops need runtime checking, but shouldn't __chk_fail. */
188 memcpy (buf
, "abcdefghij", l0
+ 10);
189 memmove (buf
+ 1, buf
, l0
+ 9);
190 if (memcmp (buf
, "aabcdefghi", 10))
193 if (mempcpy (buf
+ 5, "abcde", l0
+ 5) != buf
+ 10
194 || memcmp (buf
, "aabcdabcde", 10))
197 memset (buf
+ 8, 'j', l0
+ 2);
198 if (memcmp (buf
, "aabcdabcjj", 10))
201 strcpy (buf
+ 4, str1
+ 5);
202 if (memcmp (buf
, "aabcEDCBA", 10))
205 if (stpcpy (buf
+ 8, str2
) != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
208 strncpy (buf
+ 6, "X", l0
+ 4);
209 if (memcmp (buf
, "aabcEDX\0\0", 10))
212 if (stpncpy (buf
+ 5, "cd", l0
+ 5) != buf
+ 7
213 || memcmp (buf
, "aabcEcd\0\0", 10))
216 if (sprintf (buf
+ 7, "%d", num1
) != 2 || memcmp (buf
, "aabcEcd67", 10))
219 if (snprintf (buf
+ 7, 3, "%d", num2
) != 6 || memcmp (buf
, "aabcEcd98", 10))
224 if (memcmp (buf
, "aabcEcd9A", 10))
228 strncat (buf
, "ZYXWV", l0
+ 2);
229 if (memcmp (buf
, "aabcEcdZY", 10))
232 memcpy (a
.buf1
, "abcdefghij", l0
+ 10);
233 memmove (a
.buf1
+ 1, a
.buf1
, l0
+ 9);
234 if (memcmp (a
.buf1
, "aabcdefghi", 10))
237 if (mempcpy (a
.buf1
+ 5, "abcde", l0
+ 5) != a
.buf1
+ 10
238 || memcmp (a
.buf1
, "aabcdabcde", 10))
241 memset (a
.buf1
+ 8, 'j', l0
+ 2);
242 if (memcmp (a
.buf1
, "aabcdabcjj", 10))
245 #if __USE_FORTIFY_LEVEL < 2
246 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
247 and sufficient GCC support, as the string operations overflow
248 from a.buf1 into a.buf2. */
249 strcpy (a
.buf1
+ 4, str1
+ 5);
250 if (memcmp (a
.buf1
, "aabcEDCBA", 10))
253 if (stpcpy (a
.buf1
+ 8, str2
) != a
.buf1
+ 9
254 || memcmp (a
.buf1
, "aabcEDCBF", 10))
257 strncpy (a
.buf1
+ 6, "X", l0
+ 4);
258 if (memcmp (a
.buf1
, "aabcEDX\0\0", 10))
261 if (sprintf (a
.buf1
+ 7, "%d", num1
) != 2
262 || memcmp (a
.buf1
, "aabcEDX67", 10))
265 if (snprintf (a
.buf1
+ 7, 3, "%d", num2
) != 6
266 || memcmp (a
.buf1
, "aabcEDX98", 10))
269 a
.buf1
[l0
+ 8] = '\0';
270 strcat (a
.buf1
, "A");
271 if (memcmp (a
.buf1
, "aabcEDX9A", 10))
274 a
.buf1
[l0
+ 7] = '\0';
275 strncat (a
.buf1
, "ZYXWV", l0
+ 2);
276 if (memcmp (a
.buf1
, "aabcEDXZY", 10))
281 #if __USE_FORTIFY_LEVEL >= 1
282 /* Now check if all buffer overflows are caught at runtime. */
285 memcpy (buf
+ 1, "abcdefghij", l0
+ 10);
289 memmove (buf
+ 2, buf
+ 1, l0
+ 9);
293 p
= (char *) mempcpy (buf
+ 6, "abcde", l0
+ 5);
297 memset (buf
+ 9, 'j', l0
+ 2);
301 strcpy (buf
+ 5, str1
+ 5);
305 p
= stpcpy (buf
+ 9, str2
);
309 strncpy (buf
+ 7, "X", l0
+ 4);
313 stpncpy (buf
+ 6, "cd", l0
+ 5);
316 # if !defined __cplusplus || defined __va_arg_pack
318 sprintf (buf
+ 8, "%d", num1
);
322 snprintf (buf
+ 8, l0
+ 3, "%d", num2
);
326 swprintf (wbuf
+ 8, 3, L
"%d", num1
);
330 swprintf (wbuf
+ 8, l0
+ 3, L
"%d", num1
);
334 memcpy (buf
, str1
+ 2, l0
+ 9);
339 memcpy (buf
, str1
+ 3, l0
+ 8);
341 strncat (buf
, "ZYXWV", l0
+ 3);
345 memcpy (a
.buf1
+ 1, "abcdefghij", l0
+ 10);
349 memmove (a
.buf1
+ 2, a
.buf1
+ 1, l0
+ 9);
353 p
= (char *) mempcpy (a
.buf1
+ 6, "abcde", l0
+ 5);
357 memset (a
.buf1
+ 9, 'j', l0
+ 2);
360 # if __USE_FORTIFY_LEVEL >= 2
367 strcpy (a
.buf1
+ (O
+ 4), str1
+ 5);
371 p
= stpcpy (a
.buf1
+ (O
+ 8), str2
);
375 strncpy (a
.buf1
+ (O
+ 6), "X", l0
+ 4);
378 # if !defined __cplusplus || defined __va_arg_pack
380 sprintf (a
.buf1
+ (O
+ 7), "%d", num1
);
384 snprintf (a
.buf1
+ (O
+ 7), l0
+ 3, "%d", num2
);
388 memcpy (a
.buf1
, str1
+ (3 - O
), l0
+ 8 + O
);
390 strcat (a
.buf1
, "AB");
393 memcpy (a
.buf1
, str1
+ (4 - O
), l0
+ 7 + O
);
395 strncat (a
.buf1
, "ZYXWV", l0
+ 3);
400 /* These ops can be done without runtime checking of object size. */
401 wmemcpy (wbuf
, L
"abcdefghij", 10);
402 wmemmove (wbuf
+ 1, wbuf
, 9);
403 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
406 if (wmempcpy (wbuf
+ 5, L
"abcde", 5) != wbuf
+ 10
407 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
410 wmemset (wbuf
+ 8, L
'j', 2);
411 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
414 wcscpy (wbuf
+ 4, L
"EDCBA");
415 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
418 if (wcpcpy (wbuf
+ 8, L
"F") != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
421 wcsncpy (wbuf
+ 6, L
"X", 4);
422 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
425 if (swprintf (wbuf
+ 7, 3, L
"%ls", L
"987654") >= 0
426 || wmemcmp (wbuf
, L
"aabcEDX98", 10))
429 if (swprintf (wbuf
+ 7, 3, L
"64") != 2
430 || wmemcmp (wbuf
, L
"aabcEDX64", 10))
433 /* These ops need runtime checking, but shouldn't __chk_fail. */
434 wmemcpy (wbuf
, L
"abcdefghij", l0
+ 10);
435 wmemmove (wbuf
+ 1, wbuf
, l0
+ 9);
436 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
439 if (wmempcpy (wbuf
+ 5, L
"abcde", l0
+ 5) != wbuf
+ 10
440 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
443 wmemset (wbuf
+ 8, L
'j', l0
+ 2);
444 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
447 wcscpy (wbuf
+ 4, wstr1
+ 5);
448 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
451 if (wcpcpy (wbuf
+ 8, wstr2
) != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
454 wcsncpy (wbuf
+ 6, L
"X", l0
+ 4);
455 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
458 if (wcpncpy (wbuf
+ 5, L
"cd", l0
+ 5) != wbuf
+ 7
459 || wmemcmp (wbuf
, L
"aabcEcd\0\0", 10))
462 if (swprintf (wbuf
+ 7, 3, L
"%d", num2
) >= 0
463 || wmemcmp (wbuf
, L
"aabcEcd98", 10))
466 wbuf
[l0
+ 8] = L
'\0';
468 if (wmemcmp (wbuf
, L
"aabcEcd9A", 10))
471 wbuf
[l0
+ 7] = L
'\0';
472 wcsncat (wbuf
, L
"ZYXWV", l0
+ 2);
473 if (wmemcmp (wbuf
, L
"aabcEcdZY", 10))
476 wmemcpy (wa
.buf1
, L
"abcdefghij", l0
+ 10);
477 wmemmove (wa
.buf1
+ 1, wa
.buf1
, l0
+ 9);
478 if (wmemcmp (wa
.buf1
, L
"aabcdefghi", 10))
481 if (wmempcpy (wa
.buf1
+ 5, L
"abcde", l0
+ 5) != wa
.buf1
+ 10
482 || wmemcmp (wa
.buf1
, L
"aabcdabcde", 10))
485 wmemset (wa
.buf1
+ 8, L
'j', l0
+ 2);
486 if (wmemcmp (wa
.buf1
, L
"aabcdabcjj", 10))
489 #if __USE_FORTIFY_LEVEL < 2
490 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
491 and sufficient GCC support, as the string operations overflow
492 from a.buf1 into a.buf2. */
493 wcscpy (wa
.buf1
+ 4, wstr1
+ 5);
494 if (wmemcmp (wa
.buf1
, L
"aabcEDCBA", 10))
497 if (wcpcpy (wa
.buf1
+ 8, wstr2
) != wa
.buf1
+ 9
498 || wmemcmp (wa
.buf1
, L
"aabcEDCBF", 10))
501 wcsncpy (wa
.buf1
+ 6, L
"X", l0
+ 4);
502 if (wmemcmp (wa
.buf1
, L
"aabcEDX\0\0", 10))
505 if (swprintf (wa
.buf1
+ 7, 3, L
"%d", num2
) >= 0
506 || wmemcmp (wa
.buf1
, L
"aabcEDX98", 10))
509 wa
.buf1
[l0
+ 8] = L
'\0';
510 wcscat (wa
.buf1
, L
"A");
511 if (wmemcmp (wa
.buf1
, L
"aabcEDX9A", 10))
514 wa
.buf1
[l0
+ 7] = L
'\0';
515 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 2);
516 if (wmemcmp (wa
.buf1
, L
"aabcEDXZY", 10))
521 #if __USE_FORTIFY_LEVEL >= 1
522 /* Now check if all buffer overflows are caught at runtime. */
525 wmemcpy (wbuf
+ 1, L
"abcdefghij", l0
+ 10);
529 wmemcpy (wbuf
+ 9, L
"abcdefghij", l0
+ 10);
533 wmemmove (wbuf
+ 2, wbuf
+ 1, l0
+ 9);
537 wp
= wmempcpy (wbuf
+ 6, L
"abcde", l0
+ 5);
541 wmemset (wbuf
+ 9, L
'j', l0
+ 2);
545 wcscpy (wbuf
+ 5, wstr1
+ 5);
549 wp
= wcpcpy (wbuf
+ 9, wstr2
);
553 wcsncpy (wbuf
+ 7, L
"X", l0
+ 4);
557 wcsncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
561 wcpncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
565 wcpncpy (wbuf
+ 6, L
"cd", l0
+ 5);
568 wmemcpy (wbuf
, wstr1
+ 2, l0
+ 9);
570 wcscat (wbuf
, L
"AB");
573 wmemcpy (wbuf
, wstr1
+ 3, l0
+ 8);
575 wcsncat (wbuf
, L
"ZYXWV", l0
+ 3);
579 wmemcpy (wa
.buf1
+ 1, L
"abcdefghij", l0
+ 10);
583 wmemmove (wa
.buf1
+ 2, wa
.buf1
+ 1, l0
+ 9);
587 wp
= wmempcpy (wa
.buf1
+ 6, L
"abcde", l0
+ 5);
591 wmemset (wa
.buf1
+ 9, L
'j', l0
+ 2);
594 #if __USE_FORTIFY_LEVEL >= 2
601 wcscpy (wa
.buf1
+ (O
+ 4), wstr1
+ 5);
605 wp
= wcpcpy (wa
.buf1
+ (O
+ 8), wstr2
);
609 wcsncpy (wa
.buf1
+ (O
+ 6), L
"X", l0
+ 4);
612 wmemcpy (wa
.buf1
, wstr1
+ (3 - O
), l0
+ 8 + O
);
614 wcscat (wa
.buf1
, L
"AB");
617 wmemcpy (wa
.buf1
, wstr1
+ (4 - O
), l0
+ 7 + O
);
619 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 3);
624 /* Now checks for %n protection. */
626 /* Constant literals passed directly are always ok
627 (even with warnings about possible bugs from GCC). */
629 if (sprintf (buf
, "%s%n%s%n", str2
, &n1
, str2
, &n2
) != 2
630 || n1
!= 1 || n2
!= 2)
633 /* In this case the format string is not known at compile time,
634 but resides in read-only memory, so is ok. */
635 if (snprintf (buf
, 4, str3
, str2
, &n1
, str2
, &n2
) != 2
636 || n1
!= 1 || n2
!= 2)
639 strcpy (buf2
+ 2, "%n%s%n");
640 /* When the format string is writable and contains %n,
641 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
643 if (sprintf (buf
, buf2
, str2
, &n1
, str2
, &n1
) != 2)
648 if (snprintf (buf
, 3, buf2
, str2
, &n1
, str2
, &n1
) != 2)
652 /* But if there is no %n, even writable format string
655 if (sprintf (buf
, buf2
+ 4, str2
) != 1)
658 /* Constant literals passed directly are always ok
659 (even with warnings about possible bugs from GCC). */
660 if (printf ("%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
661 || n1
!= 7 || n2
!= 14)
664 /* In this case the format string is not known at compile time,
665 but resides in read-only memory, so is ok. */
666 if (printf (str3
, str4
, &n1
, str5
, &n2
) != 14
667 || n1
!= 7 || n2
!= 14)
670 strcpy (buf2
+ 2, "%n%s%n");
671 /* When the format string is writable and contains %n,
672 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
674 if (printf (buf2
, str4
, &n1
, str5
, &n1
) != 14)
678 /* But if there is no %n, even writable format string
681 if (printf (buf2
+ 4, str5
) != 7)
686 /* Constant literals passed directly are always ok
687 (even with warnings about possible bugs from GCC). */
688 if (fprintf (fp
, "%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
689 || n1
!= 7 || n2
!= 14)
692 /* In this case the format string is not known at compile time,
693 but resides in read-only memory, so is ok. */
694 if (fprintf (fp
, str3
, str4
, &n1
, str5
, &n2
) != 14
695 || n1
!= 7 || n2
!= 14)
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 (fprintf (fp
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
706 /* But if there is no %n, even writable format string
709 if (fprintf (fp
, buf2
+ 4, str5
) != 7)
713 strcpy (buf2
+ 2, "%n%s%n");
714 /* When the format string is writable and contains %n,
715 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
717 if (asprintf (&my_ptr
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
726 if (obstack_printf (&obs
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
729 obstack_free (&obs
, NULL
);
732 if (asprintf (&my_ptr
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
738 if (obstack_printf (&obs
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
740 obstack_free (&obs
, NULL
);
742 if (freopen (temp_filename
, "r", stdin
) == NULL
)
744 puts ("could not open temporary file");
748 if (gets (buf
) != buf
|| memcmp (buf
, "abcdefgh", 9))
750 if (gets (buf
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
753 #if __USE_FORTIFY_LEVEL >= 1
755 if (gets (buf
) != buf
)
762 if (fgets (buf
, sizeof (buf
), stdin
) != buf
763 || memcmp (buf
, "abcdefgh\n", 10))
765 if (fgets (buf
, sizeof (buf
), stdin
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
770 if (fgets (buf
, l0
+ sizeof (buf
), stdin
) != buf
771 || memcmp (buf
, "abcdefgh\n", 10))
774 #if __USE_FORTIFY_LEVEL >= 1
776 if (fgets (buf
, sizeof (buf
) + 1, stdin
) != buf
)
781 if (fgets (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
788 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
789 || memcmp (buf
, "abcdefgh\n", 10))
791 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
792 || memcmp (buf
, "ABCDEFGHI", 10))
797 if (fgets_unlocked (buf
, l0
+ sizeof (buf
), stdin
) != buf
798 || memcmp (buf
, "abcdefgh\n", 10))
801 #if __USE_FORTIFY_LEVEL >= 1
803 if (fgets_unlocked (buf
, sizeof (buf
) + 1, stdin
) != buf
)
808 if (fgets_unlocked (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
815 if (fread (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
816 || memcmp (buf
, "abcdefgh\nA", 10))
818 if (fread (buf
, sizeof (buf
), 1, stdin
) != 1
819 || memcmp (buf
, "BCDEFGHI\na", 10))
824 if (fread (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
825 || memcmp (buf
, "abcdefgh\nA", 10))
827 if (fread (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
828 || memcmp (buf
, "BCDEFGHI\na", 10))
831 #if __USE_FORTIFY_LEVEL >= 1
833 if (fread (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
838 if (fread (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
845 if (fread_unlocked (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
846 || memcmp (buf
, "abcdefgh\nA", 10))
848 if (fread_unlocked (buf
, sizeof (buf
), 1, stdin
) != 1
849 || memcmp (buf
, "BCDEFGHI\na", 10))
854 if (fread_unlocked (buf
, 1, 4, stdin
) != 4
855 || memcmp (buf
, "abcdFGHI\na", 10))
857 if (fread_unlocked (buf
, 4, 1, stdin
) != 1
858 || memcmp (buf
, "efghFGHI\na", 10))
863 if (fread_unlocked (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
864 || memcmp (buf
, "abcdefgh\nA", 10))
866 if (fread_unlocked (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
867 || memcmp (buf
, "BCDEFGHI\na", 10))
870 #if __USE_FORTIFY_LEVEL >= 1
872 if (fread_unlocked (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
877 if (fread_unlocked (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
882 lseek (fileno (stdin
), 0, SEEK_SET
);
884 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
885 || memcmp (buf
, "abcdefgh\n", 9))
887 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
888 || memcmp (buf
, "ABCDEFGHI", 9))
891 lseek (fileno (stdin
), 0, SEEK_SET
);
893 if (read (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1) != sizeof (buf
) - 1
894 || memcmp (buf
, "abcdefgh\n", 9))
897 #if __USE_FORTIFY_LEVEL >= 1
899 if (read (fileno (stdin
), buf
, sizeof (buf
) + 1) != sizeof (buf
) + 1)
904 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
906 || memcmp (buf
, "\nABCDEFGH", 9))
908 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
909 || memcmp (buf
, "abcdefgh\n", 9))
911 if (pread (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
913 || memcmp (buf
, "h\nABCDEFG", 9))
916 #if __USE_FORTIFY_LEVEL >= 1
918 if (pread (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
924 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
926 || memcmp (buf
, "\nABCDEFGH", 9))
928 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
929 || memcmp (buf
, "abcdefgh\n", 9))
931 if (pread64 (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
933 || memcmp (buf
, "h\nABCDEFG", 9))
936 #if __USE_FORTIFY_LEVEL >= 1
938 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
944 if (freopen (temp_filename
, "r", stdin
) == NULL
)
946 puts ("could not open temporary file");
950 if (fseek (stdin
, 9 + 10 + 11, SEEK_SET
))
952 puts ("could not seek in test file");
956 #if __USE_FORTIFY_LEVEL >= 1
958 if (gets (buf
) != buf
)
963 /* Check whether missing N$ formats are detected. */
965 printf ("%3$d\n", 1, 2, 3, 4);
969 fprintf (stdout
, "%3$d\n", 1, 2, 3, 4);
973 sprintf (buf
, "%3$d\n", 1, 2, 3, 4);
977 snprintf (buf
, sizeof (buf
), "%3$d\n", 1, 2, 3, 4);
981 if (socketpair (PF_UNIX
, SOCK_STREAM
, 0, sp
))
985 const char *sendstr
= "abcdefgh\nABCDEFGH\n0123456789\n";
986 if ((size_t) send (sp
[0], sendstr
, strlen (sendstr
), 0)
991 if (recv (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
)
993 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
996 if (recv (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
)
997 != sizeof recvbuf
- 7
998 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
1001 #if __USE_FORTIFY_LEVEL >= 1
1003 if (recv (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
)
1009 if (recv (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
)
1010 != sizeof recvbuf
- 3)
1016 struct sockaddr_un sa_un
;
1018 sl
= sizeof (sa_un
);
1019 if (recvfrom (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
,
1020 (struct sockaddr
*) &sa_un
, &sl
)
1022 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
1025 sl
= sizeof (sa_un
);
1026 if (recvfrom (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
,
1027 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 7
1028 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
1031 #if __USE_FORTIFY_LEVEL >= 1
1033 sl
= sizeof (sa_un
);
1034 if (recvfrom (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
,
1035 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
)
1040 sl
= sizeof (sa_un
);
1041 if (recvfrom (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
,
1042 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 3)
1051 char fname
[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1052 char *enddir
= strchr (fname
, '\0');
1053 if (mkdtemp (fname
) == NULL
)
1055 printf ("mkdtemp failed: %m\n");
1059 if (symlink ("bar", fname
) != 0)
1062 char readlinkbuf
[4];
1063 if (readlink (fname
, readlinkbuf
, 4) != 3
1064 || memcmp (readlinkbuf
, "bar", 3) != 0)
1066 if (readlink (fname
, readlinkbuf
+ 1, l0
+ 3) != 3
1067 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1070 #if __USE_FORTIFY_LEVEL >= 1
1072 if (readlink (fname
, readlinkbuf
+ 2, l0
+ 3) != 3)
1077 if (readlink (fname
, readlinkbuf
+ 3, 4) != 3)
1082 int tmpfd
= open ("/tmp", O_RDONLY
| O_DIRECTORY
);
1086 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
, 4) != 3
1087 || memcmp (readlinkbuf
, "bar", 3) != 0)
1089 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 1,
1091 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1094 #if __USE_FORTIFY_LEVEL >= 1
1096 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 2,
1102 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 3,
1110 char *cwd1
= getcwd (NULL
, 0);
1114 char *cwd2
= getcwd (NULL
, 250);
1120 if (strcmp (cwd1
, cwd2
) != 0)
1127 char *cwd3
= getcwd (NULL
, 0);
1130 if (strcmp (fname
, cwd3
) != 0)
1131 printf ("getcwd after chdir is '%s' != '%s',"
1132 "get{c,}wd tests skipped\n", cwd3
, fname
);
1135 char getcwdbuf
[sizeof fname
- 3];
1137 char *cwd4
= getcwd (getcwdbuf
, sizeof getcwdbuf
);
1138 if (cwd4
!= getcwdbuf
1139 || strcmp (getcwdbuf
, fname
) != 0)
1142 cwd4
= getcwd (getcwdbuf
+ 1, l0
+ sizeof getcwdbuf
- 1);
1143 if (cwd4
!= getcwdbuf
+ 1
1144 || getcwdbuf
[0] != fname
[0]
1145 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1148 #if __USE_FORTIFY_LEVEL >= 1
1150 if (getcwd (getcwdbuf
+ 2, l0
+ sizeof getcwdbuf
)
1156 if (getcwd (getcwdbuf
+ 2, sizeof getcwdbuf
)
1162 if (getwd (getcwdbuf
) != getcwdbuf
1163 || strcmp (getcwdbuf
, fname
) != 0)
1166 if (getwd (getcwdbuf
+ 1) != getcwdbuf
+ 1
1167 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1170 #if __USE_FORTIFY_LEVEL >= 1
1172 if (getwd (getcwdbuf
+ 2) != getcwdbuf
+ 2)
1178 if (chdir (cwd1
) != 0)
1186 if (unlink (fname
) != 0)
1190 if (rmdir (fname
) != 0)
1195 char largebuf
[PATH_MAX
];
1196 char *realres
= realpath (".", largebuf
);
1197 if (realres
!= largebuf
)
1200 # if __USE_FORTIFY_LEVEL >= 1
1203 realres
= realpath (".", realbuf
);
1204 if (realres
!= realbuf
)
1210 if (setlocale (LC_ALL
, "de_DE.UTF-8") != NULL
)
1212 assert (MB_CUR_MAX
<= 10);
1214 /* First a simple test. */
1216 if (wctomb (enough
, L
'A') != 1)
1219 #if __USE_FORTIFY_LEVEL >= 1
1220 /* We know the wchar_t encoding is ISO 10646. So pick a
1221 character which has a multibyte representation which does not
1225 if (wctomb (smallbuf
, L
'\x100') != 2)
1231 memset (&s
, '\0', sizeof (s
));
1232 if (wcrtomb (enough
, L
'D', &s
) != 1 || enough
[0] != 'D')
1235 #if __USE_FORTIFY_LEVEL >= 1
1236 /* We know the wchar_t encoding is ISO 10646. So pick a
1237 character which has a multibyte representation which does not
1241 if (wcrtomb (smallbuf
, L
'\x100', &s
) != 2)
1246 wchar_t wenough
[10];
1247 memset (&s
, '\0', sizeof (s
));
1248 const char *cp
= "A";
1249 if (mbsrtowcs (wenough
, &cp
, 10, &s
) != 1
1250 || wcscmp (wenough
, L
"A") != 0)
1254 if (mbsrtowcs (wenough
, &cp
, l0
+ 10, &s
) != 2
1255 || wcscmp (wenough
, L
"BC") != 0)
1258 #if __USE_FORTIFY_LEVEL >= 1
1260 wchar_t wsmallbuf
[2];
1262 mbsrtowcs (wsmallbuf
, &cp
, 10, &s
);
1267 if (mbstowcs (wenough
, cp
, 10) != 1
1268 || wcscmp (wenough
, L
"A") != 0)
1272 if (mbstowcs (wenough
, cp
, l0
+ 10) != 3
1273 || wcscmp (wenough
, L
"DEF") != 0)
1276 #if __USE_FORTIFY_LEVEL >= 1
1278 wchar_t wsmallbuf
[2];
1280 mbstowcs (wsmallbuf
, cp
, 10);
1284 memset (&s
, '\0', sizeof (s
));
1286 wcscpy (wenough
, L
"DEF");
1287 if (mbsnrtowcs (wenough
, &cp
, 1, 10, &s
) != 1
1288 || wcscmp (wenough
, L
"AEF") != 0)
1292 if (mbsnrtowcs (wenough
, &cp
, 1, l0
+ 10, &s
) != 1
1293 || wcscmp (wenough
, L
"IEF") != 0)
1296 #if __USE_FORTIFY_LEVEL >= 1
1298 wchar_t wsmallbuf
[2];
1300 mbsnrtowcs (wsmallbuf
, &cp
, 3, 10, &s
);
1304 memset (&s
, '\0', sizeof (s
));
1305 const wchar_t *wcp
= L
"A";
1306 if (wcsrtombs (enough
, &wcp
, 10, &s
) != 1
1307 || strcmp (enough
, "A") != 0)
1311 if (wcsrtombs (enough
, &wcp
, l0
+ 10, &s
) != 2
1312 || strcmp (enough
, "BC") != 0)
1315 #if __USE_FORTIFY_LEVEL >= 1
1319 wcsrtombs (smallbuf
, &wcp
, 10, &s
);
1323 memset (enough
, 'Z', sizeof (enough
));
1325 if (wcstombs (enough
, wcp
, 10) != 2
1326 || strcmp (enough
, "EF") != 0)
1330 if (wcstombs (enough
, wcp
, l0
+ 10) != 1
1331 || strcmp (enough
, "G") != 0)
1334 #if __USE_FORTIFY_LEVEL >= 1
1338 wcstombs (smallbuf
, wcp
, 10);
1342 memset (&s
, '\0', sizeof (s
));
1344 if (wcsnrtombs (enough
, &wcp
, 1, 10, &s
) != 1
1345 || strcmp (enough
, "A") != 0)
1349 if (wcsnrtombs (enough
, &wcp
, 1, l0
+ 10, &s
) != 1
1350 || strcmp (enough
, "B") != 0)
1353 #if __USE_FORTIFY_LEVEL >= 1
1357 wcsnrtombs (smallbuf
, &wcp
, 3, 10, &s
);
1363 puts ("cannot set locale");
1367 fd
= posix_openpt (O_RDWR
);
1371 if (ptsname_r (fd
, enough
, sizeof (enough
)) != 0)
1374 #if __USE_FORTIFY_LEVEL >= 1
1377 if (ptsname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1385 confstr (_CS_GNU_LIBC_VERSION
, largebuf
, sizeof (largebuf
));
1386 # if __USE_FORTIFY_LEVEL >= 1
1389 confstr (_CS_GNU_LIBC_VERSION
, smallbuf
, sizeof (largebuf
));
1395 int ngr
= getgroups (5, grpslarge
);
1396 asm volatile ("" : : "r" (ngr
));
1397 #if __USE_FORTIFY_LEVEL >= 1
1400 ngr
= getgroups (5, (gid_t
*) smallbuf
);
1401 asm volatile ("" : : "r" (ngr
));
1405 fd
= open (_PATH_TTY
, O_RDONLY
);
1409 if (ttyname_r (fd
, enough
, sizeof (enough
)) != 0)
1412 #if __USE_FORTIFY_LEVEL >= 1
1415 if (ttyname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1422 char hostnamelarge
[1000];
1423 gethostname (hostnamelarge
, sizeof (hostnamelarge
));
1424 #if __USE_FORTIFY_LEVEL >= 1
1427 gethostname (smallbuf
, sizeof (hostnamelarge
));
1431 char loginlarge
[1000];
1432 getlogin_r (loginlarge
, sizeof (hostnamelarge
));
1433 #if __USE_FORTIFY_LEVEL >= 1
1436 getlogin_r (smallbuf
, sizeof (loginlarge
));
1440 char domainnamelarge
[1000];
1441 int res
= getdomainname (domainnamelarge
, sizeof (domainnamelarge
));
1442 asm volatile ("" : : "r" (res
));
1443 #if __USE_FORTIFY_LEVEL >= 1
1446 res
= getdomainname (smallbuf
, sizeof (domainnamelarge
));
1447 asm volatile ("" : : "r" (res
));