1 /* Copyright (C) 2004, 2005, 2006 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. */
34 #include <sys/socket.h>
38 static void do_prepare (void);
39 static int do_test (void);
40 #define PREPARE(argc, argv) do_prepare ()
41 #define TEST_FUNCTION do_test ()
42 #include "../test-skeleton.c"
47 int temp_fd
= create_temp_file ("tst-chk1.", &temp_filename
);
50 printf ("cannot create temporary file: %m\n");
54 const char *strs
= "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
55 if (write (temp_fd
, strs
, strlen (strs
)) != strlen (strs
))
57 puts ("could not write test strings into file");
58 unlink (temp_filename
);
63 volatile int chk_fail_ok
;
73 longjmp (chk_fail_buf
, 1);
84 const char *str1
= "JIHGFEDCBA";
85 const char *str2
= "F";
86 const char *str3
= "%s%n%s%n";
87 const char *str4
= "Hello, ";
88 const char *str5
= "World!\n";
89 const wchar_t *wstr1
= L
"JIHGFEDCBA";
90 const wchar_t *wstr2
= L
"F";
91 const wchar_t *wstr3
= L
"%s%n%s%n";
92 const wchar_t *wstr4
= L
"Hello, ";
93 const wchar_t *wstr5
= L
"World!\n";
99 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
100 #define CHK_FAIL_START \
102 if (! setjmp (chk_fail_buf)) \
104 #define CHK_FAIL_END \
108 #if __USE_FORTIFY_LEVEL >= 2
109 #define CHK_FAIL2_START CHK_FAIL_START
110 #define CHK_FAIL2_END CHK_FAIL_END
112 #define CHK_FAIL2_START
113 #define CHK_FAIL2_END
120 sa
.sa_handler
= handler
;
122 sigemptyset (&sa
.sa_mask
);
124 sigaction (SIGABRT
, &sa
, NULL
);
126 /* Avoid all the buffer overflow messages on stderr. */
127 int fd
= open (_PATH_DEVNULL
, O_WRONLY
);
129 close (STDERR_FILENO
);
132 dup2 (fd
, STDERR_FILENO
);
135 setenv ("LIBC_FATAL_STDERR_", "1", 1);
137 struct A
{ char buf1
[9]; char buf2
[1]; } a
;
138 struct wA
{ wchar_t buf1
[9]; wchar_t buf2
[1]; } wa
;
140 printf ("Test checking routines at fortify level %d\n",
141 #ifdef __USE_FORTIFY_LEVEL
142 (int) __USE_FORTIFY_LEVEL
148 /* These ops can be done without runtime checking of object size. */
149 memcpy (buf
, "abcdefghij", 10);
150 memmove (buf
+ 1, buf
, 9);
151 if (memcmp (buf
, "aabcdefghi", 10))
154 if (mempcpy (buf
+ 5, "abcde", 5) != buf
+ 10
155 || memcmp (buf
, "aabcdabcde", 10))
158 memset (buf
+ 8, 'j', 2);
159 if (memcmp (buf
, "aabcdabcjj", 10))
162 strcpy (buf
+ 4, "EDCBA");
163 if (memcmp (buf
, "aabcEDCBA", 10))
166 if (stpcpy (buf
+ 8, "F") != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
169 strncpy (buf
+ 6, "X", 4);
170 if (memcmp (buf
, "aabcEDX\0\0", 10))
173 if (sprintf (buf
+ 7, "%s", "67") != 2 || memcmp (buf
, "aabcEDX67", 10))
176 if (snprintf (buf
+ 7, 3, "%s", "987654") != 6
177 || memcmp (buf
, "aabcEDX98", 10))
180 /* These ops need runtime checking, but shouldn't __chk_fail. */
181 memcpy (buf
, "abcdefghij", l0
+ 10);
182 memmove (buf
+ 1, buf
, l0
+ 9);
183 if (memcmp (buf
, "aabcdefghi", 10))
186 if (mempcpy (buf
+ 5, "abcde", l0
+ 5) != buf
+ 10
187 || memcmp (buf
, "aabcdabcde", 10))
190 memset (buf
+ 8, 'j', l0
+ 2);
191 if (memcmp (buf
, "aabcdabcjj", 10))
194 strcpy (buf
+ 4, str1
+ 5);
195 if (memcmp (buf
, "aabcEDCBA", 10))
198 if (stpcpy (buf
+ 8, str2
) != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
201 strncpy (buf
+ 6, "X", l0
+ 4);
202 if (memcmp (buf
, "aabcEDX\0\0", 10))
205 if (stpncpy (buf
+ 5, "cd", l0
+ 5) != buf
+ 7
206 || memcmp (buf
, "aabcEcd\0\0", 10))
209 if (sprintf (buf
+ 7, "%d", num1
) != 2 || memcmp (buf
, "aabcEcd67", 10))
212 if (snprintf (buf
+ 7, 3, "%d", num2
) != 6 || memcmp (buf
, "aabcEcd98", 10))
217 if (memcmp (buf
, "aabcEcd9A", 10))
221 strncat (buf
, "ZYXWV", l0
+ 2);
222 if (memcmp (buf
, "aabcEcdZY", 10))
225 memcpy (a
.buf1
, "abcdefghij", l0
+ 10);
226 memmove (a
.buf1
+ 1, a
.buf1
, l0
+ 9);
227 if (memcmp (a
.buf1
, "aabcdefghi", 10))
230 if (mempcpy (a
.buf1
+ 5, "abcde", l0
+ 5) != a
.buf1
+ 10
231 || memcmp (a
.buf1
, "aabcdabcde", 10))
234 memset (a
.buf1
+ 8, 'j', l0
+ 2);
235 if (memcmp (a
.buf1
, "aabcdabcjj", 10))
238 #if __USE_FORTIFY_LEVEL < 2 || !__GNUC_PREREQ (4, 0)
239 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
240 and sufficient GCC support, as the string operations overflow
241 from a.buf1 into a.buf2. */
242 strcpy (a
.buf1
+ 4, str1
+ 5);
243 if (memcmp (a
.buf1
, "aabcEDCBA", 10))
246 if (stpcpy (a
.buf1
+ 8, str2
) != a
.buf1
+ 9
247 || memcmp (a
.buf1
, "aabcEDCBF", 10))
250 strncpy (a
.buf1
+ 6, "X", l0
+ 4);
251 if (memcmp (a
.buf1
, "aabcEDX\0\0", 10))
254 if (sprintf (a
.buf1
+ 7, "%d", num1
) != 2
255 || memcmp (a
.buf1
, "aabcEDX67", 10))
258 if (snprintf (a
.buf1
+ 7, 3, "%d", num2
) != 6
259 || memcmp (a
.buf1
, "aabcEDX98", 10))
262 a
.buf1
[l0
+ 8] = '\0';
263 strcat (a
.buf1
, "A");
264 if (memcmp (a
.buf1
, "aabcEDX9A", 10))
267 a
.buf1
[l0
+ 7] = '\0';
268 strncat (a
.buf1
, "ZYXWV", l0
+ 2);
269 if (memcmp (a
.buf1
, "aabcEDXZY", 10))
274 #if __USE_FORTIFY_LEVEL >= 1
275 /* Now check if all buffer overflows are caught at runtime. */
278 memcpy (buf
+ 1, "abcdefghij", l0
+ 10);
282 memmove (buf
+ 2, buf
+ 1, l0
+ 9);
286 p
= mempcpy (buf
+ 6, "abcde", l0
+ 5);
290 memset (buf
+ 9, 'j', l0
+ 2);
294 strcpy (buf
+ 5, str1
+ 5);
298 p
= stpcpy (buf
+ 9, str2
);
302 strncpy (buf
+ 7, "X", l0
+ 4);
306 stpncpy (buf
+ 6, "cd", l0
+ 5);
310 sprintf (buf
+ 8, "%d", num1
);
314 snprintf (buf
+ 8, l0
+ 3, "%d", num2
);
317 memcpy (buf
, str1
+ 2, l0
+ 9);
322 memcpy (buf
, str1
+ 3, l0
+ 8);
324 strncat (buf
, "ZYXWV", l0
+ 3);
328 memcpy (a
.buf1
+ 1, "abcdefghij", l0
+ 10);
332 memmove (a
.buf1
+ 2, a
.buf1
+ 1, l0
+ 9);
336 p
= mempcpy (a
.buf1
+ 6, "abcde", l0
+ 5);
340 memset (a
.buf1
+ 9, 'j', l0
+ 2);
343 #if __USE_FORTIFY_LEVEL >= 2 && __GNUC_PREREQ (4, 0)
350 strcpy (a
.buf1
+ (O
+ 4), str1
+ 5);
354 p
= stpcpy (a
.buf1
+ (O
+ 8), str2
);
358 strncpy (a
.buf1
+ (O
+ 6), "X", l0
+ 4);
362 sprintf (a
.buf1
+ (O
+ 7), "%d", num1
);
366 snprintf (a
.buf1
+ (O
+ 7), l0
+ 3, "%d", num2
);
369 memcpy (a
.buf1
, str1
+ (3 - O
), l0
+ 8 + O
);
371 strcat (a
.buf1
, "AB");
374 memcpy (a
.buf1
, str1
+ (4 - O
), l0
+ 7 + O
);
376 strncat (a
.buf1
, "ZYXWV", l0
+ 3);
381 /* These ops can be done without runtime checking of object size. */
382 wmemcpy (wbuf
, L
"abcdefghij", 10);
383 wmemmove (wbuf
+ 1, wbuf
, 9);
384 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
387 if (wmempcpy (wbuf
+ 5, L
"abcde", 5) != wbuf
+ 10
388 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
391 wmemset (wbuf
+ 8, L
'j', 2);
392 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
395 wcscpy (wbuf
+ 4, L
"EDCBA");
396 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
399 if (wcpcpy (wbuf
+ 8, L
"F") != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
402 wcsncpy (wbuf
+ 6, L
"X", 4);
403 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
406 if (swprintf (wbuf
+ 7, 3, L
"%ls", L
"987654") >= 0
407 || wmemcmp (wbuf
, L
"aabcEDX98", 10))
410 if (swprintf (wbuf
+ 7, 3, L
"64") != 2
411 || wmemcmp (wbuf
, L
"aabcEDX64", 10))
414 /* These ops need runtime checking, but shouldn't __chk_fail. */
415 wmemcpy (wbuf
, L
"abcdefghij", l0
+ 10);
416 wmemmove (wbuf
+ 1, wbuf
, l0
+ 9);
417 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
420 if (wmempcpy (wbuf
+ 5, L
"abcde", l0
+ 5) != wbuf
+ 10
421 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
424 wmemset (wbuf
+ 8, L
'j', l0
+ 2);
425 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
428 wcscpy (wbuf
+ 4, wstr1
+ 5);
429 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
432 if (wcpcpy (wbuf
+ 8, wstr2
) != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
435 wcsncpy (wbuf
+ 6, L
"X", l0
+ 4);
436 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
439 if (wcpncpy (wbuf
+ 5, L
"cd", l0
+ 5) != wbuf
+ 7
440 || wmemcmp (wbuf
, L
"aabcEcd\0\0", 10))
443 if (swprintf (wbuf
+ 7, 3, L
"%d", num2
) >= 0
444 || wmemcmp (wbuf
, L
"aabcEcd98", 10))
447 wbuf
[l0
+ 8] = L
'\0';
449 if (wmemcmp (wbuf
, L
"aabcEcd9A", 10))
452 wbuf
[l0
+ 7] = L
'\0';
453 wcsncat (wbuf
, L
"ZYXWV", l0
+ 2);
454 if (wmemcmp (wbuf
, L
"aabcEcdZY", 10))
457 wmemcpy (wa
.buf1
, L
"abcdefghij", l0
+ 10);
458 wmemmove (wa
.buf1
+ 1, wa
.buf1
, l0
+ 9);
459 if (wmemcmp (wa
.buf1
, L
"aabcdefghi", 10))
462 if (wmempcpy (wa
.buf1
+ 5, L
"abcde", l0
+ 5) != wa
.buf1
+ 10
463 || wmemcmp (wa
.buf1
, L
"aabcdabcde", 10))
466 wmemset (wa
.buf1
+ 8, L
'j', l0
+ 2);
467 if (wmemcmp (wa
.buf1
, L
"aabcdabcjj", 10))
470 #if __USE_FORTIFY_LEVEL < 2
471 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
472 and sufficient GCC support, as the string operations overflow
473 from a.buf1 into a.buf2. */
474 wcscpy (wa
.buf1
+ 4, wstr1
+ 5);
475 if (wmemcmp (wa
.buf1
, L
"aabcEDCBA", 10))
478 if (wcpcpy (wa
.buf1
+ 8, wstr2
) != wa
.buf1
+ 9
479 || wmemcmp (wa
.buf1
, L
"aabcEDCBF", 10))
482 wcsncpy (wa
.buf1
+ 6, L
"X", l0
+ 4);
483 if (wmemcmp (wa
.buf1
, L
"aabcEDX\0\0", 10))
486 if (swprintf (wa
.buf1
+ 7, 3, L
"%d", num2
) >= 0
487 || wmemcmp (wa
.buf1
, L
"aabcEDX98", 10))
490 wa
.buf1
[l0
+ 8] = L
'\0';
491 wcscat (wa
.buf1
, L
"A");
492 if (wmemcmp (wa
.buf1
, L
"aabcEDX9A", 10))
495 wa
.buf1
[l0
+ 7] = L
'\0';
496 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 2);
497 if (wmemcmp (wa
.buf1
, L
"aabcEDXZY", 10))
502 #if __USE_FORTIFY_LEVEL >= 1
503 /* Now check if all buffer overflows are caught at runtime. */
506 wmemcpy (wbuf
+ 1, L
"abcdefghij", l0
+ 10);
510 wmemmove (wbuf
+ 2, wbuf
+ 1, l0
+ 9);
514 wp
= wmempcpy (wbuf
+ 6, L
"abcde", l0
+ 5);
518 wmemset (wbuf
+ 9, L
'j', l0
+ 2);
522 wcscpy (wbuf
+ 5, wstr1
+ 5);
526 wp
= wcpcpy (wbuf
+ 9, wstr2
);
530 wcsncpy (wbuf
+ 7, L
"X", l0
+ 4);
534 wcpncpy (wbuf
+ 6, L
"cd", l0
+ 5);
537 wmemcpy (wbuf
, wstr1
+ 2, l0
+ 9);
539 wcscat (wbuf
, L
"AB");
542 wmemcpy (wbuf
, wstr1
+ 3, l0
+ 8);
544 wcsncat (wbuf
, L
"ZYXWV", l0
+ 3);
548 wmemcpy (wa
.buf1
+ 1, L
"abcdefghij", l0
+ 10);
552 wmemmove (wa
.buf1
+ 2, wa
.buf1
+ 1, l0
+ 9);
556 wp
= wmempcpy (wa
.buf1
+ 6, L
"abcde", l0
+ 5);
560 wmemset (wa
.buf1
+ 9, L
'j', l0
+ 2);
563 #if __USE_FORTIFY_LEVEL >= 2
570 wcscpy (wa
.buf1
+ (O
+ 4), wstr1
+ 5);
574 wp
= wcpcpy (wa
.buf1
+ (O
+ 8), wstr2
);
578 wcsncpy (wa
.buf1
+ (O
+ 6), L
"X", l0
+ 4);
581 wmemcpy (wa
.buf1
, wstr1
+ (3 - O
), l0
+ 8 + O
);
583 wcscat (wa
.buf1
, L
"AB");
586 wmemcpy (wa
.buf1
, wstr1
+ (4 - O
), l0
+ 7 + O
);
588 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 3);
593 /* Now checks for %n protection. */
595 /* Constant literals passed directly are always ok
596 (even with warnings about possible bugs from GCC). */
598 if (sprintf (buf
, "%s%n%s%n", str2
, &n1
, str2
, &n2
) != 2
599 || n1
!= 1 || n2
!= 2)
602 /* In this case the format string is not known at compile time,
603 but resides in read-only memory, so is ok. */
604 if (snprintf (buf
, 4, str3
, str2
, &n1
, str2
, &n2
) != 2
605 || n1
!= 1 || n2
!= 2)
608 strcpy (buf2
+ 2, "%n%s%n");
609 /* When the format string is writable and contains %n,
610 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
612 if (sprintf (buf
, buf2
, str2
, &n1
, str2
, &n1
) != 2)
617 if (snprintf (buf
, 3, buf2
, str2
, &n1
, str2
, &n1
) != 2)
621 /* But if there is no %n, even writable format string
624 if (sprintf (buf
, buf2
+ 4, str2
) != 1)
627 /* Constant literals passed directly are always ok
628 (even with warnings about possible bugs from GCC). */
629 if (printf ("%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
630 || n1
!= 7 || n2
!= 14)
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 (printf (str3
, str4
, &n1
, str5
, &n2
) != 14
636 || n1
!= 7 || n2
!= 14)
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 (printf (buf2
, str4
, &n1
, str5
, &n1
) != 14)
647 /* But if there is no %n, even writable format string
650 if (printf (buf2
+ 4, str5
) != 7)
655 /* Constant literals passed directly are always ok
656 (even with warnings about possible bugs from GCC). */
657 if (fprintf (fp
, "%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
658 || n1
!= 7 || n2
!= 14)
661 /* In this case the format string is not known at compile time,
662 but resides in read-only memory, so is ok. */
663 if (fprintf (fp
, str3
, str4
, &n1
, str5
, &n2
) != 14
664 || n1
!= 7 || n2
!= 14)
667 strcpy (buf2
+ 2, "%n%s%n");
668 /* When the format string is writable and contains %n,
669 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
671 if (fprintf (fp
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
675 /* But if there is no %n, even writable format string
678 if (fprintf (fp
, buf2
+ 4, str5
) != 7)
681 if (freopen (temp_filename
, "r", stdin
) == NULL
)
683 puts ("could not open temporary file");
687 if (gets (buf
) != buf
|| memcmp (buf
, "abcdefgh", 9))
689 if (gets (buf
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
692 #if __USE_FORTIFY_LEVEL >= 1
694 if (gets (buf
) != buf
)
701 if (fgets (buf
, sizeof (buf
), stdin
) != buf
702 || memcmp (buf
, "abcdefgh\n", 10))
704 if (fgets (buf
, sizeof (buf
), stdin
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
709 if (fgets (buf
, l0
+ sizeof (buf
), stdin
) != buf
710 || memcmp (buf
, "abcdefgh\n", 10))
713 #if __USE_FORTIFY_LEVEL >= 1
715 if (fgets (buf
, sizeof (buf
) + 1, stdin
) != buf
)
720 if (fgets (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
727 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
728 || memcmp (buf
, "abcdefgh\n", 10))
730 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
731 || memcmp (buf
, "ABCDEFGHI", 10))
736 if (fgets_unlocked (buf
, l0
+ sizeof (buf
), stdin
) != buf
737 || memcmp (buf
, "abcdefgh\n", 10))
740 #if __USE_FORTIFY_LEVEL >= 1
742 if (fgets_unlocked (buf
, sizeof (buf
) + 1, stdin
) != buf
)
747 if (fgets_unlocked (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
752 lseek (fileno (stdin
), 0, SEEK_SET
);
754 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
755 || memcmp (buf
, "abcdefgh\n", 9))
757 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
758 || memcmp (buf
, "ABCDEFGHI", 9))
761 lseek (fileno (stdin
), 0, SEEK_SET
);
763 if (read (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1) != sizeof (buf
) - 1
764 || memcmp (buf
, "abcdefgh\n", 9))
767 #if __USE_FORTIFY_LEVEL >= 1
769 if (read (fileno (stdin
), buf
, sizeof (buf
) + 1) != sizeof (buf
) + 1)
774 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
776 || memcmp (buf
, "\nABCDEFGH", 9))
778 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
779 || memcmp (buf
, "abcdefgh\n", 9))
781 if (pread (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
783 || memcmp (buf
, "h\nABCDEFG", 9))
786 #if __USE_FORTIFY_LEVEL >= 1
788 if (pread (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
794 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
796 || memcmp (buf
, "\nABCDEFGH", 9))
798 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
799 || memcmp (buf
, "abcdefgh\n", 9))
801 if (pread64 (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
803 || memcmp (buf
, "h\nABCDEFG", 9))
806 #if __USE_FORTIFY_LEVEL >= 1
808 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
814 if (freopen (temp_filename
, "r", stdin
) == NULL
)
816 puts ("could not open temporary file");
820 if (fseek (stdin
, 9 + 10 + 11, SEEK_SET
))
822 puts ("could not seek in test file");
826 #if __USE_FORTIFY_LEVEL >= 1
828 if (gets (buf
) != buf
)
833 /* Check whether missing N$ formats are detected. */
835 printf ("%3$d\n", 1, 2, 3, 4);
839 fprintf (stdout
, "%3$d\n", 1, 2, 3, 4);
843 sprintf (buf
, "%3$d\n", 1, 2, 3, 4);
847 snprintf (buf
, sizeof (buf
), "%3$d\n", 1, 2, 3, 4);
851 if (socketpair (PF_UNIX
, SOCK_STREAM
, 0, sp
))
855 const char *sendstr
= "abcdefgh\nABCDEFGH\n0123456789\n";
856 if (send (sp
[0], sendstr
, strlen (sendstr
), 0) != strlen (sendstr
))
860 if (recv (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
)
862 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
865 if (recv (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
)
866 != sizeof recvbuf
- 7
867 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
870 #if __USE_FORTIFY_LEVEL >= 1
872 if (recv (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
)
878 if (recv (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
)
879 != sizeof recvbuf
- 3)
885 struct sockaddr_un sa_un
;
888 if (recvfrom (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
, &sa_un
, &sl
)
890 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
894 if (recvfrom (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
,
895 &sa_un
, &sl
) != sizeof recvbuf
- 7
896 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
899 #if __USE_FORTIFY_LEVEL >= 1
902 if (recvfrom (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
, &sa_un
, &sl
)
909 if (recvfrom (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
,
910 &sa_un
, &sl
) != sizeof recvbuf
- 3)
919 char fname
[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
920 char *enddir
= strchr (fname
, '\0');
921 if (mkdtemp (fname
) == NULL
)
923 printf ("mkdtemp failed: %m\n");
927 if (symlink ("bar", fname
) != 0)
931 if (readlink (fname
, readlinkbuf
, 4) != 3
932 || memcmp (readlinkbuf
, "bar", 3) != 0)
934 if (readlink (fname
, readlinkbuf
+ 1, l0
+ 3) != 3
935 || memcmp (readlinkbuf
, "bbar", 4) != 0)
938 #if __USE_FORTIFY_LEVEL >= 1
940 if (readlink (fname
, readlinkbuf
+ 2, l0
+ 3) != 3)
945 if (readlink (fname
, readlinkbuf
+ 3, 4) != 3)
950 int tmpfd
= open ("/tmp", O_RDONLY
| O_DIRECTORY
);
954 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
, 4) != 3
955 || memcmp (readlinkbuf
, "bar", 3) != 0)
957 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 1,
959 || memcmp (readlinkbuf
, "bbar", 4) != 0)
962 #if __USE_FORTIFY_LEVEL >= 1
964 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 2,
970 if (readlinkat (tmpfd
, fname
+ sizeof ("/tmp/") - 1, readlinkbuf
+ 3,
978 char *cwd1
= getcwd (NULL
, 0);
982 char *cwd2
= getcwd (NULL
, 250);
988 if (strcmp (cwd1
, cwd2
) != 0)
995 char *cwd3
= getcwd (NULL
, 0);
998 if (strcmp (fname
, cwd3
) != 0)
999 printf ("getcwd after chdir is '%s' != '%s',"
1000 "get{c,}wd tests skipped\n", cwd3
, fname
);
1003 char getcwdbuf
[sizeof fname
- 3];
1005 char *cwd4
= getcwd (getcwdbuf
, sizeof getcwdbuf
);
1006 if (cwd4
!= getcwdbuf
1007 || strcmp (getcwdbuf
, fname
) != 0)
1010 cwd4
= getcwd (getcwdbuf
+ 1, l0
+ sizeof getcwdbuf
- 1);
1011 if (cwd4
!= getcwdbuf
+ 1
1012 || getcwdbuf
[0] != fname
[0]
1013 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1016 #if __USE_FORTIFY_LEVEL >= 1
1018 if (getcwd (getcwdbuf
+ 2, l0
+ sizeof getcwdbuf
)
1024 if (getcwd (getcwdbuf
+ 2, sizeof getcwdbuf
)
1030 if (getwd (getcwdbuf
) != getcwdbuf
1031 || strcmp (getcwdbuf
, fname
) != 0)
1034 if (getwd (getcwdbuf
+ 1) != getcwdbuf
+ 1
1035 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1038 #if __USE_FORTIFY_LEVEL >= 1
1040 if (getwd (getcwdbuf
+ 2) != getcwdbuf
+ 2)
1046 if (chdir (cwd1
) != 0)
1054 if (unlink (fname
) != 0)
1058 if (rmdir (fname
) != 0)
1063 char largebuf
[PATH_MAX
];
1064 char *realres
= realpath (".", largebuf
);
1065 if (realres
!= largebuf
)
1068 # if __USE_FORTIFY_LEVEL >= 1
1071 realres
= realpath (".", realbuf
);
1072 if (realres
!= realbuf
)
1078 if (setlocale (LC_ALL
, "de_DE.UTF-8") != NULL
)
1080 assert (MB_CUR_MAX
<= 10);
1082 /* First a simple test. */
1084 if (wctomb (enough
, L
'A') != 1)
1087 #if __USE_FORTIFY_LEVEL >= 1
1088 /* We know the wchar_t encoding is ISO 10646. So pick a
1089 character which has a multibyte representation which does not
1093 if (wctomb (smallbuf
, L
'\x100') != 2)
1099 memset (&s
, '\0', sizeof (s
));
1100 if (wcrtomb (enough
, L
'D', &s
) != 1 || enough
[0] != 'D')
1103 #if __USE_FORTIFY_LEVEL >= 1
1104 /* We know the wchar_t encoding is ISO 10646. So pick a
1105 character which has a multibyte representation which does not
1109 if (wcrtomb (smallbuf
, L
'\x100', &s
) != 2)
1114 wchar_t wenough
[10];
1115 memset (&s
, '\0', sizeof (s
));
1116 const char *cp
= "A";
1117 if (mbsrtowcs (wenough
, &cp
, 10, &s
) != 1
1118 || wcscmp (wenough
, L
"A") != 0)
1122 if (mbsrtowcs (wenough
, &cp
, l0
+ 10, &s
) != 2
1123 || wcscmp (wenough
, L
"BC") != 0)
1126 #if __USE_FORTIFY_LEVEL >= 1
1128 wchar_t wsmallbuf
[2];
1130 mbsrtowcs (wsmallbuf
, &cp
, 10, &s
);
1135 if (mbstowcs (wenough
, cp
, 10) != 1
1136 || wcscmp (wenough
, L
"A") != 0)
1140 if (mbstowcs (wenough
, cp
, l0
+ 10) != 3
1141 || wcscmp (wenough
, L
"DEF") != 0)
1144 #if __USE_FORTIFY_LEVEL >= 1
1146 wchar_t wsmallbuf
[2];
1148 mbstowcs (wsmallbuf
, cp
, 10);
1152 memset (&s
, '\0', sizeof (s
));
1154 wcscpy (wenough
, L
"DEF");
1155 if (mbsnrtowcs (wenough
, &cp
, 1, 10, &s
) != 1
1156 || wcscmp (wenough
, L
"AEF") != 0)
1160 if (mbsnrtowcs (wenough
, &cp
, 1, l0
+ 10, &s
) != 1
1161 || wcscmp (wenough
, L
"IEF") != 0)
1164 #if __USE_FORTIFY_LEVEL >= 1
1166 wchar_t wsmallbuf
[2];
1168 mbsnrtowcs (wsmallbuf
, &cp
, 3, 10, &s
);
1172 memset (&s
, '\0', sizeof (s
));
1173 const wchar_t *wcp
= L
"A";
1174 if (wcsrtombs (enough
, &wcp
, 10, &s
) != 1
1175 || strcmp (enough
, "A") != 0)
1179 if (wcsrtombs (enough
, &wcp
, l0
+ 10, &s
) != 2
1180 || strcmp (enough
, "BC") != 0)
1183 #if __USE_FORTIFY_LEVEL >= 1
1187 wcsrtombs (smallbuf
, &wcp
, 10, &s
);
1191 memset (enough
, 'Z', sizeof (enough
));
1193 if (wcstombs (enough
, wcp
, 10) != 2
1194 || strcmp (enough
, "EF") != 0)
1198 if (wcstombs (enough
, wcp
, l0
+ 10) != 1
1199 || strcmp (enough
, "G") != 0)
1202 #if __USE_FORTIFY_LEVEL >= 1
1206 wcstombs (smallbuf
, wcp
, 10);
1210 memset (&s
, '\0', sizeof (s
));
1212 if (wcsnrtombs (enough
, &wcp
, 1, 10, &s
) != 1
1213 || strcmp (enough
, "A") != 0)
1217 if (wcsnrtombs (enough
, &wcp
, 1, l0
+ 10, &s
) != 1
1218 || strcmp (enough
, "B") != 0)
1221 #if __USE_FORTIFY_LEVEL >= 1
1225 wcsnrtombs (smallbuf
, &wcp
, 3, 10, &s
);
1231 puts ("cannot set locale");
1235 fd
= posix_openpt (O_RDWR
);
1239 if (ptsname_r (fd
, enough
, sizeof (enough
)) != 0)
1242 #if __USE_FORTIFY_LEVEL >= 1
1245 if (ptsname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1253 confstr (_CS_GNU_LIBC_VERSION
, largebuf
, sizeof (largebuf
));
1254 # if __USE_FORTIFY_LEVEL >= 1
1257 confstr (_CS_GNU_LIBC_VERSION
, smallbuf
, sizeof (largebuf
));
1263 int ngr
= getgroups (5, grpslarge
);
1264 asm volatile ("" : : "r" (ngr
));
1265 #if __USE_FORTIFY_LEVEL >= 1
1268 ngr
= getgroups (5, (gid_t
*) smallbuf
);
1269 asm volatile ("" : : "r" (ngr
));
1273 fd
= open (_PATH_TTY
, O_RDONLY
);
1277 if (ttyname_r (fd
, enough
, sizeof (enough
)) != 0)
1280 #if __USE_FORTIFY_LEVEL >= 1
1283 if (ttyname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1290 char hostnamelarge
[1000];
1291 gethostname (hostnamelarge
, sizeof (hostnamelarge
));
1292 #if __USE_FORTIFY_LEVEL >= 1
1295 gethostname (smallbuf
, sizeof (hostnamelarge
));
1299 char loginlarge
[1000];
1300 getlogin_r (loginlarge
, sizeof (hostnamelarge
));
1301 #if __USE_FORTIFY_LEVEL >= 1
1304 getlogin_r (smallbuf
, sizeof (loginlarge
));
1308 char domainnamelarge
[1000];
1309 int res
= getdomainname (domainnamelarge
, sizeof (domainnamelarge
));
1310 asm volatile ("" : : "r" (res
));
1311 #if __USE_FORTIFY_LEVEL >= 1
1314 res
= getdomainname (smallbuf
, sizeof (domainnamelarge
));
1315 asm volatile ("" : : "r" (res
));