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
20 /* Hack: make sure GCC doesn't know __chk_fail () will not return. */
35 #include <sys/socket.h>
38 #define obstack_chunk_alloc malloc
39 #define obstack_chunk_free free
42 static void do_prepare (void);
43 static int do_test (void);
44 #define PREPARE(argc, argv) do_prepare ()
45 #define TEST_FUNCTION do_test ()
46 #include "../test-skeleton.c"
51 int temp_fd
= create_temp_file ("tst-chk1.", &temp_filename
);
54 printf ("cannot create temporary file: %m\n");
58 const char *strs
= "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
59 if ((size_t) write (temp_fd
, strs
, strlen (strs
)) != strlen (strs
))
61 puts ("could not write test strings into file");
62 unlink (temp_filename
);
67 volatile int chk_fail_ok
;
77 longjmp (chk_fail_buf
, 1);
88 const char *str1
= "JIHGFEDCBA";
89 const char *str2
= "F";
90 const char *str3
= "%s%n%s%n";
91 const char *str4
= "Hello, ";
92 const char *str5
= "World!\n";
93 const wchar_t *wstr1
= L
"JIHGFEDCBA";
94 const wchar_t *wstr2
= L
"F";
95 const wchar_t *wstr3
= L
"%s%n%s%n";
96 const wchar_t *wstr4
= L
"Hello, ";
97 const wchar_t *wstr5
= L
"World!\n";
103 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
104 #define CHK_FAIL_START \
106 if (! setjmp (chk_fail_buf)) \
108 #define CHK_FAIL_END \
112 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
113 #define CHK_FAIL2_START CHK_FAIL_START
114 #define CHK_FAIL2_END CHK_FAIL_END
116 #define CHK_FAIL2_START
117 #define CHK_FAIL2_END
124 sa
.sa_handler
= handler
;
126 sigemptyset (&sa
.sa_mask
);
128 sigaction (SIGABRT
, &sa
, NULL
);
130 /* Avoid all the buffer overflow messages on stderr. */
131 int fd
= open (_PATH_DEVNULL
, O_WRONLY
);
133 close (STDERR_FILENO
);
136 dup2 (fd
, STDERR_FILENO
);
139 setenv ("LIBC_FATAL_STDERR_", "1", 1);
141 struct A
{ char buf1
[9]; char buf2
[1]; } a
;
142 struct wA
{ wchar_t buf1
[9]; wchar_t buf2
[1]; } wa
;
144 printf ("Test checking routines at fortify level %d\n",
145 #ifdef __USE_FORTIFY_LEVEL
146 (int) __USE_FORTIFY_LEVEL
152 #if defined __USE_FORTIFY_LEVEL && !defined __extern_always_inline
153 printf ("Test skipped");
158 /* These ops can be done without runtime checking of object size. */
159 memcpy (buf
, "abcdefghij", 10);
160 memmove (buf
+ 1, buf
, 9);
161 if (memcmp (buf
, "aabcdefghi", 10))
164 if (mempcpy (buf
+ 5, "abcde", 5) != buf
+ 10
165 || memcmp (buf
, "aabcdabcde", 10))
168 memset (buf
+ 8, 'j', 2);
169 if (memcmp (buf
, "aabcdabcjj", 10))
172 strcpy (buf
+ 4, "EDCBA");
173 if (memcmp (buf
, "aabcEDCBA", 10))
176 if (stpcpy (buf
+ 8, "F") != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
179 strncpy (buf
+ 6, "X", 4);
180 if (memcmp (buf
, "aabcEDX\0\0", 10))
183 if (sprintf (buf
+ 7, "%s", "67") != 2 || memcmp (buf
, "aabcEDX67", 10))
186 if (snprintf (buf
+ 7, 3, "%s", "987654") != 6
187 || memcmp (buf
, "aabcEDX98", 10))
190 /* These ops need runtime checking, but shouldn't __chk_fail. */
191 memcpy (buf
, "abcdefghij", l0
+ 10);
192 memmove (buf
+ 1, buf
, l0
+ 9);
193 if (memcmp (buf
, "aabcdefghi", 10))
196 if (mempcpy (buf
+ 5, "abcde", l0
+ 5) != buf
+ 10
197 || memcmp (buf
, "aabcdabcde", 10))
200 memset (buf
+ 8, 'j', l0
+ 2);
201 if (memcmp (buf
, "aabcdabcjj", 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 memcpy (a
.buf1
, "abcdefghij", l0
+ 10);
236 memmove (a
.buf1
+ 1, a
.buf1
, l0
+ 9);
237 if (memcmp (a
.buf1
, "aabcdefghi", 10))
240 if (mempcpy (a
.buf1
+ 5, "abcde", l0
+ 5) != a
.buf1
+ 10
241 || memcmp (a
.buf1
, "aabcdabcde", 10))
244 memset (a
.buf1
+ 8, 'j', l0
+ 2);
245 if (memcmp (a
.buf1
, "aabcdabcjj", 10))
248 #if __USE_FORTIFY_LEVEL < 2 || !__GNUC_PREREQ (4, 0)
249 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
250 and sufficient GCC support, as the string operations overflow
251 from a.buf1 into a.buf2. */
252 strcpy (a
.buf1
+ 4, str1
+ 5);
253 if (memcmp (a
.buf1
, "aabcEDCBA", 10))
256 if (stpcpy (a
.buf1
+ 8, str2
) != a
.buf1
+ 9
257 || memcmp (a
.buf1
, "aabcEDCBF", 10))
260 strncpy (a
.buf1
+ 6, "X", l0
+ 4);
261 if (memcmp (a
.buf1
, "aabcEDX\0\0", 10))
264 if (sprintf (a
.buf1
+ 7, "%d", num1
) != 2
265 || memcmp (a
.buf1
, "aabcEDX67", 10))
268 if (snprintf (a
.buf1
+ 7, 3, "%d", num2
) != 6
269 || memcmp (a
.buf1
, "aabcEDX98", 10))
272 a
.buf1
[l0
+ 8] = '\0';
273 strcat (a
.buf1
, "A");
274 if (memcmp (a
.buf1
, "aabcEDX9A", 10))
277 a
.buf1
[l0
+ 7] = '\0';
278 strncat (a
.buf1
, "ZYXWV", l0
+ 2);
279 if (memcmp (a
.buf1
, "aabcEDXZY", 10))
284 #if __USE_FORTIFY_LEVEL >= 1
285 /* Now check if all buffer overflows are caught at runtime. */
288 memcpy (buf
+ 1, "abcdefghij", l0
+ 10);
292 memmove (buf
+ 2, buf
+ 1, l0
+ 9);
296 p
= (char *) mempcpy (buf
+ 6, "abcde", l0
+ 5);
300 memset (buf
+ 9, 'j', l0
+ 2);
304 strcpy (buf
+ 5, str1
+ 5);
308 p
= stpcpy (buf
+ 9, str2
);
312 strncpy (buf
+ 7, "X", l0
+ 4);
316 stpncpy (buf
+ 6, "cd", l0
+ 5);
319 # if !defined __cplusplus || defined __va_arg_pack
321 sprintf (buf
+ 8, "%d", num1
);
325 snprintf (buf
+ 8, l0
+ 3, "%d", num2
);
329 swprintf (wbuf
+ 8, 3, L
"%d", num1
);
333 swprintf (wbuf
+ 8, l0
+ 3, L
"%d", num1
);
337 memcpy (buf
, str1
+ 2, l0
+ 9);
342 memcpy (buf
, str1
+ 3, l0
+ 8);
344 strncat (buf
, "ZYXWV", l0
+ 3);
348 memcpy (a
.buf1
+ 1, "abcdefghij", l0
+ 10);
352 memmove (a
.buf1
+ 2, a
.buf1
+ 1, l0
+ 9);
356 p
= (char *) mempcpy (a
.buf1
+ 6, "abcde", l0
+ 5);
360 memset (a
.buf1
+ 9, 'j', l0
+ 2);
363 # if __USE_FORTIFY_LEVEL >= 2 && __GNUC_PREREQ (4, 0)
370 strcpy (a
.buf1
+ (O
+ 4), str1
+ 5);
374 p
= stpcpy (a
.buf1
+ (O
+ 8), str2
);
378 strncpy (a
.buf1
+ (O
+ 6), "X", l0
+ 4);
381 # if !defined __cplusplus || defined __va_arg_pack
383 sprintf (a
.buf1
+ (O
+ 7), "%d", num1
);
387 snprintf (a
.buf1
+ (O
+ 7), l0
+ 3, "%d", num2
);
391 memcpy (a
.buf1
, str1
+ (3 - O
), l0
+ 8 + O
);
393 strcat (a
.buf1
, "AB");
396 memcpy (a
.buf1
, str1
+ (4 - O
), l0
+ 7 + O
);
398 strncat (a
.buf1
, "ZYXWV", l0
+ 3);
403 /* These ops can be done without runtime checking of object size. */
404 wmemcpy (wbuf
, L
"abcdefghij", 10);
405 wmemmove (wbuf
+ 1, wbuf
, 9);
406 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
409 if (wmempcpy (wbuf
+ 5, L
"abcde", 5) != wbuf
+ 10
410 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
413 wmemset (wbuf
+ 8, L
'j', 2);
414 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
417 wcscpy (wbuf
+ 4, L
"EDCBA");
418 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
421 if (wcpcpy (wbuf
+ 8, L
"F") != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
424 wcsncpy (wbuf
+ 6, L
"X", 4);
425 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
428 if (swprintf (wbuf
+ 7, 3, L
"%ls", L
"987654") >= 0
429 || wmemcmp (wbuf
, L
"aabcEDX98", 10))
432 if (swprintf (wbuf
+ 7, 3, L
"64") != 2
433 || wmemcmp (wbuf
, L
"aabcEDX64", 10))
436 /* These ops need runtime checking, but shouldn't __chk_fail. */
437 wmemcpy (wbuf
, L
"abcdefghij", l0
+ 10);
438 wmemmove (wbuf
+ 1, wbuf
, l0
+ 9);
439 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
442 if (wmempcpy (wbuf
+ 5, L
"abcde", l0
+ 5) != wbuf
+ 10
443 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
446 wmemset (wbuf
+ 8, L
'j', l0
+ 2);
447 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
450 wcscpy (wbuf
+ 4, wstr1
+ 5);
451 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
454 if (wcpcpy (wbuf
+ 8, wstr2
) != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
457 wcsncpy (wbuf
+ 6, L
"X", l0
+ 4);
458 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
461 if (wcpncpy (wbuf
+ 5, L
"cd", l0
+ 5) != wbuf
+ 7
462 || wmemcmp (wbuf
, L
"aabcEcd\0\0", 10))
465 if (swprintf (wbuf
+ 7, 3, L
"%d", num2
) >= 0
466 || wmemcmp (wbuf
, L
"aabcEcd98", 10))
469 wbuf
[l0
+ 8] = L
'\0';
471 if (wmemcmp (wbuf
, L
"aabcEcd9A", 10))
474 wbuf
[l0
+ 7] = L
'\0';
475 wcsncat (wbuf
, L
"ZYXWV", l0
+ 2);
476 if (wmemcmp (wbuf
, L
"aabcEcdZY", 10))
479 wmemcpy (wa
.buf1
, L
"abcdefghij", l0
+ 10);
480 wmemmove (wa
.buf1
+ 1, wa
.buf1
, l0
+ 9);
481 if (wmemcmp (wa
.buf1
, L
"aabcdefghi", 10))
484 if (wmempcpy (wa
.buf1
+ 5, L
"abcde", l0
+ 5) != wa
.buf1
+ 10
485 || wmemcmp (wa
.buf1
, L
"aabcdabcde", 10))
488 wmemset (wa
.buf1
+ 8, L
'j', l0
+ 2);
489 if (wmemcmp (wa
.buf1
, L
"aabcdabcjj", 10))
492 #if __USE_FORTIFY_LEVEL < 2
493 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
494 and sufficient GCC support, as the string operations overflow
495 from a.buf1 into a.buf2. */
496 wcscpy (wa
.buf1
+ 4, wstr1
+ 5);
497 if (wmemcmp (wa
.buf1
, L
"aabcEDCBA", 10))
500 if (wcpcpy (wa
.buf1
+ 8, wstr2
) != wa
.buf1
+ 9
501 || wmemcmp (wa
.buf1
, L
"aabcEDCBF", 10))
504 wcsncpy (wa
.buf1
+ 6, L
"X", l0
+ 4);
505 if (wmemcmp (wa
.buf1
, L
"aabcEDX\0\0", 10))
508 if (swprintf (wa
.buf1
+ 7, 3, L
"%d", num2
) >= 0
509 || wmemcmp (wa
.buf1
, L
"aabcEDX98", 10))
512 wa
.buf1
[l0
+ 8] = L
'\0';
513 wcscat (wa
.buf1
, L
"A");
514 if (wmemcmp (wa
.buf1
, L
"aabcEDX9A", 10))
517 wa
.buf1
[l0
+ 7] = L
'\0';
518 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 2);
519 if (wmemcmp (wa
.buf1
, L
"aabcEDXZY", 10))
524 #if __USE_FORTIFY_LEVEL >= 1
525 /* Now check if all buffer overflows are caught at runtime. */
528 wmemcpy (wbuf
+ 1, L
"abcdefghij", l0
+ 10);
532 wmemcpy (wbuf
+ 9, L
"abcdefghij", l0
+ 10);
536 wmemmove (wbuf
+ 2, wbuf
+ 1, l0
+ 9);
540 wp
= wmempcpy (wbuf
+ 6, L
"abcde", l0
+ 5);
544 wmemset (wbuf
+ 9, L
'j', l0
+ 2);
548 wcscpy (wbuf
+ 5, wstr1
+ 5);
552 wp
= wcpcpy (wbuf
+ 9, wstr2
);
556 wcsncpy (wbuf
+ 7, L
"X", l0
+ 4);
560 wcsncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
564 wcpncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
568 wcpncpy (wbuf
+ 6, L
"cd", l0
+ 5);
571 wmemcpy (wbuf
, wstr1
+ 2, l0
+ 9);
573 wcscat (wbuf
, L
"AB");
576 wmemcpy (wbuf
, wstr1
+ 3, l0
+ 8);
578 wcsncat (wbuf
, L
"ZYXWV", l0
+ 3);
582 wmemcpy (wa
.buf1
+ 1, L
"abcdefghij", l0
+ 10);
586 wmemmove (wa
.buf1
+ 2, wa
.buf1
+ 1, l0
+ 9);
590 wp
= wmempcpy (wa
.buf1
+ 6, L
"abcde", l0
+ 5);
594 wmemset (wa
.buf1
+ 9, L
'j', l0
+ 2);
597 #if __USE_FORTIFY_LEVEL >= 2
604 wcscpy (wa
.buf1
+ (O
+ 4), wstr1
+ 5);
608 wp
= wcpcpy (wa
.buf1
+ (O
+ 8), wstr2
);
612 wcsncpy (wa
.buf1
+ (O
+ 6), L
"X", l0
+ 4);
615 wmemcpy (wa
.buf1
, wstr1
+ (3 - O
), l0
+ 8 + O
);
617 wcscat (wa
.buf1
, L
"AB");
620 wmemcpy (wa
.buf1
, wstr1
+ (4 - O
), l0
+ 7 + O
);
622 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 3);
627 /* Now checks for %n protection. */
629 /* Constant literals passed directly are always ok
630 (even with warnings about possible bugs from GCC). */
632 if (sprintf (buf
, "%s%n%s%n", str2
, &n1
, str2
, &n2
) != 2
633 || n1
!= 1 || n2
!= 2)
636 /* In this case the format string is not known at compile time,
637 but resides in read-only memory, so is ok. */
638 if (snprintf (buf
, 4, str3
, str2
, &n1
, str2
, &n2
) != 2
639 || n1
!= 1 || n2
!= 2)
642 strcpy (buf2
+ 2, "%n%s%n");
643 /* When the format string is writable and contains %n,
644 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
646 if (sprintf (buf
, buf2
, str2
, &n1
, str2
, &n1
) != 2)
651 if (snprintf (buf
, 3, buf2
, str2
, &n1
, str2
, &n1
) != 2)
655 /* But if there is no %n, even writable format string
658 if (sprintf (buf
, buf2
+ 4, str2
) != 1)
661 /* Constant literals passed directly are always ok
662 (even with warnings about possible bugs from GCC). */
663 if (printf ("%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
664 || n1
!= 7 || n2
!= 14)
667 /* In this case the format string is not known at compile time,
668 but resides in read-only memory, so is ok. */
669 if (printf (str3
, str4
, &n1
, str5
, &n2
) != 14
670 || n1
!= 7 || n2
!= 14)
673 strcpy (buf2
+ 2, "%n%s%n");
674 /* When the format string is writable and contains %n,
675 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
677 if (printf (buf2
, str4
, &n1
, str5
, &n1
) != 14)
681 /* But if there is no %n, even writable format string
684 if (printf (buf2
+ 4, str5
) != 7)
689 /* Constant literals passed directly are always ok
690 (even with warnings about possible bugs from GCC). */
691 if (fprintf (fp
, "%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
692 || n1
!= 7 || n2
!= 14)
695 /* In this case the format string is not known at compile time,
696 but resides in read-only memory, so is ok. */
697 if (fprintf (fp
, str3
, str4
, &n1
, str5
, &n2
) != 14
698 || n1
!= 7 || n2
!= 14)
701 strcpy (buf2
+ 2, "%n%s%n");
702 /* When the format string is writable and contains %n,
703 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
705 if (fprintf (fp
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
709 /* But if there is no %n, even writable format string
712 if (fprintf (fp
, buf2
+ 4, str5
) != 7)
716 strcpy (buf2
+ 2, "%n%s%n");
717 /* When the format string is writable and contains %n,
718 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
720 if (asprintf (&my_ptr
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
729 if (obstack_printf (&obs
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
732 obstack_free (&obs
, NULL
);
735 if (asprintf (&my_ptr
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
741 if (obstack_printf (&obs
, "%s%n%s%n", str4
, &n1
, str5
, &n1
) != 14)
743 obstack_free (&obs
, NULL
);
745 if (freopen (temp_filename
, "r", stdin
) == NULL
)
747 puts ("could not open temporary file");
751 if (gets (buf
) != buf
|| memcmp (buf
, "abcdefgh", 9))
753 if (gets (buf
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
756 #if __USE_FORTIFY_LEVEL >= 1
758 if (gets (buf
) != buf
)
765 if (fgets (buf
, sizeof (buf
), stdin
) != buf
766 || memcmp (buf
, "abcdefgh\n", 10))
768 if (fgets (buf
, sizeof (buf
), stdin
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
773 if (fgets (buf
, l0
+ sizeof (buf
), stdin
) != buf
774 || memcmp (buf
, "abcdefgh\n", 10))
777 #if __USE_FORTIFY_LEVEL >= 1
779 if (fgets (buf
, sizeof (buf
) + 1, stdin
) != buf
)
784 if (fgets (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
791 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
792 || memcmp (buf
, "abcdefgh\n", 10))
794 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
795 || memcmp (buf
, "ABCDEFGHI", 10))
800 if (fgets_unlocked (buf
, l0
+ sizeof (buf
), stdin
) != buf
801 || memcmp (buf
, "abcdefgh\n", 10))
804 #if __USE_FORTIFY_LEVEL >= 1
806 if (fgets_unlocked (buf
, sizeof (buf
) + 1, stdin
) != buf
)
811 if (fgets_unlocked (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
818 if (fread (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
819 || memcmp (buf
, "abcdefgh\nA", 10))
821 if (fread (buf
, sizeof (buf
), 1, stdin
) != 1
822 || memcmp (buf
, "BCDEFGHI\na", 10))
827 if (fread (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
828 || memcmp (buf
, "abcdefgh\nA", 10))
830 if (fread (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
831 || memcmp (buf
, "BCDEFGHI\na", 10))
834 #if __USE_FORTIFY_LEVEL >= 1
836 if (fread (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
841 if (fread (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
848 if (fread_unlocked (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
849 || memcmp (buf
, "abcdefgh\nA", 10))
851 if (fread_unlocked (buf
, sizeof (buf
), 1, stdin
) != 1
852 || memcmp (buf
, "BCDEFGHI\na", 10))
857 if (fread_unlocked (buf
, 1, 4, stdin
) != 4
858 || memcmp (buf
, "abcdFGHI\na", 10))
860 if (fread_unlocked (buf
, 4, 1, stdin
) != 1
861 || memcmp (buf
, "efghFGHI\na", 10))
866 if (fread_unlocked (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
867 || memcmp (buf
, "abcdefgh\nA", 10))
869 if (fread_unlocked (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
870 || memcmp (buf
, "BCDEFGHI\na", 10))
873 #if __USE_FORTIFY_LEVEL >= 1
875 if (fread_unlocked (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
880 if (fread_unlocked (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
885 lseek (fileno (stdin
), 0, SEEK_SET
);
887 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
888 || memcmp (buf
, "abcdefgh\n", 9))
890 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
891 || memcmp (buf
, "ABCDEFGHI", 9))
894 lseek (fileno (stdin
), 0, SEEK_SET
);
896 if (read (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1) != sizeof (buf
) - 1
897 || memcmp (buf
, "abcdefgh\n", 9))
900 #if __USE_FORTIFY_LEVEL >= 1
902 if (read (fileno (stdin
), buf
, sizeof (buf
) + 1) != sizeof (buf
) + 1)
907 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
909 || memcmp (buf
, "\nABCDEFGH", 9))
911 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
912 || memcmp (buf
, "abcdefgh\n", 9))
914 if (pread (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
916 || memcmp (buf
, "h\nABCDEFG", 9))
919 #if __USE_FORTIFY_LEVEL >= 1
921 if (pread (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
927 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
929 || memcmp (buf
, "\nABCDEFGH", 9))
931 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
932 || memcmp (buf
, "abcdefgh\n", 9))
934 if (pread64 (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
936 || memcmp (buf
, "h\nABCDEFG", 9))
939 #if __USE_FORTIFY_LEVEL >= 1
941 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
947 if (freopen (temp_filename
, "r", stdin
) == NULL
)
949 puts ("could not open temporary file");
953 if (fseek (stdin
, 9 + 10 + 11, SEEK_SET
))
955 puts ("could not seek in test file");
959 #if __USE_FORTIFY_LEVEL >= 1
961 if (gets (buf
) != buf
)
966 /* Check whether missing N$ formats are detected. */
968 printf ("%3$d\n", 1, 2, 3, 4);
972 fprintf (stdout
, "%3$d\n", 1, 2, 3, 4);
976 sprintf (buf
, "%3$d\n", 1, 2, 3, 4);
980 snprintf (buf
, sizeof (buf
), "%3$d\n", 1, 2, 3, 4);
984 if (socketpair (PF_UNIX
, SOCK_STREAM
, 0, sp
))
988 const char *sendstr
= "abcdefgh\nABCDEFGH\n0123456789\n";
989 if ((size_t) send (sp
[0], sendstr
, strlen (sendstr
), 0)
994 if (recv (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
)
996 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
999 if (recv (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
)
1000 != sizeof recvbuf
- 7
1001 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
1004 #if __USE_FORTIFY_LEVEL >= 1
1006 if (recv (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
)
1012 if (recv (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
)
1013 != sizeof recvbuf
- 3)
1019 struct sockaddr_un sa_un
;
1021 sl
= sizeof (sa_un
);
1022 if (recvfrom (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
,
1023 (struct sockaddr
*) &sa_un
, &sl
)
1025 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
1028 sl
= sizeof (sa_un
);
1029 if (recvfrom (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
,
1030 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 7
1031 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
1034 #if __USE_FORTIFY_LEVEL >= 1
1036 sl
= sizeof (sa_un
);
1037 if (recvfrom (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
,
1038 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
)
1043 sl
= sizeof (sa_un
);
1044 if (recvfrom (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
,
1045 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 3)
1054 char fname
[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1055 char *enddir
= strchr (fname
, '\0');
1056 if (mkdtemp (fname
) == NULL
)
1058 printf ("mkdtemp failed: %m\n");
1062 if (symlink ("bar", fname
) != 0)
1065 char readlinkbuf
[4];
1066 if (readlink (fname
, readlinkbuf
, 4) != 3
1067 || memcmp (readlinkbuf
, "bar", 3) != 0)
1069 if (readlink (fname
, readlinkbuf
+ 1, l0
+ 3) != 3
1070 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1073 #if __USE_FORTIFY_LEVEL >= 1
1075 if (readlink (fname
, readlinkbuf
+ 2, l0
+ 3) != 3)
1080 if (readlink (fname
, readlinkbuf
+ 3, 4) != 3)
1085 int tmpfd
= open ("/tmp", O_RDONLY
| O_DIRECTORY
);
1089 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
, 4) != 3
1090 || memcmp (readlinkbuf
, "bar", 3) != 0)
1092 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 1,
1094 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1097 #if __USE_FORTIFY_LEVEL >= 1
1099 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 2,
1105 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 3,
1113 char *cwd1
= getcwd (NULL
, 0);
1117 char *cwd2
= getcwd (NULL
, 250);
1123 if (strcmp (cwd1
, cwd2
) != 0)
1130 char *cwd3
= getcwd (NULL
, 0);
1133 if (strcmp (fname
, cwd3
) != 0)
1134 printf ("getcwd after chdir is '%s' != '%s',"
1135 "get{c,}wd tests skipped\n", cwd3
, fname
);
1138 char getcwdbuf
[sizeof fname
- 3];
1140 char *cwd4
= getcwd (getcwdbuf
, sizeof getcwdbuf
);
1141 if (cwd4
!= getcwdbuf
1142 || strcmp (getcwdbuf
, fname
) != 0)
1145 cwd4
= getcwd (getcwdbuf
+ 1, l0
+ sizeof getcwdbuf
- 1);
1146 if (cwd4
!= getcwdbuf
+ 1
1147 || getcwdbuf
[0] != fname
[0]
1148 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1151 #if __USE_FORTIFY_LEVEL >= 1
1153 if (getcwd (getcwdbuf
+ 2, l0
+ sizeof getcwdbuf
)
1159 if (getcwd (getcwdbuf
+ 2, sizeof getcwdbuf
)
1165 if (getwd (getcwdbuf
) != getcwdbuf
1166 || strcmp (getcwdbuf
, fname
) != 0)
1169 if (getwd (getcwdbuf
+ 1) != getcwdbuf
+ 1
1170 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1173 #if __USE_FORTIFY_LEVEL >= 1
1175 if (getwd (getcwdbuf
+ 2) != getcwdbuf
+ 2)
1181 if (chdir (cwd1
) != 0)
1189 if (unlink (fname
) != 0)
1193 if (rmdir (fname
) != 0)
1198 char largebuf
[PATH_MAX
];
1199 char *realres
= realpath (".", largebuf
);
1200 if (realres
!= largebuf
)
1203 # if __USE_FORTIFY_LEVEL >= 1
1206 realres
= realpath (".", realbuf
);
1207 if (realres
!= realbuf
)
1213 if (setlocale (LC_ALL
, "de_DE.UTF-8") != NULL
)
1215 assert (MB_CUR_MAX
<= 10);
1217 /* First a simple test. */
1219 if (wctomb (enough
, L
'A') != 1)
1222 #if __USE_FORTIFY_LEVEL >= 1
1223 /* We know the wchar_t encoding is ISO 10646. So pick a
1224 character which has a multibyte representation which does not
1228 if (wctomb (smallbuf
, L
'\x100') != 2)
1234 memset (&s
, '\0', sizeof (s
));
1235 if (wcrtomb (enough
, L
'D', &s
) != 1 || enough
[0] != 'D')
1238 #if __USE_FORTIFY_LEVEL >= 1
1239 /* We know the wchar_t encoding is ISO 10646. So pick a
1240 character which has a multibyte representation which does not
1244 if (wcrtomb (smallbuf
, L
'\x100', &s
) != 2)
1249 wchar_t wenough
[10];
1250 memset (&s
, '\0', sizeof (s
));
1251 const char *cp
= "A";
1252 if (mbsrtowcs (wenough
, &cp
, 10, &s
) != 1
1253 || wcscmp (wenough
, L
"A") != 0)
1257 if (mbsrtowcs (wenough
, &cp
, l0
+ 10, &s
) != 2
1258 || wcscmp (wenough
, L
"BC") != 0)
1261 #if __USE_FORTIFY_LEVEL >= 1
1263 wchar_t wsmallbuf
[2];
1265 mbsrtowcs (wsmallbuf
, &cp
, 10, &s
);
1270 if (mbstowcs (wenough
, cp
, 10) != 1
1271 || wcscmp (wenough
, L
"A") != 0)
1275 if (mbstowcs (wenough
, cp
, l0
+ 10) != 3
1276 || wcscmp (wenough
, L
"DEF") != 0)
1279 #if __USE_FORTIFY_LEVEL >= 1
1281 wchar_t wsmallbuf
[2];
1283 mbstowcs (wsmallbuf
, cp
, 10);
1287 memset (&s
, '\0', sizeof (s
));
1289 wcscpy (wenough
, L
"DEF");
1290 if (mbsnrtowcs (wenough
, &cp
, 1, 10, &s
) != 1
1291 || wcscmp (wenough
, L
"AEF") != 0)
1295 if (mbsnrtowcs (wenough
, &cp
, 1, l0
+ 10, &s
) != 1
1296 || wcscmp (wenough
, L
"IEF") != 0)
1299 #if __USE_FORTIFY_LEVEL >= 1
1301 wchar_t wsmallbuf
[2];
1303 mbsnrtowcs (wsmallbuf
, &cp
, 3, 10, &s
);
1307 memset (&s
, '\0', sizeof (s
));
1308 const wchar_t *wcp
= L
"A";
1309 if (wcsrtombs (enough
, &wcp
, 10, &s
) != 1
1310 || strcmp (enough
, "A") != 0)
1314 if (wcsrtombs (enough
, &wcp
, l0
+ 10, &s
) != 2
1315 || strcmp (enough
, "BC") != 0)
1318 #if __USE_FORTIFY_LEVEL >= 1
1322 wcsrtombs (smallbuf
, &wcp
, 10, &s
);
1326 memset (enough
, 'Z', sizeof (enough
));
1328 if (wcstombs (enough
, wcp
, 10) != 2
1329 || strcmp (enough
, "EF") != 0)
1333 if (wcstombs (enough
, wcp
, l0
+ 10) != 1
1334 || strcmp (enough
, "G") != 0)
1337 #if __USE_FORTIFY_LEVEL >= 1
1341 wcstombs (smallbuf
, wcp
, 10);
1345 memset (&s
, '\0', sizeof (s
));
1347 if (wcsnrtombs (enough
, &wcp
, 1, 10, &s
) != 1
1348 || strcmp (enough
, "A") != 0)
1352 if (wcsnrtombs (enough
, &wcp
, 1, l0
+ 10, &s
) != 1
1353 || strcmp (enough
, "B") != 0)
1356 #if __USE_FORTIFY_LEVEL >= 1
1360 wcsnrtombs (smallbuf
, &wcp
, 3, 10, &s
);
1366 puts ("cannot set locale");
1370 fd
= posix_openpt (O_RDWR
);
1374 if (ptsname_r (fd
, enough
, sizeof (enough
)) != 0)
1377 #if __USE_FORTIFY_LEVEL >= 1
1380 if (ptsname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1388 confstr (_CS_GNU_LIBC_VERSION
, largebuf
, sizeof (largebuf
));
1389 # if __USE_FORTIFY_LEVEL >= 1
1392 confstr (_CS_GNU_LIBC_VERSION
, smallbuf
, sizeof (largebuf
));
1398 int ngr
= getgroups (5, grpslarge
);
1399 asm volatile ("" : : "r" (ngr
));
1400 #if __USE_FORTIFY_LEVEL >= 1
1403 ngr
= getgroups (5, (gid_t
*) smallbuf
);
1404 asm volatile ("" : : "r" (ngr
));
1408 fd
= open (_PATH_TTY
, O_RDONLY
);
1412 if (ttyname_r (fd
, enough
, sizeof (enough
)) != 0)
1415 #if __USE_FORTIFY_LEVEL >= 1
1418 if (ttyname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1425 char hostnamelarge
[1000];
1426 gethostname (hostnamelarge
, sizeof (hostnamelarge
));
1427 #if __USE_FORTIFY_LEVEL >= 1
1430 gethostname (smallbuf
, sizeof (hostnamelarge
));
1434 char loginlarge
[1000];
1435 getlogin_r (loginlarge
, sizeof (hostnamelarge
));
1436 #if __USE_FORTIFY_LEVEL >= 1
1439 getlogin_r (smallbuf
, sizeof (loginlarge
));
1443 char domainnamelarge
[1000];
1444 int res
= getdomainname (domainnamelarge
, sizeof (domainnamelarge
));
1445 asm volatile ("" : : "r" (res
));
1446 #if __USE_FORTIFY_LEVEL >= 1
1449 res
= getdomainname (smallbuf
, sizeof (domainnamelarge
));
1450 asm volatile ("" : : "r" (res
));