1 /* Copyright (C) 2004, 2005, 2006, 2007 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
31 #include <sys/socket.h>
35 static void do_prepare (void);
36 static int do_test (void);
37 #define PREPARE(argc, argv) do_prepare ()
38 #define TEST_FUNCTION do_test ()
39 #include "../test-skeleton.c"
44 int temp_fd
= create_temp_file ("tst-chk1.", &temp_filename
);
47 printf ("cannot create temporary file: %m\n");
51 const char *strs
= "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
52 if ((size_t) write (temp_fd
, strs
, strlen (strs
)) != strlen (strs
))
54 puts ("could not write test strings into file");
55 unlink (temp_filename
);
60 volatile int chk_fail_ok
;
70 longjmp (chk_fail_buf
, 1);
81 const char *str1
= "JIHGFEDCBA";
82 const char *str2
= "F";
83 const char *str3
= "%s%n%s%n";
84 const char *str4
= "Hello, ";
85 const char *str5
= "World!\n";
86 const wchar_t *wstr1
= L
"JIHGFEDCBA";
87 const wchar_t *wstr2
= L
"F";
88 const wchar_t *wstr3
= L
"%s%n%s%n";
89 const wchar_t *wstr4
= L
"Hello, ";
90 const wchar_t *wstr5
= L
"World!\n";
96 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
97 #define CHK_FAIL_START \
99 if (! setjmp (chk_fail_buf)) \
101 #define CHK_FAIL_END \
105 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
106 #define CHK_FAIL2_START CHK_FAIL_START
107 #define CHK_FAIL2_END CHK_FAIL_END
109 #define CHK_FAIL2_START
110 #define CHK_FAIL2_END
117 sa
.sa_handler
= handler
;
119 sigemptyset (&sa
.sa_mask
);
121 sigaction (SIGABRT
, &sa
, NULL
);
123 /* Avoid all the buffer overflow messages on stderr. */
124 int fd
= open (_PATH_DEVNULL
, O_WRONLY
);
126 close (STDERR_FILENO
);
129 dup2 (fd
, STDERR_FILENO
);
132 setenv ("LIBC_FATAL_STDERR_", "1", 1);
134 struct A
{ char buf1
[9]; char buf2
[1]; } a
;
135 struct wA
{ wchar_t buf1
[9]; wchar_t buf2
[1]; } wa
;
137 printf ("Test checking routines at fortify level %d\n",
138 #ifdef __USE_FORTIFY_LEVEL
139 (int) __USE_FORTIFY_LEVEL
145 #if defined __USE_FORTIFY_LEVEL && !defined __extern_always_inline
146 printf ("Test skipped");
151 /* These ops can be done without runtime checking of object size. */
152 memcpy (buf
, "abcdefghij", 10);
153 memmove (buf
+ 1, buf
, 9);
154 if (memcmp (buf
, "aabcdefghi", 10))
157 if (mempcpy (buf
+ 5, "abcde", 5) != buf
+ 10
158 || memcmp (buf
, "aabcdabcde", 10))
161 memset (buf
+ 8, 'j', 2);
162 if (memcmp (buf
, "aabcdabcjj", 10))
165 strcpy (buf
+ 4, "EDCBA");
166 if (memcmp (buf
, "aabcEDCBA", 10))
169 if (stpcpy (buf
+ 8, "F") != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
172 strncpy (buf
+ 6, "X", 4);
173 if (memcmp (buf
, "aabcEDX\0\0", 10))
176 if (sprintf (buf
+ 7, "%s", "67") != 2 || memcmp (buf
, "aabcEDX67", 10))
179 if (snprintf (buf
+ 7, 3, "%s", "987654") != 6
180 || memcmp (buf
, "aabcEDX98", 10))
183 /* These ops need runtime checking, but shouldn't __chk_fail. */
184 memcpy (buf
, "abcdefghij", l0
+ 10);
185 memmove (buf
+ 1, buf
, l0
+ 9);
186 if (memcmp (buf
, "aabcdefghi", 10))
189 if (mempcpy (buf
+ 5, "abcde", l0
+ 5) != buf
+ 10
190 || memcmp (buf
, "aabcdabcde", 10))
193 memset (buf
+ 8, 'j', l0
+ 2);
194 if (memcmp (buf
, "aabcdabcjj", 10))
197 strcpy (buf
+ 4, str1
+ 5);
198 if (memcmp (buf
, "aabcEDCBA", 10))
201 if (stpcpy (buf
+ 8, str2
) != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
204 strncpy (buf
+ 6, "X", l0
+ 4);
205 if (memcmp (buf
, "aabcEDX\0\0", 10))
208 if (stpncpy (buf
+ 5, "cd", l0
+ 5) != buf
+ 7
209 || memcmp (buf
, "aabcEcd\0\0", 10))
212 if (sprintf (buf
+ 7, "%d", num1
) != 2 || memcmp (buf
, "aabcEcd67", 10))
215 if (snprintf (buf
+ 7, 3, "%d", num2
) != 6 || memcmp (buf
, "aabcEcd98", 10))
220 if (memcmp (buf
, "aabcEcd9A", 10))
224 strncat (buf
, "ZYXWV", l0
+ 2);
225 if (memcmp (buf
, "aabcEcdZY", 10))
228 memcpy (a
.buf1
, "abcdefghij", l0
+ 10);
229 memmove (a
.buf1
+ 1, a
.buf1
, l0
+ 9);
230 if (memcmp (a
.buf1
, "aabcdefghi", 10))
233 if (mempcpy (a
.buf1
+ 5, "abcde", l0
+ 5) != a
.buf1
+ 10
234 || memcmp (a
.buf1
, "aabcdabcde", 10))
237 memset (a
.buf1
+ 8, 'j', l0
+ 2);
238 if (memcmp (a
.buf1
, "aabcdabcjj", 10))
241 #if __USE_FORTIFY_LEVEL < 2
242 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
243 and sufficient GCC support, as the string operations overflow
244 from a.buf1 into a.buf2. */
245 strcpy (a
.buf1
+ 4, str1
+ 5);
246 if (memcmp (a
.buf1
, "aabcEDCBA", 10))
249 if (stpcpy (a
.buf1
+ 8, str2
) != a
.buf1
+ 9
250 || memcmp (a
.buf1
, "aabcEDCBF", 10))
253 strncpy (a
.buf1
+ 6, "X", l0
+ 4);
254 if (memcmp (a
.buf1
, "aabcEDX\0\0", 10))
257 if (sprintf (a
.buf1
+ 7, "%d", num1
) != 2
258 || memcmp (a
.buf1
, "aabcEDX67", 10))
261 if (snprintf (a
.buf1
+ 7, 3, "%d", num2
) != 6
262 || memcmp (a
.buf1
, "aabcEDX98", 10))
265 a
.buf1
[l0
+ 8] = '\0';
266 strcat (a
.buf1
, "A");
267 if (memcmp (a
.buf1
, "aabcEDX9A", 10))
270 a
.buf1
[l0
+ 7] = '\0';
271 strncat (a
.buf1
, "ZYXWV", l0
+ 2);
272 if (memcmp (a
.buf1
, "aabcEDXZY", 10))
277 #if __USE_FORTIFY_LEVEL >= 1
278 /* Now check if all buffer overflows are caught at runtime. */
281 memcpy (buf
+ 1, "abcdefghij", l0
+ 10);
285 memmove (buf
+ 2, buf
+ 1, l0
+ 9);
289 p
= (char *) mempcpy (buf
+ 6, "abcde", l0
+ 5);
293 memset (buf
+ 9, 'j', l0
+ 2);
297 strcpy (buf
+ 5, str1
+ 5);
301 p
= stpcpy (buf
+ 9, str2
);
305 strncpy (buf
+ 7, "X", l0
+ 4);
309 stpncpy (buf
+ 6, "cd", l0
+ 5);
312 # if !defined __cplusplus || defined __va_arg_pack
314 sprintf (buf
+ 8, "%d", num1
);
318 snprintf (buf
+ 8, l0
+ 3, "%d", num2
);
322 swprintf (wbuf
+ 8, 3, L
"%d", num1
);
326 swprintf (wbuf
+ 8, l0
+ 3, L
"%d", num1
);
330 memcpy (buf
, str1
+ 2, l0
+ 9);
335 memcpy (buf
, str1
+ 3, l0
+ 8);
337 strncat (buf
, "ZYXWV", l0
+ 3);
341 memcpy (a
.buf1
+ 1, "abcdefghij", l0
+ 10);
345 memmove (a
.buf1
+ 2, a
.buf1
+ 1, l0
+ 9);
349 p
= (char *) mempcpy (a
.buf1
+ 6, "abcde", l0
+ 5);
353 memset (a
.buf1
+ 9, 'j', l0
+ 2);
356 # if __USE_FORTIFY_LEVEL >= 2
363 strcpy (a
.buf1
+ (O
+ 4), str1
+ 5);
367 p
= stpcpy (a
.buf1
+ (O
+ 8), str2
);
371 strncpy (a
.buf1
+ (O
+ 6), "X", l0
+ 4);
374 # if !defined __cplusplus || defined __va_arg_pack
376 sprintf (a
.buf1
+ (O
+ 7), "%d", num1
);
380 snprintf (a
.buf1
+ (O
+ 7), l0
+ 3, "%d", num2
);
384 memcpy (a
.buf1
, str1
+ (3 - O
), l0
+ 8 + O
);
386 strcat (a
.buf1
, "AB");
389 memcpy (a
.buf1
, str1
+ (4 - O
), l0
+ 7 + O
);
391 strncat (a
.buf1
, "ZYXWV", l0
+ 3);
396 /* These ops can be done without runtime checking of object size. */
397 wmemcpy (wbuf
, L
"abcdefghij", 10);
398 wmemmove (wbuf
+ 1, wbuf
, 9);
399 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
402 if (wmempcpy (wbuf
+ 5, L
"abcde", 5) != wbuf
+ 10
403 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
406 wmemset (wbuf
+ 8, L
'j', 2);
407 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
410 wcscpy (wbuf
+ 4, L
"EDCBA");
411 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
414 if (wcpcpy (wbuf
+ 8, L
"F") != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
417 wcsncpy (wbuf
+ 6, L
"X", 4);
418 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
421 if (swprintf (wbuf
+ 7, 3, L
"%ls", L
"987654") >= 0
422 || wmemcmp (wbuf
, L
"aabcEDX98", 10))
425 if (swprintf (wbuf
+ 7, 3, L
"64") != 2
426 || wmemcmp (wbuf
, L
"aabcEDX64", 10))
429 /* These ops need runtime checking, but shouldn't __chk_fail. */
430 wmemcpy (wbuf
, L
"abcdefghij", l0
+ 10);
431 wmemmove (wbuf
+ 1, wbuf
, l0
+ 9);
432 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
435 if (wmempcpy (wbuf
+ 5, L
"abcde", l0
+ 5) != wbuf
+ 10
436 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
439 wmemset (wbuf
+ 8, L
'j', l0
+ 2);
440 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
443 wcscpy (wbuf
+ 4, wstr1
+ 5);
444 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
447 if (wcpcpy (wbuf
+ 8, wstr2
) != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
450 wcsncpy (wbuf
+ 6, L
"X", l0
+ 4);
451 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
454 if (wcpncpy (wbuf
+ 5, L
"cd", l0
+ 5) != wbuf
+ 7
455 || wmemcmp (wbuf
, L
"aabcEcd\0\0", 10))
458 if (swprintf (wbuf
+ 7, 3, L
"%d", num2
) >= 0
459 || wmemcmp (wbuf
, L
"aabcEcd98", 10))
462 wbuf
[l0
+ 8] = L
'\0';
464 if (wmemcmp (wbuf
, L
"aabcEcd9A", 10))
467 wbuf
[l0
+ 7] = L
'\0';
468 wcsncat (wbuf
, L
"ZYXWV", l0
+ 2);
469 if (wmemcmp (wbuf
, L
"aabcEcdZY", 10))
472 wmemcpy (wa
.buf1
, L
"abcdefghij", l0
+ 10);
473 wmemmove (wa
.buf1
+ 1, wa
.buf1
, l0
+ 9);
474 if (wmemcmp (wa
.buf1
, L
"aabcdefghi", 10))
477 if (wmempcpy (wa
.buf1
+ 5, L
"abcde", l0
+ 5) != wa
.buf1
+ 10
478 || wmemcmp (wa
.buf1
, L
"aabcdabcde", 10))
481 wmemset (wa
.buf1
+ 8, L
'j', l0
+ 2);
482 if (wmemcmp (wa
.buf1
, L
"aabcdabcjj", 10))
485 #if __USE_FORTIFY_LEVEL < 2
486 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
487 and sufficient GCC support, as the string operations overflow
488 from a.buf1 into a.buf2. */
489 wcscpy (wa
.buf1
+ 4, wstr1
+ 5);
490 if (wmemcmp (wa
.buf1
, L
"aabcEDCBA", 10))
493 if (wcpcpy (wa
.buf1
+ 8, wstr2
) != wa
.buf1
+ 9
494 || wmemcmp (wa
.buf1
, L
"aabcEDCBF", 10))
497 wcsncpy (wa
.buf1
+ 6, L
"X", l0
+ 4);
498 if (wmemcmp (wa
.buf1
, L
"aabcEDX\0\0", 10))
501 if (swprintf (wa
.buf1
+ 7, 3, L
"%d", num2
) >= 0
502 || wmemcmp (wa
.buf1
, L
"aabcEDX98", 10))
505 wa
.buf1
[l0
+ 8] = L
'\0';
506 wcscat (wa
.buf1
, L
"A");
507 if (wmemcmp (wa
.buf1
, L
"aabcEDX9A", 10))
510 wa
.buf1
[l0
+ 7] = L
'\0';
511 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 2);
512 if (wmemcmp (wa
.buf1
, L
"aabcEDXZY", 10))
517 #if __USE_FORTIFY_LEVEL >= 1
518 /* Now check if all buffer overflows are caught at runtime. */
521 wmemcpy (wbuf
+ 1, L
"abcdefghij", l0
+ 10);
525 wmemcpy (wbuf
+ 9, L
"abcdefghij", l0
+ 10);
529 wmemmove (wbuf
+ 2, wbuf
+ 1, l0
+ 9);
533 wp
= wmempcpy (wbuf
+ 6, L
"abcde", l0
+ 5);
537 wmemset (wbuf
+ 9, L
'j', l0
+ 2);
541 wcscpy (wbuf
+ 5, wstr1
+ 5);
545 wp
= wcpcpy (wbuf
+ 9, wstr2
);
549 wcsncpy (wbuf
+ 7, L
"X", l0
+ 4);
553 wcsncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
557 wcpncpy (wbuf
+ 9, L
"XABCDEFGH", 8);
561 wcpncpy (wbuf
+ 6, L
"cd", l0
+ 5);
564 wmemcpy (wbuf
, wstr1
+ 2, l0
+ 9);
566 wcscat (wbuf
, L
"AB");
569 wmemcpy (wbuf
, wstr1
+ 3, l0
+ 8);
571 wcsncat (wbuf
, L
"ZYXWV", l0
+ 3);
575 wmemcpy (wa
.buf1
+ 1, L
"abcdefghij", l0
+ 10);
579 wmemmove (wa
.buf1
+ 2, wa
.buf1
+ 1, l0
+ 9);
583 wp
= wmempcpy (wa
.buf1
+ 6, L
"abcde", l0
+ 5);
587 wmemset (wa
.buf1
+ 9, L
'j', l0
+ 2);
590 #if __USE_FORTIFY_LEVEL >= 2
597 wcscpy (wa
.buf1
+ (O
+ 4), wstr1
+ 5);
601 wp
= wcpcpy (wa
.buf1
+ (O
+ 8), wstr2
);
605 wcsncpy (wa
.buf1
+ (O
+ 6), L
"X", l0
+ 4);
608 wmemcpy (wa
.buf1
, wstr1
+ (3 - O
), l0
+ 8 + O
);
610 wcscat (wa
.buf1
, L
"AB");
613 wmemcpy (wa
.buf1
, wstr1
+ (4 - O
), l0
+ 7 + O
);
615 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 3);
620 /* Now checks for %n protection. */
622 /* Constant literals passed directly are always ok
623 (even with warnings about possible bugs from GCC). */
625 if (sprintf (buf
, "%s%n%s%n", str2
, &n1
, str2
, &n2
) != 2
626 || n1
!= 1 || n2
!= 2)
629 /* In this case the format string is not known at compile time,
630 but resides in read-only memory, so is ok. */
631 if (snprintf (buf
, 4, str3
, str2
, &n1
, str2
, &n2
) != 2
632 || n1
!= 1 || n2
!= 2)
635 strcpy (buf2
+ 2, "%n%s%n");
636 /* When the format string is writable and contains %n,
637 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
639 if (sprintf (buf
, buf2
, str2
, &n1
, str2
, &n1
) != 2)
644 if (snprintf (buf
, 3, buf2
, str2
, &n1
, str2
, &n1
) != 2)
648 /* But if there is no %n, even writable format string
651 if (sprintf (buf
, buf2
+ 4, str2
) != 1)
654 /* Constant literals passed directly are always ok
655 (even with warnings about possible bugs from GCC). */
656 if (printf ("%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
657 || n1
!= 7 || n2
!= 14)
660 /* In this case the format string is not known at compile time,
661 but resides in read-only memory, so is ok. */
662 if (printf (str3
, str4
, &n1
, str5
, &n2
) != 14
663 || n1
!= 7 || n2
!= 14)
666 strcpy (buf2
+ 2, "%n%s%n");
667 /* When the format string is writable and contains %n,
668 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
670 if (printf (buf2
, str4
, &n1
, str5
, &n1
) != 14)
674 /* But if there is no %n, even writable format string
677 if (printf (buf2
+ 4, str5
) != 7)
682 /* Constant literals passed directly are always ok
683 (even with warnings about possible bugs from GCC). */
684 if (fprintf (fp
, "%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
685 || n1
!= 7 || n2
!= 14)
688 /* In this case the format string is not known at compile time,
689 but resides in read-only memory, so is ok. */
690 if (fprintf (fp
, str3
, str4
, &n1
, str5
, &n2
) != 14
691 || n1
!= 7 || n2
!= 14)
694 strcpy (buf2
+ 2, "%n%s%n");
695 /* When the format string is writable and contains %n,
696 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
698 if (fprintf (fp
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
702 /* But if there is no %n, even writable format string
705 if (fprintf (fp
, buf2
+ 4, str5
) != 7)
708 if (freopen (temp_filename
, "r", stdin
) == NULL
)
710 puts ("could not open temporary file");
714 if (gets (buf
) != buf
|| memcmp (buf
, "abcdefgh", 9))
716 if (gets (buf
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
719 #if __USE_FORTIFY_LEVEL >= 1
721 if (gets (buf
) != buf
)
728 if (fgets (buf
, sizeof (buf
), stdin
) != buf
729 || memcmp (buf
, "abcdefgh\n", 10))
731 if (fgets (buf
, sizeof (buf
), stdin
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
736 if (fgets (buf
, l0
+ sizeof (buf
), stdin
) != buf
737 || memcmp (buf
, "abcdefgh\n", 10))
740 #if __USE_FORTIFY_LEVEL >= 1
742 if (fgets (buf
, sizeof (buf
) + 1, stdin
) != buf
)
747 if (fgets (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
754 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
755 || memcmp (buf
, "abcdefgh\n", 10))
757 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
758 || memcmp (buf
, "ABCDEFGHI", 10))
763 if (fgets_unlocked (buf
, l0
+ sizeof (buf
), stdin
) != buf
764 || memcmp (buf
, "abcdefgh\n", 10))
767 #if __USE_FORTIFY_LEVEL >= 1
769 if (fgets_unlocked (buf
, sizeof (buf
) + 1, stdin
) != buf
)
774 if (fgets_unlocked (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
781 if (fread (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
782 || memcmp (buf
, "abcdefgh\nA", 10))
784 if (fread (buf
, sizeof (buf
), 1, stdin
) != 1
785 || memcmp (buf
, "BCDEFGHI\na", 10))
790 if (fread (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
791 || memcmp (buf
, "abcdefgh\nA", 10))
793 if (fread (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
794 || memcmp (buf
, "BCDEFGHI\na", 10))
797 #if __USE_FORTIFY_LEVEL >= 1
799 if (fread (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
804 if (fread (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
811 if (fread_unlocked (buf
, 1, sizeof (buf
), stdin
) != sizeof (buf
)
812 || memcmp (buf
, "abcdefgh\nA", 10))
814 if (fread_unlocked (buf
, sizeof (buf
), 1, stdin
) != 1
815 || memcmp (buf
, "BCDEFGHI\na", 10))
820 if (fread_unlocked (buf
, 1, 4, stdin
) != 4
821 || memcmp (buf
, "abcdFGHI\na", 10))
823 if (fread_unlocked (buf
, 4, 1, stdin
) != 1
824 || memcmp (buf
, "efghFGHI\na", 10))
829 if (fread_unlocked (buf
, l0
+ 1, sizeof (buf
), stdin
) != sizeof (buf
)
830 || memcmp (buf
, "abcdefgh\nA", 10))
832 if (fread_unlocked (buf
, sizeof (buf
), l0
+ 1, stdin
) != 1
833 || memcmp (buf
, "BCDEFGHI\na", 10))
836 #if __USE_FORTIFY_LEVEL >= 1
838 if (fread_unlocked (buf
, 1, sizeof (buf
) + 1, stdin
) != sizeof (buf
) + 1)
843 if (fread_unlocked (buf
, sizeof (buf
) + 1, l0
+ 1, stdin
) != 1)
848 lseek (fileno (stdin
), 0, SEEK_SET
);
850 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
851 || memcmp (buf
, "abcdefgh\n", 9))
853 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
854 || memcmp (buf
, "ABCDEFGHI", 9))
857 lseek (fileno (stdin
), 0, SEEK_SET
);
859 if (read (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1) != sizeof (buf
) - 1
860 || memcmp (buf
, "abcdefgh\n", 9))
863 #if __USE_FORTIFY_LEVEL >= 1
865 if (read (fileno (stdin
), buf
, sizeof (buf
) + 1) != sizeof (buf
) + 1)
870 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
872 || memcmp (buf
, "\nABCDEFGH", 9))
874 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
875 || memcmp (buf
, "abcdefgh\n", 9))
877 if (pread (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
879 || memcmp (buf
, "h\nABCDEFG", 9))
882 #if __USE_FORTIFY_LEVEL >= 1
884 if (pread (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
890 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
892 || memcmp (buf
, "\nABCDEFGH", 9))
894 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
895 || memcmp (buf
, "abcdefgh\n", 9))
897 if (pread64 (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
899 || memcmp (buf
, "h\nABCDEFG", 9))
902 #if __USE_FORTIFY_LEVEL >= 1
904 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
910 if (freopen (temp_filename
, "r", stdin
) == NULL
)
912 puts ("could not open temporary file");
916 if (fseek (stdin
, 9 + 10 + 11, SEEK_SET
))
918 puts ("could not seek in test file");
922 #if __USE_FORTIFY_LEVEL >= 1
924 if (gets (buf
) != buf
)
929 /* Check whether missing N$ formats are detected. */
931 printf ("%3$d\n", 1, 2, 3, 4);
935 fprintf (stdout
, "%3$d\n", 1, 2, 3, 4);
939 sprintf (buf
, "%3$d\n", 1, 2, 3, 4);
943 snprintf (buf
, sizeof (buf
), "%3$d\n", 1, 2, 3, 4);
947 if (socketpair (PF_UNIX
, SOCK_STREAM
, 0, sp
))
951 const char *sendstr
= "abcdefgh\nABCDEFGH\n0123456789\n";
952 if ((size_t) send (sp
[0], sendstr
, strlen (sendstr
), 0)
957 if (recv (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
)
959 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
962 if (recv (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
)
963 != sizeof recvbuf
- 7
964 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
967 #if __USE_FORTIFY_LEVEL >= 1
969 if (recv (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
)
975 if (recv (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
)
976 != sizeof recvbuf
- 3)
982 struct sockaddr_un sa_un
;
985 if (recvfrom (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
,
986 (struct sockaddr
*) &sa_un
, &sl
)
988 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
992 if (recvfrom (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
,
993 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 7
994 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
997 #if __USE_FORTIFY_LEVEL >= 1
1000 if (recvfrom (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
,
1001 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
)
1006 sl
= sizeof (sa_un
);
1007 if (recvfrom (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
,
1008 (struct sockaddr
*) &sa_un
, &sl
) != sizeof recvbuf
- 3)
1017 char fname
[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1018 char *enddir
= strchr (fname
, '\0');
1019 if (mkdtemp (fname
) == NULL
)
1021 printf ("mkdtemp failed: %m\n");
1025 if (symlink ("bar", fname
) != 0)
1028 char readlinkbuf
[4];
1029 if (readlink (fname
, readlinkbuf
, 4) != 3
1030 || memcmp (readlinkbuf
, "bar", 3) != 0)
1032 if (readlink (fname
, readlinkbuf
+ 1, l0
+ 3) != 3
1033 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1036 #if __USE_FORTIFY_LEVEL >= 1
1038 if (readlink (fname
, readlinkbuf
+ 2, l0
+ 3) != 3)
1043 if (readlink (fname
, readlinkbuf
+ 3, 4) != 3)
1048 int tmpfd
= open ("/tmp", O_RDONLY
| O_DIRECTORY
);
1052 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
, 4) != 3
1053 || memcmp (readlinkbuf
, "bar", 3) != 0)
1055 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 1,
1057 || memcmp (readlinkbuf
, "bbar", 4) != 0)
1060 #if __USE_FORTIFY_LEVEL >= 1
1062 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 2,
1068 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 3,
1076 char *cwd1
= getcwd (NULL
, 0);
1080 char *cwd2
= getcwd (NULL
, 250);
1086 if (strcmp (cwd1
, cwd2
) != 0)
1093 char *cwd3
= getcwd (NULL
, 0);
1096 if (strcmp (fname
, cwd3
) != 0)
1097 printf ("getcwd after chdir is '%s' != '%s',"
1098 "get{c,}wd tests skipped\n", cwd3
, fname
);
1101 char getcwdbuf
[sizeof fname
- 3];
1103 char *cwd4
= getcwd (getcwdbuf
, sizeof getcwdbuf
);
1104 if (cwd4
!= getcwdbuf
1105 || strcmp (getcwdbuf
, fname
) != 0)
1108 cwd4
= getcwd (getcwdbuf
+ 1, l0
+ sizeof getcwdbuf
- 1);
1109 if (cwd4
!= getcwdbuf
+ 1
1110 || getcwdbuf
[0] != fname
[0]
1111 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1114 #if __USE_FORTIFY_LEVEL >= 1
1116 if (getcwd (getcwdbuf
+ 2, l0
+ sizeof getcwdbuf
)
1122 if (getcwd (getcwdbuf
+ 2, sizeof getcwdbuf
)
1128 if (getwd (getcwdbuf
) != getcwdbuf
1129 || strcmp (getcwdbuf
, fname
) != 0)
1132 if (getwd (getcwdbuf
+ 1) != getcwdbuf
+ 1
1133 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1136 #if __USE_FORTIFY_LEVEL >= 1
1138 if (getwd (getcwdbuf
+ 2) != getcwdbuf
+ 2)
1144 if (chdir (cwd1
) != 0)
1152 if (unlink (fname
) != 0)
1156 if (rmdir (fname
) != 0)
1161 char largebuf
[PATH_MAX
];
1162 char *realres
= realpath (".", largebuf
);
1163 if (realres
!= largebuf
)
1166 # if __USE_FORTIFY_LEVEL >= 1
1169 realres
= realpath (".", realbuf
);
1170 if (realres
!= realbuf
)
1176 if (setlocale (LC_ALL
, "de_DE.UTF-8") != NULL
)
1178 assert (MB_CUR_MAX
<= 10);
1180 /* First a simple test. */
1182 if (wctomb (enough
, L
'A') != 1)
1185 #if __USE_FORTIFY_LEVEL >= 1
1186 /* We know the wchar_t encoding is ISO 10646. So pick a
1187 character which has a multibyte representation which does not
1191 if (wctomb (smallbuf
, L
'\x100') != 2)
1197 memset (&s
, '\0', sizeof (s
));
1198 if (wcrtomb (enough
, L
'D', &s
) != 1 || enough
[0] != 'D')
1201 #if __USE_FORTIFY_LEVEL >= 1
1202 /* We know the wchar_t encoding is ISO 10646. So pick a
1203 character which has a multibyte representation which does not
1207 if (wcrtomb (smallbuf
, L
'\x100', &s
) != 2)
1212 wchar_t wenough
[10];
1213 memset (&s
, '\0', sizeof (s
));
1214 const char *cp
= "A";
1215 if (mbsrtowcs (wenough
, &cp
, 10, &s
) != 1
1216 || wcscmp (wenough
, L
"A") != 0)
1220 if (mbsrtowcs (wenough
, &cp
, l0
+ 10, &s
) != 2
1221 || wcscmp (wenough
, L
"BC") != 0)
1224 #if __USE_FORTIFY_LEVEL >= 1
1226 wchar_t wsmallbuf
[2];
1228 mbsrtowcs (wsmallbuf
, &cp
, 10, &s
);
1233 if (mbstowcs (wenough
, cp
, 10) != 1
1234 || wcscmp (wenough
, L
"A") != 0)
1238 if (mbstowcs (wenough
, cp
, l0
+ 10) != 3
1239 || wcscmp (wenough
, L
"DEF") != 0)
1242 #if __USE_FORTIFY_LEVEL >= 1
1244 wchar_t wsmallbuf
[2];
1246 mbstowcs (wsmallbuf
, cp
, 10);
1250 memset (&s
, '\0', sizeof (s
));
1252 wcscpy (wenough
, L
"DEF");
1253 if (mbsnrtowcs (wenough
, &cp
, 1, 10, &s
) != 1
1254 || wcscmp (wenough
, L
"AEF") != 0)
1258 if (mbsnrtowcs (wenough
, &cp
, 1, l0
+ 10, &s
) != 1
1259 || wcscmp (wenough
, L
"IEF") != 0)
1262 #if __USE_FORTIFY_LEVEL >= 1
1264 wchar_t wsmallbuf
[2];
1266 mbsnrtowcs (wsmallbuf
, &cp
, 3, 10, &s
);
1270 memset (&s
, '\0', sizeof (s
));
1271 const wchar_t *wcp
= L
"A";
1272 if (wcsrtombs (enough
, &wcp
, 10, &s
) != 1
1273 || strcmp (enough
, "A") != 0)
1277 if (wcsrtombs (enough
, &wcp
, l0
+ 10, &s
) != 2
1278 || strcmp (enough
, "BC") != 0)
1281 #if __USE_FORTIFY_LEVEL >= 1
1285 wcsrtombs (smallbuf
, &wcp
, 10, &s
);
1289 memset (enough
, 'Z', sizeof (enough
));
1291 if (wcstombs (enough
, wcp
, 10) != 2
1292 || strcmp (enough
, "EF") != 0)
1296 if (wcstombs (enough
, wcp
, l0
+ 10) != 1
1297 || strcmp (enough
, "G") != 0)
1300 #if __USE_FORTIFY_LEVEL >= 1
1304 wcstombs (smallbuf
, wcp
, 10);
1308 memset (&s
, '\0', sizeof (s
));
1310 if (wcsnrtombs (enough
, &wcp
, 1, 10, &s
) != 1
1311 || strcmp (enough
, "A") != 0)
1315 if (wcsnrtombs (enough
, &wcp
, 1, l0
+ 10, &s
) != 1
1316 || strcmp (enough
, "B") != 0)
1319 #if __USE_FORTIFY_LEVEL >= 1
1323 wcsnrtombs (smallbuf
, &wcp
, 3, 10, &s
);
1329 puts ("cannot set locale");
1333 fd
= posix_openpt (O_RDWR
);
1337 if (ptsname_r (fd
, enough
, sizeof (enough
)) != 0)
1340 #if __USE_FORTIFY_LEVEL >= 1
1343 if (ptsname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1351 confstr (_CS_GNU_LIBC_VERSION
, largebuf
, sizeof (largebuf
));
1352 # if __USE_FORTIFY_LEVEL >= 1
1355 confstr (_CS_GNU_LIBC_VERSION
, smallbuf
, sizeof (largebuf
));
1361 int ngr
= getgroups (5, grpslarge
);
1362 asm volatile ("" : : "r" (ngr
));
1363 #if __USE_FORTIFY_LEVEL >= 1
1366 ngr
= getgroups (5, (gid_t
*) smallbuf
);
1367 asm volatile ("" : : "r" (ngr
));
1371 fd
= open (_PATH_TTY
, O_RDONLY
);
1375 if (ttyname_r (fd
, enough
, sizeof (enough
)) != 0)
1378 #if __USE_FORTIFY_LEVEL >= 1
1381 if (ttyname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1388 char hostnamelarge
[1000];
1389 gethostname (hostnamelarge
, sizeof (hostnamelarge
));
1390 #if __USE_FORTIFY_LEVEL >= 1
1393 gethostname (smallbuf
, sizeof (hostnamelarge
));
1397 char loginlarge
[1000];
1398 getlogin_r (loginlarge
, sizeof (hostnamelarge
));
1399 #if __USE_FORTIFY_LEVEL >= 1
1402 getlogin_r (smallbuf
, sizeof (loginlarge
));
1406 char domainnamelarge
[1000];
1407 int res
= getdomainname (domainnamelarge
, sizeof (domainnamelarge
));
1408 asm volatile ("" : : "r" (res
));
1409 #if __USE_FORTIFY_LEVEL >= 1
1412 res
= getdomainname (smallbuf
, sizeof (domainnamelarge
));
1413 asm volatile ("" : : "r" (res
));