1 /* Copyright (C) 2004, 2005 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 (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
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 /* These ops can be done without runtime checking of object size. */
146 memcpy (buf
, "abcdefghij", 10);
147 memmove (buf
+ 1, buf
, 9);
148 if (memcmp (buf
, "aabcdefghi", 10))
151 if (mempcpy (buf
+ 5, "abcde", 5) != buf
+ 10
152 || memcmp (buf
, "aabcdabcde", 10))
155 memset (buf
+ 8, 'j', 2);
156 if (memcmp (buf
, "aabcdabcjj", 10))
159 strcpy (buf
+ 4, "EDCBA");
160 if (memcmp (buf
, "aabcEDCBA", 10))
163 if (stpcpy (buf
+ 8, "F") != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
166 strncpy (buf
+ 6, "X", 4);
167 if (memcmp (buf
, "aabcEDX\0\0", 10))
170 if (sprintf (buf
+ 7, "%s", "67") != 2 || memcmp (buf
, "aabcEDX67", 10))
173 if (snprintf (buf
+ 7, 3, "%s", "987654") != 6
174 || memcmp (buf
, "aabcEDX98", 10))
177 /* These ops need runtime checking, but shouldn't __chk_fail. */
178 memcpy (buf
, "abcdefghij", l0
+ 10);
179 memmove (buf
+ 1, buf
, l0
+ 9);
180 if (memcmp (buf
, "aabcdefghi", 10))
183 if (mempcpy (buf
+ 5, "abcde", l0
+ 5) != buf
+ 10
184 || memcmp (buf
, "aabcdabcde", 10))
187 memset (buf
+ 8, 'j', l0
+ 2);
188 if (memcmp (buf
, "aabcdabcjj", 10))
191 strcpy (buf
+ 4, str1
+ 5);
192 if (memcmp (buf
, "aabcEDCBA", 10))
195 if (stpcpy (buf
+ 8, str2
) != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
198 strncpy (buf
+ 6, "X", l0
+ 4);
199 if (memcmp (buf
, "aabcEDX\0\0", 10))
202 if (stpncpy (buf
+ 5, "cd", l0
+ 5) != buf
+ 7
203 || memcmp (buf
, "aabcEcd\0\0", 10))
206 if (sprintf (buf
+ 7, "%d", num1
) != 2 || memcmp (buf
, "aabcEcd67", 10))
209 if (snprintf (buf
+ 7, 3, "%d", num2
) != 6 || memcmp (buf
, "aabcEcd98", 10))
214 if (memcmp (buf
, "aabcEcd9A", 10))
218 strncat (buf
, "ZYXWV", l0
+ 2);
219 if (memcmp (buf
, "aabcEcdZY", 10))
222 memcpy (a
.buf1
, "abcdefghij", l0
+ 10);
223 memmove (a
.buf1
+ 1, a
.buf1
, l0
+ 9);
224 if (memcmp (a
.buf1
, "aabcdefghi", 10))
227 if (mempcpy (a
.buf1
+ 5, "abcde", l0
+ 5) != a
.buf1
+ 10
228 || memcmp (a
.buf1
, "aabcdabcde", 10))
231 memset (a
.buf1
+ 8, 'j', l0
+ 2);
232 if (memcmp (a
.buf1
, "aabcdabcjj", 10))
235 #if __USE_FORTIFY_LEVEL < 2
236 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
237 and sufficient GCC support, as the string operations overflow
238 from a.buf1 into a.buf2. */
239 strcpy (a
.buf1
+ 4, str1
+ 5);
240 if (memcmp (a
.buf1
, "aabcEDCBA", 10))
243 if (stpcpy (a
.buf1
+ 8, str2
) != a
.buf1
+ 9
244 || memcmp (a
.buf1
, "aabcEDCBF", 10))
247 strncpy (a
.buf1
+ 6, "X", l0
+ 4);
248 if (memcmp (a
.buf1
, "aabcEDX\0\0", 10))
251 if (sprintf (a
.buf1
+ 7, "%d", num1
) != 2
252 || memcmp (a
.buf1
, "aabcEDX67", 10))
255 if (snprintf (a
.buf1
+ 7, 3, "%d", num2
) != 6
256 || memcmp (a
.buf1
, "aabcEDX98", 10))
259 a
.buf1
[l0
+ 8] = '\0';
260 strcat (a
.buf1
, "A");
261 if (memcmp (a
.buf1
, "aabcEDX9A", 10))
264 a
.buf1
[l0
+ 7] = '\0';
265 strncat (a
.buf1
, "ZYXWV", l0
+ 2);
266 if (memcmp (a
.buf1
, "aabcEDXZY", 10))
271 #if __USE_FORTIFY_LEVEL >= 1
272 /* Now check if all buffer overflows are caught at runtime. */
275 memcpy (buf
+ 1, "abcdefghij", l0
+ 10);
279 memmove (buf
+ 2, buf
+ 1, l0
+ 9);
283 p
= mempcpy (buf
+ 6, "abcde", l0
+ 5);
287 memset (buf
+ 9, 'j', l0
+ 2);
291 strcpy (buf
+ 5, str1
+ 5);
295 p
= stpcpy (buf
+ 9, str2
);
299 strncpy (buf
+ 7, "X", l0
+ 4);
303 stpncpy (buf
+ 6, "cd", l0
+ 5);
307 sprintf (buf
+ 8, "%d", num1
);
311 snprintf (buf
+ 8, l0
+ 3, "%d", num2
);
314 memcpy (buf
, str1
+ 2, l0
+ 9);
319 memcpy (buf
, str1
+ 3, l0
+ 8);
321 strncat (buf
, "ZYXWV", l0
+ 3);
325 memcpy (a
.buf1
+ 1, "abcdefghij", l0
+ 10);
329 memmove (a
.buf1
+ 2, a
.buf1
+ 1, l0
+ 9);
333 p
= mempcpy (a
.buf1
+ 6, "abcde", l0
+ 5);
337 memset (a
.buf1
+ 9, 'j', l0
+ 2);
340 #if __USE_FORTIFY_LEVEL >= 2
347 strcpy (a
.buf1
+ (O
+ 4), str1
+ 5);
351 p
= stpcpy (a
.buf1
+ (O
+ 8), str2
);
355 strncpy (a
.buf1
+ (O
+ 6), "X", l0
+ 4);
359 sprintf (a
.buf1
+ (O
+ 7), "%d", num1
);
363 snprintf (a
.buf1
+ (O
+ 7), l0
+ 3, "%d", num2
);
366 memcpy (a
.buf1
, str1
+ (3 - O
), l0
+ 8 + O
);
368 strcat (a
.buf1
, "AB");
371 memcpy (a
.buf1
, str1
+ (4 - O
), l0
+ 7 + O
);
373 strncat (a
.buf1
, "ZYXWV", l0
+ 3);
378 /* These ops can be done without runtime checking of object size. */
379 wmemcpy (wbuf
, L
"abcdefghij", 10);
380 wmemmove (wbuf
+ 1, wbuf
, 9);
381 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
384 if (wmempcpy (wbuf
+ 5, L
"abcde", 5) != wbuf
+ 10
385 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
388 wmemset (wbuf
+ 8, L
'j', 2);
389 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
392 wcscpy (wbuf
+ 4, L
"EDCBA");
393 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
396 if (wcpcpy (wbuf
+ 8, L
"F") != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
399 wcsncpy (wbuf
+ 6, L
"X", 4);
400 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
403 if (swprintf (wbuf
+ 7, 3, L
"%ls", L
"987654") >= 0
404 || wmemcmp (wbuf
, L
"aabcEDX98", 10))
407 if (swprintf (wbuf
+ 7, 3, L
"64") != 2
408 || wmemcmp (wbuf
, L
"aabcEDX64", 10))
411 /* These ops need runtime checking, but shouldn't __chk_fail. */
412 wmemcpy (wbuf
, L
"abcdefghij", l0
+ 10);
413 wmemmove (wbuf
+ 1, wbuf
, l0
+ 9);
414 if (wmemcmp (wbuf
, L
"aabcdefghi", 10))
417 if (wmempcpy (wbuf
+ 5, L
"abcde", l0
+ 5) != wbuf
+ 10
418 || wmemcmp (wbuf
, L
"aabcdabcde", 10))
421 wmemset (wbuf
+ 8, L
'j', l0
+ 2);
422 if (wmemcmp (wbuf
, L
"aabcdabcjj", 10))
425 wcscpy (wbuf
+ 4, wstr1
+ 5);
426 if (wmemcmp (wbuf
, L
"aabcEDCBA", 10))
429 if (wcpcpy (wbuf
+ 8, wstr2
) != wbuf
+ 9 || wmemcmp (wbuf
, L
"aabcEDCBF", 10))
432 wcsncpy (wbuf
+ 6, L
"X", l0
+ 4);
433 if (wmemcmp (wbuf
, L
"aabcEDX\0\0", 10))
436 if (wcpncpy (wbuf
+ 5, L
"cd", l0
+ 5) != wbuf
+ 7
437 || wmemcmp (wbuf
, L
"aabcEcd\0\0", 10))
440 if (swprintf (wbuf
+ 7, 3, L
"%d", num2
) >= 0
441 || wmemcmp (wbuf
, L
"aabcEcd98", 10))
444 wbuf
[l0
+ 8] = L
'\0';
446 if (wmemcmp (wbuf
, L
"aabcEcd9A", 10))
449 wbuf
[l0
+ 7] = L
'\0';
450 wcsncat (wbuf
, L
"ZYXWV", l0
+ 2);
451 if (wmemcmp (wbuf
, L
"aabcEcdZY", 10))
454 wmemcpy (wa
.buf1
, L
"abcdefghij", l0
+ 10);
455 wmemmove (wa
.buf1
+ 1, wa
.buf1
, l0
+ 9);
456 if (wmemcmp (wa
.buf1
, L
"aabcdefghi", 10))
459 if (wmempcpy (wa
.buf1
+ 5, L
"abcde", l0
+ 5) != wa
.buf1
+ 10
460 || wmemcmp (wa
.buf1
, L
"aabcdabcde", 10))
463 wmemset (wa
.buf1
+ 8, L
'j', l0
+ 2);
464 if (wmemcmp (wa
.buf1
, L
"aabcdabcjj", 10))
467 #if __USE_FORTIFY_LEVEL < 2
468 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
469 and sufficient GCC support, as the string operations overflow
470 from a.buf1 into a.buf2. */
471 wcscpy (wa
.buf1
+ 4, wstr1
+ 5);
472 if (wmemcmp (wa
.buf1
, L
"aabcEDCBA", 10))
475 if (wcpcpy (wa
.buf1
+ 8, wstr2
) != wa
.buf1
+ 9
476 || wmemcmp (wa
.buf1
, L
"aabcEDCBF", 10))
479 wcsncpy (wa
.buf1
+ 6, L
"X", l0
+ 4);
480 if (wmemcmp (wa
.buf1
, L
"aabcEDX\0\0", 10))
483 if (swprintf (wa
.buf1
+ 7, 3, L
"%d", num2
) >= 0
484 || wmemcmp (wa
.buf1
, L
"aabcEDX98", 10))
487 wa
.buf1
[l0
+ 8] = L
'\0';
488 wcscat (wa
.buf1
, L
"A");
489 if (wmemcmp (wa
.buf1
, L
"aabcEDX9A", 10))
492 wa
.buf1
[l0
+ 7] = L
'\0';
493 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 2);
494 if (wmemcmp (wa
.buf1
, L
"aabcEDXZY", 10))
499 #if __USE_FORTIFY_LEVEL >= 1
500 /* Now check if all buffer overflows are caught at runtime. */
503 wmemcpy (wbuf
+ 1, L
"abcdefghij", l0
+ 10);
507 wmemmove (wbuf
+ 2, wbuf
+ 1, l0
+ 9);
511 wp
= wmempcpy (wbuf
+ 6, L
"abcde", l0
+ 5);
515 wmemset (wbuf
+ 9, L
'j', l0
+ 2);
519 wcscpy (wbuf
+ 5, wstr1
+ 5);
523 wp
= wcpcpy (wbuf
+ 9, wstr2
);
527 wcsncpy (wbuf
+ 7, L
"X", l0
+ 4);
531 wcpncpy (wbuf
+ 6, L
"cd", l0
+ 5);
534 wmemcpy (wbuf
, wstr1
+ 2, l0
+ 9);
536 wcscat (wbuf
, L
"AB");
539 wmemcpy (wbuf
, wstr1
+ 3, l0
+ 8);
541 wcsncat (wbuf
, L
"ZYXWV", l0
+ 3);
545 wmemcpy (wa
.buf1
+ 1, L
"abcdefghij", l0
+ 10);
549 wmemmove (wa
.buf1
+ 2, wa
.buf1
+ 1, l0
+ 9);
553 wp
= wmempcpy (wa
.buf1
+ 6, L
"abcde", l0
+ 5);
557 wmemset (wa
.buf1
+ 9, L
'j', l0
+ 2);
560 #if __USE_FORTIFY_LEVEL >= 2
567 wcscpy (wa
.buf1
+ (O
+ 4), wstr1
+ 5);
571 wp
= wcpcpy (wa
.buf1
+ (O
+ 8), wstr2
);
575 wcsncpy (wa
.buf1
+ (O
+ 6), L
"X", l0
+ 4);
578 wmemcpy (wa
.buf1
, wstr1
+ (3 - O
), l0
+ 8 + O
);
580 wcscat (wa
.buf1
, L
"AB");
583 wmemcpy (wa
.buf1
, wstr1
+ (4 - O
), l0
+ 7 + O
);
585 wcsncat (wa
.buf1
, L
"ZYXWV", l0
+ 3);
590 /* Now checks for %n protection. */
592 /* Constant literals passed directly are always ok
593 (even with warnings about possible bugs from GCC). */
595 if (sprintf (buf
, "%s%n%s%n", str2
, &n1
, str2
, &n2
) != 2
596 || n1
!= 1 || n2
!= 2)
599 /* In this case the format string is not known at compile time,
600 but resides in read-only memory, so is ok. */
601 if (snprintf (buf
, 4, str3
, str2
, &n1
, str2
, &n2
) != 2
602 || n1
!= 1 || n2
!= 2)
605 strcpy (buf2
+ 2, "%n%s%n");
606 /* When the format string is writable and contains %n,
607 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
609 if (sprintf (buf
, buf2
, str2
, &n1
, str2
, &n1
) != 2)
614 if (snprintf (buf
, 3, buf2
, str2
, &n1
, str2
, &n1
) != 2)
618 /* But if there is no %n, even writable format string
621 if (sprintf (buf
, buf2
+ 4, str2
) != 1)
624 /* Constant literals passed directly are always ok
625 (even with warnings about possible bugs from GCC). */
626 if (printf ("%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
627 || n1
!= 7 || n2
!= 14)
630 /* In this case the format string is not known at compile time,
631 but resides in read-only memory, so is ok. */
632 if (printf (str3
, str4
, &n1
, str5
, &n2
) != 14
633 || n1
!= 7 || n2
!= 14)
636 strcpy (buf2
+ 2, "%n%s%n");
637 /* When the format string is writable and contains %n,
638 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
640 if (printf (buf2
, str4
, &n1
, str5
, &n1
) != 14)
644 /* But if there is no %n, even writable format string
647 if (printf (buf2
+ 4, str5
) != 7)
652 /* Constant literals passed directly are always ok
653 (even with warnings about possible bugs from GCC). */
654 if (fprintf (fp
, "%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
655 || n1
!= 7 || n2
!= 14)
658 /* In this case the format string is not known at compile time,
659 but resides in read-only memory, so is ok. */
660 if (fprintf (fp
, str3
, str4
, &n1
, str5
, &n2
) != 14
661 || n1
!= 7 || n2
!= 14)
664 strcpy (buf2
+ 2, "%n%s%n");
665 /* When the format string is writable and contains %n,
666 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
668 if (fprintf (fp
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
672 /* But if there is no %n, even writable format string
675 if (fprintf (fp
, buf2
+ 4, str5
) != 7)
678 if (freopen (temp_filename
, "r", stdin
) == NULL
)
680 puts ("could not open temporary file");
684 if (gets (buf
) != buf
|| memcmp (buf
, "abcdefgh", 9))
686 if (gets (buf
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
689 #if __USE_FORTIFY_LEVEL >= 1
691 if (gets (buf
) != buf
)
698 if (fgets (buf
, sizeof (buf
), stdin
) != buf
699 || memcmp (buf
, "abcdefgh\n", 10))
701 if (fgets (buf
, sizeof (buf
), stdin
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
706 if (fgets (buf
, l0
+ sizeof (buf
), stdin
) != buf
707 || memcmp (buf
, "abcdefgh\n", 10))
710 #if __USE_FORTIFY_LEVEL >= 1
712 if (fgets (buf
, sizeof (buf
) + 1, stdin
) != buf
)
717 if (fgets (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
724 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
725 || memcmp (buf
, "abcdefgh\n", 10))
727 if (fgets_unlocked (buf
, sizeof (buf
), stdin
) != buf
728 || memcmp (buf
, "ABCDEFGHI", 10))
733 if (fgets_unlocked (buf
, l0
+ sizeof (buf
), stdin
) != buf
734 || memcmp (buf
, "abcdefgh\n", 10))
737 #if __USE_FORTIFY_LEVEL >= 1
739 if (fgets_unlocked (buf
, sizeof (buf
) + 1, stdin
) != buf
)
744 if (fgets_unlocked (buf
, l0
+ sizeof (buf
) + 1, stdin
) != buf
)
749 lseek (fileno (stdin
), 0, SEEK_SET
);
751 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
752 || memcmp (buf
, "abcdefgh\n", 9))
754 if (read (fileno (stdin
), buf
, sizeof (buf
) - 1) != sizeof (buf
) - 1
755 || memcmp (buf
, "ABCDEFGHI", 9))
758 lseek (fileno (stdin
), 0, SEEK_SET
);
760 if (read (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1) != sizeof (buf
) - 1
761 || memcmp (buf
, "abcdefgh\n", 9))
764 #if __USE_FORTIFY_LEVEL >= 1
766 if (read (fileno (stdin
), buf
, sizeof (buf
) + 1) != sizeof (buf
) + 1)
771 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
773 || memcmp (buf
, "\nABCDEFGH", 9))
775 if (pread (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
776 || memcmp (buf
, "abcdefgh\n", 9))
778 if (pread (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
780 || memcmp (buf
, "h\nABCDEFG", 9))
783 #if __USE_FORTIFY_LEVEL >= 1
785 if (pread (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
791 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, sizeof (buf
) - 2)
793 || memcmp (buf
, "\nABCDEFGH", 9))
795 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) - 1, 0) != sizeof (buf
) - 1
796 || memcmp (buf
, "abcdefgh\n", 9))
798 if (pread64 (fileno (stdin
), buf
, l0
+ sizeof (buf
) - 1, sizeof (buf
) - 3)
800 || memcmp (buf
, "h\nABCDEFG", 9))
803 #if __USE_FORTIFY_LEVEL >= 1
805 if (pread64 (fileno (stdin
), buf
, sizeof (buf
) + 1, 2 * sizeof (buf
))
811 if (freopen (temp_filename
, "r", stdin
) == NULL
)
813 puts ("could not open temporary file");
817 if (fseek (stdin
, 9 + 10 + 11, SEEK_SET
))
819 puts ("could not seek in test file");
823 #if __USE_FORTIFY_LEVEL >= 1
825 if (gets (buf
) != buf
)
830 /* Check whether missing N$ formats are detected. */
832 printf ("%3$d\n", 1, 2, 3, 4);
836 fprintf (stdout
, "%3$d\n", 1, 2, 3, 4);
840 sprintf (buf
, "%3$d\n", 1, 2, 3, 4);
844 snprintf (buf
, sizeof (buf
), "%3$d\n", 1, 2, 3, 4);
848 if (socketpair (PF_UNIX
, SOCK_STREAM
, 0, sp
))
852 const char *sendstr
= "abcdefgh\nABCDEFGH\n0123456789\n";
853 if (send (sp
[0], sendstr
, strlen (sendstr
), 0) != strlen (sendstr
))
857 if (recv (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
)
859 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
862 if (recv (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
)
863 != sizeof recvbuf
- 7
864 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
867 #if __USE_FORTIFY_LEVEL >= 1
869 if (recv (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
)
875 if (recv (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
)
876 != sizeof recvbuf
- 3)
882 struct sockaddr_un sa_un
;
885 if (recvfrom (sp
[1], recvbuf
, sizeof recvbuf
, MSG_PEEK
, &sa_un
, &sl
)
887 || memcmp (recvbuf
, sendstr
, sizeof recvbuf
) != 0)
891 if (recvfrom (sp
[1], recvbuf
+ 6, l0
+ sizeof recvbuf
- 7, MSG_PEEK
,
892 &sa_un
, &sl
) != sizeof recvbuf
- 7
893 || memcmp (recvbuf
+ 6, sendstr
, sizeof recvbuf
- 7) != 0)
896 #if __USE_FORTIFY_LEVEL >= 1
899 if (recvfrom (sp
[1], recvbuf
+ 1, sizeof recvbuf
, MSG_PEEK
, &sa_un
, &sl
)
906 if (recvfrom (sp
[1], recvbuf
+ 4, l0
+ sizeof recvbuf
- 3, MSG_PEEK
,
907 &sa_un
, &sl
) != sizeof recvbuf
- 3)
916 char fname
[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
917 char *enddir
= strchr (fname
, '\0');
918 if (mkdtemp (fname
) == NULL
)
920 printf ("mkdtemp failed: %m\n");
924 if (symlink ("bar", fname
) != 0)
928 if (readlink (fname
, readlinkbuf
, 4) != 3
929 || memcmp (readlinkbuf
, "bar", 3) != 0)
931 if (readlink (fname
, readlinkbuf
+ 1, l0
+ 3) != 3
932 || memcmp (readlinkbuf
, "bbar", 4) != 0)
935 #if __USE_FORTIFY_LEVEL >= 1
937 if (readlink (fname
, readlinkbuf
+ 2, l0
+ 3) != 3)
942 if (readlink (fname
, readlinkbuf
+ 3, 4) != 3)
947 char *cwd1
= getcwd (NULL
, 0);
951 char *cwd2
= getcwd (NULL
, 250);
957 if (strcmp (cwd1
, cwd2
) != 0)
964 char *cwd3
= getcwd (NULL
, 0);
967 if (strcmp (fname
, cwd3
) != 0)
968 printf ("getcwd after chdir is '%s' != '%s',"
969 "get{c,}wd tests skipped\n", cwd3
, fname
);
972 char getcwdbuf
[sizeof fname
- 3];
974 char *cwd4
= getcwd (getcwdbuf
, sizeof getcwdbuf
);
975 if (cwd4
!= getcwdbuf
976 || strcmp (getcwdbuf
, fname
) != 0)
979 cwd4
= getcwd (getcwdbuf
+ 1, l0
+ sizeof getcwdbuf
- 1);
980 if (cwd4
!= getcwdbuf
+ 1
981 || getcwdbuf
[0] != fname
[0]
982 || strcmp (getcwdbuf
+ 1, fname
) != 0)
985 #if __USE_FORTIFY_LEVEL >= 1
987 if (getcwd (getcwdbuf
+ 2, l0
+ sizeof getcwdbuf
)
993 if (getcwd (getcwdbuf
+ 2, sizeof getcwdbuf
)
999 if (getwd (getcwdbuf
) != getcwdbuf
1000 || strcmp (getcwdbuf
, fname
) != 0)
1003 if (getwd (getcwdbuf
+ 1) != getcwdbuf
+ 1
1004 || strcmp (getcwdbuf
+ 1, fname
) != 0)
1007 #if __USE_FORTIFY_LEVEL >= 1
1009 if (getwd (getcwdbuf
+ 2) != getcwdbuf
+ 2)
1015 if (chdir (cwd1
) != 0)
1023 if (unlink (fname
) != 0)
1027 if (rmdir (fname
) != 0)
1032 char largebuf
[PATH_MAX
];
1033 char *realres
= realpath (".", largebuf
);
1034 if (realres
!= largebuf
)
1037 # if __USE_FORTIFY_LEVEL >= 1
1040 realres
= realpath (".", realbuf
);
1041 if (realres
!= realbuf
)
1047 if (setlocale (LC_ALL
, "de_DE.UTF-8") != NULL
)
1049 assert (MB_CUR_MAX
<= 10);
1051 /* First a simple test. */
1053 if (wctomb (enough
, L
'A') != 1)
1056 #if __USE_FORTIFY_LEVEL >= 1
1057 /* We know the wchar_t encoding is ISO 10646. So pick a
1058 character which has a multibyte representation which does not
1062 if (wctomb (smallbuf
, L
'\x100') != 2)
1068 memset (&s
, '\0', sizeof (s
));
1069 if (wcrtomb (enough
, L
'D', &s
) != 1 || enough
[0] != 'D')
1072 #if __USE_FORTIFY_LEVEL >= 1
1073 /* We know the wchar_t encoding is ISO 10646. So pick a
1074 character which has a multibyte representation which does not
1078 if (wcrtomb (smallbuf
, L
'\x100', &s
) != 2)
1083 wchar_t wenough
[10];
1084 memset (&s
, '\0', sizeof (s
));
1085 const char *cp
= "A";
1086 if (mbsrtowcs (wenough
, &cp
, 10, &s
) != 1
1087 || wcscmp (wenough
, L
"A") != 0)
1091 if (mbsrtowcs (wenough
, &cp
, l0
+ 10, &s
) != 2
1092 || wcscmp (wenough
, L
"BC") != 0)
1095 #if __USE_FORTIFY_LEVEL >= 1
1097 wchar_t wsmallbuf
[2];
1099 mbsrtowcs (wsmallbuf
, &cp
, 10, &s
);
1104 if (mbstowcs (wenough
, cp
, 10) != 1
1105 || wcscmp (wenough
, L
"A") != 0)
1109 if (mbstowcs (wenough
, cp
, l0
+ 10) != 3
1110 || wcscmp (wenough
, L
"DEF") != 0)
1113 #if __USE_FORTIFY_LEVEL >= 1
1115 wchar_t wsmallbuf
[2];
1117 mbstowcs (wsmallbuf
, cp
, 10);
1121 memset (&s
, '\0', sizeof (s
));
1123 wcscpy (wenough
, L
"DEF");
1124 if (mbsnrtowcs (wenough
, &cp
, 1, 10, &s
) != 1
1125 || wcscmp (wenough
, L
"AEF") != 0)
1129 if (mbsnrtowcs (wenough
, &cp
, 1, l0
+ 10, &s
) != 1
1130 || wcscmp (wenough
, L
"IEF") != 0)
1133 #if __USE_FORTIFY_LEVEL >= 1
1135 wchar_t wsmallbuf
[2];
1137 mbsnrtowcs (wsmallbuf
, &cp
, 3, 10, &s
);
1141 memset (&s
, '\0', sizeof (s
));
1142 const wchar_t *wcp
= L
"A";
1143 if (wcsrtombs (enough
, &wcp
, 10, &s
) != 1
1144 || strcmp (enough
, "A") != 0)
1148 if (wcsrtombs (enough
, &wcp
, l0
+ 10, &s
) != 2
1149 || strcmp (enough
, "BC") != 0)
1152 #if __USE_FORTIFY_LEVEL >= 1
1156 wcsrtombs (smallbuf
, &wcp
, 10, &s
);
1160 memset (enough
, 'Z', sizeof (enough
));
1162 if (wcstombs (enough
, wcp
, 10) != 2
1163 || strcmp (enough
, "EF") != 0)
1167 if (wcstombs (enough
, wcp
, l0
+ 10) != 1
1168 || strcmp (enough
, "G") != 0)
1171 #if __USE_FORTIFY_LEVEL >= 1
1175 wcstombs (smallbuf
, wcp
, 10);
1179 memset (&s
, '\0', sizeof (s
));
1181 if (wcsnrtombs (enough
, &wcp
, 1, 10, &s
) != 1
1182 || strcmp (enough
, "A") != 0)
1186 if (wcsnrtombs (enough
, &wcp
, 1, l0
+ 10, &s
) != 1
1187 || strcmp (enough
, "B") != 0)
1190 #if __USE_FORTIFY_LEVEL >= 1
1194 wcsnrtombs (smallbuf
, &wcp
, 3, 10, &s
);
1200 puts ("cannot set locale");
1204 fd
= posix_openpt (O_RDWR
);
1208 if (ptsname_r (fd
, enough
, sizeof (enough
)) != 0)
1211 #if __USE_FORTIFY_LEVEL >= 1
1214 if (ptsname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1222 confstr (_CS_GNU_LIBC_VERSION
, largebuf
, sizeof (largebuf
));
1223 # if __USE_FORTIFY_LEVEL >= 1
1226 confstr (_CS_GNU_LIBC_VERSION
, smallbuf
, sizeof (largebuf
));
1232 int ngr
= getgroups (5, grpslarge
);
1233 asm volatile ("" : : "r" (ngr
));
1234 #if __USE_FORTIFY_LEVEL >= 1
1237 ngr
= getgroups (5, (gid_t
*) smallbuf
);
1238 asm volatile ("" : : "r" (ngr
));
1242 fd
= open (_PATH_TTY
, O_RDONLY
);
1246 if (ttyname_r (fd
, enough
, sizeof (enough
)) != 0)
1249 #if __USE_FORTIFY_LEVEL >= 1
1252 if (ttyname_r (fd
, smallbuf
, sizeof (smallbuf
) + 1) == 0)
1259 char hostnamelarge
[1000];
1260 gethostname (hostnamelarge
, sizeof (hostnamelarge
));
1261 #if __USE_FORTIFY_LEVEL >= 1
1264 gethostname (smallbuf
, sizeof (hostnamelarge
));
1268 char loginlarge
[1000];
1269 getlogin_r (loginlarge
, sizeof (hostnamelarge
));
1270 #if __USE_FORTIFY_LEVEL >= 1
1273 getlogin_r (smallbuf
, sizeof (loginlarge
));
1277 char domainnamelarge
[1000];
1278 int res
= getdomainname (domainnamelarge
, sizeof (domainnamelarge
));
1279 asm volatile ("" : : "r" (res
));
1280 #if __USE_FORTIFY_LEVEL >= 1
1283 res
= getdomainname (smallbuf
, sizeof (domainnamelarge
));
1284 asm volatile ("" : : "r" (res
));