1 /* Copyright (C) 2004-2024 Free Software Foundation, Inc.
2 Copyright The GNU Toolchain Authors.
3 This file is part of the GNU C Library.
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, see
17 <https://www.gnu.org/licenses/>. */
19 /* This file tests gets. Force it to be declared. */
21 #undef __GLIBC_USE_DEPRECATED_GETS
22 #define __GLIBC_USE_DEPRECATED_GETS 1
37 #include <sys/select.h>
38 #include <sys/socket.h>
42 #include <support/temp_file.h>
43 #include <support/support.h>
46 # define MEMPCPY memcpy
47 # define WMEMPCPY wmemcpy
48 # define MEMPCPY_RET(x) 0
49 # define WMEMPCPY_RET(x) 0
51 # define MEMPCPY mempcpy
52 # define WMEMPCPY wmempcpy
53 # define MEMPCPY_RET(x) __builtin_strlen (x)
54 # define WMEMPCPY_RET(x) wcslen (x)
57 #define obstack_chunk_alloc malloc
58 #define obstack_chunk_free free
60 static char *temp_filename;
62 static int temp_fd_dprintf;
65 do_prepare (int argc, char *argv[])
67 int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
70 printf ("cannot create temporary file: %m\n");
74 const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
75 if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
77 puts ("could not write test strings into file");
78 unlink (temp_filename);
82 temp_fd_dprintf = create_temp_file ("tst-chk2.", NULL);
83 if (temp_fd_dprintf == -1)
85 printf ("cannot create temporary file: %m\n");
89 #define PREPARE do_prepare
91 static volatile int chk_fail_ok;
92 static volatile int ret;
93 static jmp_buf chk_fail_buf;
101 longjmp (chk_fail_buf, 1);
107 #if __USE_FORTIFY_LEVEL == 3
108 volatile size_t buf_size = 10;
112 #define buf_size sizeof (buf)
115 static volatile size_t l0;
116 static volatile char *p;
117 static volatile wchar_t *wp;
118 static const char *str1 = "JIHGFEDCBA";
119 static const char *str2 = "F";
120 static const char *str3 = "%s%n%s%n";
121 static const char *str4 = "Hello, ";
122 static const char *str5 = "World!\n";
123 static const wchar_t *wstr1 = L"JIHGFEDCBA";
124 static const wchar_t *wstr2 = L"F";
125 static const wchar_t *wstr3 = L"%s%n%s%n";
126 static const wchar_t *wstr4 = L"Hello, ";
127 static const wchar_t *wstr5 = L"World!\n";
128 static char buf2[10] = "%s";
129 static int num1 = 67;
130 static int num2 = 987654;
133 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
134 #define CHK_FAIL_START \
136 if (! setjmp (chk_fail_buf)) \
138 #define CHK_FAIL_END \
142 #if __USE_FORTIFY_LEVEL >= 2
143 # define CHK_FAIL2_START CHK_FAIL_START
144 # define CHK_FAIL2_END CHK_FAIL_END
146 # define CHK_FAIL2_START
147 # define CHK_FAIL2_END
153 #if __USE_FORTIFY_LEVEL == 3
154 char *buf = (char *) malloc (buf_size);
155 wchar_t *wbuf = (wchar_t *) malloc (buf_size * sizeof (wchar_t));
157 set_fortify_handler (handler);
159 struct A { char buf1[9]; char buf2[1]; } a;
160 struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
162 printf ("Test checking routines at fortify level %d\n",
163 #ifdef __USE_FORTIFY_LEVEL
164 (int) __USE_FORTIFY_LEVEL
170 #if defined __USE_FORTIFY_LEVEL && !defined __fortify_function
171 printf ("Test skipped");
176 /* These ops can be done without runtime checking of object size. */
177 memcpy (buf, "abcdefghij", 10);
178 memmove (buf + 1, buf, 9);
179 if (memcmp (buf, "aabcdefghi", 10))
182 memcpy (buf, "abcdefghij", 10);
183 bcopy (buf, buf + 1, 9);
184 if (memcmp (buf, "aabcdefghi", 10))
187 if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
188 || memcmp (buf, "aabcdabcde", 10))
191 memset (buf + 8, 'j', 2);
192 if (memcmp (buf, "aabcdabcjj", 10))
196 if (memcmp (buf, "aabcdabc\0\0", 10))
199 explicit_bzero (buf + 6, 4);
200 if (memcmp (buf, "aabcda\0\0\0\0", 10))
203 strcpy (buf + 4, "EDCBA");
204 if (memcmp (buf, "aabcEDCBA", 10))
207 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
210 strncpy (buf + 6, "X", 4);
211 if (memcmp (buf, "aabcEDX\0\0", 10))
214 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
217 if (snprintf (buf + 7, 3, "%s", "987654") != 6
218 || memcmp (buf, "aabcEDX98", 10))
221 /* These ops need runtime checking, but shouldn't __chk_fail. */
222 memcpy (buf, "abcdefghij", l0 + 10);
223 memmove (buf + 1, buf, l0 + 9);
224 if (memcmp (buf, "aabcdefghi", 10))
227 memcpy (buf, "abcdefghij", l0 + 10);
228 bcopy (buf, buf + 1, l0 + 9);
229 if (memcmp (buf, "aabcdefghi", 10))
232 if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
233 || memcmp (buf, "aabcdabcde", 10))
236 memset (buf + 8, 'j', l0 + 2);
237 if (memcmp (buf, "aabcdabcjj", 10))
240 bzero (buf + 8, l0 + 2);
241 if (memcmp (buf, "aabcdabc\0\0", 10))
244 explicit_bzero (buf + 6, l0 + 4);
245 if (memcmp (buf, "aabcda\0\0\0\0", 10))
248 strcpy (buf + 4, str1 + 5);
249 if (memcmp (buf, "aabcEDCBA", 10))
252 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
255 strncpy (buf + 6, "X", l0 + 4);
256 if (memcmp (buf, "aabcEDX\0\0", 10))
259 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
260 || memcmp (buf, "aabcEcd\0\0", 10))
263 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
266 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
271 if (memcmp (buf, "aabcEcd9A", 10))
275 strncat (buf, "ZYXWV", l0 + 2);
276 if (memcmp (buf, "aabcEcdZY", 10))
279 /* The following tests are supposed to succeed at all fortify
280 levels, even though they overflow a.buf1 into a.buf2. */
281 memcpy (a.buf1, "abcdefghij", l0 + 10);
282 memmove (a.buf1 + 1, a.buf1, l0 + 9);
283 if (memcmp (a.buf1, "aabcdefghi", 10))
286 memcpy (a.buf1, "abcdefghij", l0 + 10);
287 bcopy (a.buf1, a.buf1 + 1, l0 + 9);
288 if (memcmp (a.buf1, "aabcdefghi", 10))
291 if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
292 != a.buf1 + 5 + MEMPCPY_RET ("abcde")
293 || memcmp (a.buf1, "aabcdabcde", 10))
296 memset (a.buf1 + 8, 'j', l0 + 2);
297 if (memcmp (a.buf1, "aabcdabcjj", 10))
300 bzero (a.buf1 + 8, l0 + 2);
301 if (memcmp (a.buf1, "aabcdabc\0\0", 10))
304 explicit_bzero (a.buf1 + 6, l0 + 4);
305 if (memcmp (a.buf1, "aabcda\0\0\0\0", 10))
308 #if __USE_FORTIFY_LEVEL < 2
309 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
310 and sufficient GCC support, as the string operations overflow
311 from a.buf1 into a.buf2. */
312 strcpy (a.buf1 + 4, str1 + 5);
313 if (memcmp (a.buf1, "aabcEDCBA", 10))
316 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
317 || memcmp (a.buf1, "aabcEDCBF", 10))
320 strncpy (a.buf1 + 6, "X", l0 + 4);
321 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
324 if (sprintf (a.buf1 + 7, "%d", num1) != 2
325 || memcmp (a.buf1, "aabcEDX67", 10))
328 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
329 || memcmp (a.buf1, "aabcEDX98", 10))
332 a.buf1[l0 + 8] = '\0';
333 strcat (a.buf1, "A");
334 if (memcmp (a.buf1, "aabcEDX9A", 10))
337 a.buf1[l0 + 7] = '\0';
338 strncat (a.buf1, "ZYXWV", l0 + 2);
339 if (memcmp (a.buf1, "aabcEDXZY", 10))
344 #if __USE_FORTIFY_LEVEL >= 1
345 /* Now check if all buffer overflows are caught at runtime.
346 N.B. All tests involving a length parameter need to be done
347 twice: once with the length a compile-time constant, once without. */
350 memcpy (buf + 1, "abcdefghij", 10);
354 memcpy (buf + 1, "abcdefghij", l0 + 10);
358 memmove (buf + 2, buf + 1, 9);
362 memmove (buf + 2, buf + 1, l0 + 9);
366 bcopy (buf + 1, buf + 2, 9);
370 bcopy (buf + 1, buf + 2, l0 + 9);
375 p = (char *) mempcpy (buf + 6, "abcde", 5);
379 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
384 memset (buf + 9, 'j', 2);
388 memset (buf + 9, 'j', l0 + 2);
396 bzero (buf + 9, l0 + 2);
400 explicit_bzero (buf + 9, 2);
404 explicit_bzero (buf + 9, l0 + 2);
408 strcpy (buf + 5, str1 + 5);
412 p = stpcpy (buf + 9, str2);
416 strncpy (buf + 7, "X", 4);
420 strncpy (buf + 7, "X", l0 + 4);
424 stpncpy (buf + 6, "cd", 5);
428 stpncpy (buf + 6, "cd", l0 + 5);
432 sprintf (buf + 8, "%d", num1);
436 snprintf (buf + 8, 3, "%d", num2);
440 snprintf (buf + 8, l0 + 3, "%d", num2);
444 swprintf (wbuf + 8, 3, L"%d", num1);
448 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
451 memcpy (buf, str1 + 2, 9);
456 memcpy (buf, str1 + 3, 8);
458 strncat (buf, "ZYXWV", 3);
461 memcpy (buf, str1 + 3, 8);
463 strncat (buf, "ZYXWV", l0 + 3);
467 memcpy (a.buf1 + 1, "abcdefghij", 10);
471 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
475 memmove (a.buf1 + 2, a.buf1 + 1, 9);
479 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
483 bcopy (a.buf1 + 1, a.buf1 + 2, 9);
487 bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
492 p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
496 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
501 memset (a.buf1 + 9, 'j', 2);
505 memset (a.buf1 + 9, 'j', l0 + 2);
509 bzero (a.buf1 + 9, 2);
513 bzero (a.buf1 + 9, l0 + 2);
517 explicit_bzero (a.buf1 + 9, 2);
521 explicit_bzero (a.buf1 + 9, l0 + 2);
524 # if __USE_FORTIFY_LEVEL >= 2
531 strcpy (a.buf1 + (O + 4), str1 + 5);
535 p = stpcpy (a.buf1 + (O + 8), str2);
539 strncpy (a.buf1 + (O + 6), "X", 4);
543 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
547 strlcpy (a.buf1 + (O + 6), "X", 4);
551 strlcpy (a.buf1 + (O + 6), "X", l0 + 4);
555 char *volatile buf2 = buf;
556 if (strlcpy (buf2, "a", sizeof (buf) + 1) != 1)
561 sprintf (a.buf1 + (O + 7), "%d", num1);
565 snprintf (a.buf1 + (O + 7), 3, "%d", num2);
569 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
572 memcpy (a.buf1, str1 + (3 - O), 8 + O);
574 strcat (a.buf1, "AB");
577 memcpy (a.buf1, str1 + (4 - O), 7 + O);
579 strncat (a.buf1, "ZYXWV", l0 + 3);
582 memset (a.buf1, 0, sizeof (a.buf1));
584 strlcat (a.buf1 + (O + 6), "X", 4);
587 memset (a.buf1, 0, sizeof (a.buf1));
589 strlcat (a.buf1 + (O + 6), "X", l0 + 4);
594 char *volatile buf2 = buf;
595 if (strlcat (buf2, "a", sizeof (buf) + 1) != 1)
601 /* These ops can be done without runtime checking of object size. */
602 wmemcpy (wbuf, L"abcdefghij", 10);
603 wmemmove (wbuf + 1, wbuf, 9);
604 if (wmemcmp (wbuf, L"aabcdefghi", 10))
607 if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
608 || wmemcmp (wbuf, L"aabcdabcde", 10))
611 wmemset (wbuf + 8, L'j', 2);
612 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
615 wcscpy (wbuf + 4, L"EDCBA");
616 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
619 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
622 wcsncpy (wbuf + 6, L"X", 4);
623 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
626 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
627 || wmemcmp (wbuf, L"aabcEDX98", 10))
630 if (swprintf (wbuf + 7, 3, L"64") != 2
631 || wmemcmp (wbuf, L"aabcEDX64", 10))
634 /* These ops need runtime checking, but shouldn't __chk_fail. */
635 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
636 wmemmove (wbuf + 1, wbuf, l0 + 9);
637 if (wmemcmp (wbuf, L"aabcdefghi", 10))
640 if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
641 != wbuf + 5 + WMEMPCPY_RET (L"abcde")
642 || wmemcmp (wbuf, L"aabcdabcde", 10))
645 wmemset (wbuf + 8, L'j', l0 + 2);
646 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
649 wcscpy (wbuf + 4, wstr1 + 5);
650 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
653 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
656 wcsncpy (wbuf + 6, L"X", l0 + 4);
657 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
660 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
661 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
664 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
665 || wmemcmp (wbuf, L"aabcEcd98", 10))
668 wbuf[l0 + 8] = L'\0';
670 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
673 wbuf[l0 + 7] = L'\0';
674 wcsncat (wbuf, L"ZYXWV", l0 + 2);
675 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
678 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
679 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
680 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
683 if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
684 != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
685 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
688 wmemset (wa.buf1 + 8, L'j', l0 + 2);
689 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
692 #if __USE_FORTIFY_LEVEL < 2
693 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
694 and sufficient GCC support, as the string operations overflow
695 from a.buf1 into a.buf2. */
696 wcscpy (wa.buf1 + 4, wstr1 + 5);
697 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
700 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
701 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
704 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
705 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
708 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
709 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
712 wa.buf1[l0 + 8] = L'\0';
713 wcscat (wa.buf1, L"A");
714 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
717 wa.buf1[l0 + 7] = L'\0';
718 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
719 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
724 #if __USE_FORTIFY_LEVEL >= 1
725 /* Now check if all buffer overflows are caught at runtime.
726 N.B. All tests involving a length parameter need to be done
727 twice: once with the length a compile-time constant, once without. */
730 wmemcpy (wbuf + 1, L"abcdefghij", 10);
734 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
738 wmemcpy (wbuf + 9, L"abcdefghij", 10);
742 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
746 wmemmove (wbuf + 2, wbuf + 1, 9);
750 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
755 wp = wmempcpy (wbuf + 6, L"abcde", 5);
759 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
764 wmemset (wbuf + 9, L'j', 2);
768 wmemset (wbuf + 9, L'j', l0 + 2);
772 wcscpy (wbuf + 5, wstr1 + 5);
776 wp = wcpcpy (wbuf + 9, wstr2);
780 wcsncpy (wbuf + 7, L"X", 4);
784 wcsncpy (wbuf + 7, L"X", l0 + 4);
788 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
792 wcslcpy (wbuf + 7, L"X", 4);
796 wcslcpy (wbuf + 7, L"X", l0 + 4);
800 wcslcpy (wbuf + 9, L"XABCDEFGH", 8);
804 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
808 wcpncpy (wbuf + 6, L"cd", 5);
812 wcpncpy (wbuf + 6, L"cd", l0 + 5);
815 wmemcpy (wbuf, wstr1 + 2, 9);
817 wcscat (wbuf, L"AB");
820 wmemcpy (wbuf, wstr1 + 3, 8);
822 wcsncat (wbuf, L"ZYXWV", l0 + 3);
825 wmemcpy (wbuf, wstr1 + 4, 7);
827 wcslcat (wbuf, L"ZYXWV", l0 + 11);
831 wmemcpy (wa.buf1 + 1, L"abcdefghij", 10);
835 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
839 wmemmove (wa.buf1 + 2, wa.buf1 + 1, 9);
843 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
848 wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
852 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
857 wmemset (wa.buf1 + 9, L'j', 2);
861 wmemset (wa.buf1 + 9, L'j', l0 + 2);
864 #if __USE_FORTIFY_LEVEL >= 2
871 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
875 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
879 wcsncpy (wa.buf1 + (O + 6), L"X", 4);
883 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
886 wmemcpy (wa.buf1, wstr1 + (3 - O), 8 + O);
888 wcscat (wa.buf1, L"AB");
891 wmemcpy (wa.buf1, wstr1 + (4 - O), 7 + O);
893 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
898 /* Now checks for %n protection. */
900 /* Constant literals passed directly are always ok
901 (even with warnings about possible bugs from GCC). */
903 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
904 || n1 != 1 || n2 != 2)
907 /* In this case the format string is not known at compile time,
908 but resides in read-only memory, so is ok. */
909 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
910 || n1 != 1 || n2 != 2)
913 if (dprintf (temp_fd_dprintf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
914 || n1 != 1 || n2 != 2)
917 strcpy (buf2 + 2, "%n%s%n");
918 /* When the format string is writable and contains %n,
919 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
921 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
926 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
931 if (dprintf (temp_fd_dprintf, buf2, str2, &n1, str2, &n1) != 2)
935 /* But if there is no %n, even writable format string
938 if (sprintf (buf, buf2 + 4, str2) != 1)
941 /* Constant literals passed directly are always ok
942 (even with warnings about possible bugs from GCC). */
943 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
944 || n1 != 7 || n2 != 14)
947 /* In this case the format string is not known at compile time,
948 but resides in read-only memory, so is ok. */
949 if (printf (str3, str4, &n1, str5, &n2) != 14
950 || n1 != 7 || n2 != 14)
953 strcpy (buf2 + 2, "%n%s%n");
954 /* When the format string is writable and contains %n,
955 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
957 if (printf (buf2, str4, &n1, str5, &n1) != 14)
961 /* But if there is no %n, even writable format string
964 if (printf (buf2 + 4, str5) != 7)
969 /* Constant literals passed directly are always ok
970 (even with warnings about possible bugs from GCC). */
971 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
972 || n1 != 7 || n2 != 14)
975 /* In this case the format string is not known at compile time,
976 but resides in read-only memory, so is ok. */
977 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
978 || n1 != 7 || n2 != 14)
981 strcpy (buf2 + 2, "%n%s%n");
982 /* When the format string is writable and contains %n,
983 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
985 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
989 /* But if there is no %n, even writable format string
992 if (fprintf (fp, buf2 + 4, str5) != 7)
997 strcpy (buf2 + 2, "%n%s%n");
998 /* When the format string is writable and contains %n,
999 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
1001 if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
1008 obstack_init (&obs);
1010 if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
1013 obstack_free (&obs, NULL);
1016 if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
1021 obstack_init (&obs);
1022 if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
1024 obstack_free (&obs, NULL);
1027 if (freopen (temp_filename, "r", stdin) == NULL)
1029 puts ("could not open temporary file");
1033 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
1035 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
1038 #if __USE_FORTIFY_LEVEL >= 1
1040 if (gets (buf) != buf)
1047 if (fgets (buf, buf_size, stdin) != buf
1048 || memcmp (buf, "abcdefgh\n", 10))
1050 if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
1055 if (fgets (buf, l0 + buf_size, stdin) != buf
1056 || memcmp (buf, "abcdefgh\n", 10))
1059 #if __USE_FORTIFY_LEVEL >= 1
1061 if (fgets (buf, buf_size + 1, stdin) != buf)
1066 if (fgets (buf, l0 + buf_size + 1, stdin) != buf)
1074 if (fgets_unlocked (buf, buf_size, stdin) != buf
1075 || memcmp (buf, "abcdefgh\n", 10))
1077 if (fgets_unlocked (buf, buf_size, stdin) != buf
1078 || memcmp (buf, "ABCDEFGHI", 10))
1083 if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf
1084 || memcmp (buf, "abcdefgh\n", 10))
1087 #if __USE_FORTIFY_LEVEL >= 1
1089 if (fgets_unlocked (buf, buf_size + 1, stdin) != buf)
1094 if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf)
1102 if (fread (buf, 1, buf_size, stdin) != buf_size
1103 || memcmp (buf, "abcdefgh\nA", 10))
1105 if (fread (buf, buf_size, 1, stdin) != 1
1106 || memcmp (buf, "BCDEFGHI\na", 10))
1111 if (fread (buf, l0 + 1, buf_size, stdin) != buf_size
1112 || memcmp (buf, "abcdefgh\nA", 10))
1114 if (fread (buf, buf_size, l0 + 1, stdin) != 1
1115 || memcmp (buf, "BCDEFGHI\na", 10))
1118 #if __USE_FORTIFY_LEVEL >= 1
1120 if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1)
1125 if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1)
1132 if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size
1133 || memcmp (buf, "abcdefgh\nA", 10))
1135 if (fread_unlocked (buf, buf_size, 1, stdin) != 1
1136 || memcmp (buf, "BCDEFGHI\na", 10))
1141 if (fread_unlocked (buf, 1, 4, stdin) != 4
1142 || memcmp (buf, "abcdFGHI\na", 10))
1144 if (fread_unlocked (buf, 4, 1, stdin) != 1
1145 || memcmp (buf, "efghFGHI\na", 10))
1150 if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size
1151 || memcmp (buf, "abcdefgh\nA", 10))
1153 if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1
1154 || memcmp (buf, "BCDEFGHI\na", 10))
1157 #if __USE_FORTIFY_LEVEL >= 1
1159 if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1)
1164 if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1)
1169 lseek (fileno (stdin), 0, SEEK_SET);
1171 if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
1172 || memcmp (buf, "abcdefgh\n", 9))
1174 if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
1175 || memcmp (buf, "ABCDEFGHI", 9))
1178 lseek (fileno (stdin), 0, SEEK_SET);
1180 if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1
1181 || memcmp (buf, "abcdefgh\n", 9))
1184 #if __USE_FORTIFY_LEVEL >= 1
1186 if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1)
1191 if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1)
1196 if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2)
1198 || memcmp (buf, "\nABCDEFGH", 9))
1200 if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
1201 || memcmp (buf, "abcdefgh\n", 9))
1203 if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
1205 || memcmp (buf, "h\nABCDEFG", 9))
1208 #if __USE_FORTIFY_LEVEL >= 1
1210 if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
1216 if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
1222 if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2)
1224 || memcmp (buf, "\nABCDEFGH", 9))
1226 if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
1227 || memcmp (buf, "abcdefgh\n", 9))
1229 if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
1231 || memcmp (buf, "h\nABCDEFG", 9))
1234 #if __USE_FORTIFY_LEVEL >= 1
1236 if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
1242 if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
1248 if (freopen (temp_filename, "r", stdin) == NULL)
1250 puts ("could not open temporary file");
1254 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
1256 puts ("could not seek in test file");
1260 #if __USE_FORTIFY_LEVEL >= 1
1262 if (gets (buf) != buf)
1267 /* Check whether missing N$ formats are detected. */
1269 printf ("%3$d\n", 1, 2, 3, 4);
1273 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
1277 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
1281 snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
1285 dprintf (temp_fd_dprintf, "%3$d\n", 1, 2, 3, 4);
1289 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
1293 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
1294 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
1295 != strlen (sendstr))
1299 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
1301 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1304 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
1305 != sizeof recvbuf - 7
1306 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1309 #if __USE_FORTIFY_LEVEL >= 1
1311 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
1317 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
1318 != sizeof recvbuf - 3)
1324 struct sockaddr_un sa_un;
1326 sl = sizeof (sa_un);
1327 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
1328 (struct sockaddr *) &sa_un, &sl)
1330 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1333 sl = sizeof (sa_un);
1334 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
1335 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
1336 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1339 #if __USE_FORTIFY_LEVEL >= 1
1341 sl = sizeof (sa_un);
1342 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1343 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1348 sl = sizeof (sa_un);
1349 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1350 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1359 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1360 char *enddir = strchr (fname, '\0');
1361 if (mkdtemp (fname) == NULL)
1363 printf ("mkdtemp failed: %m\n");
1367 if (symlink ("bar", fname) != 0)
1370 char readlinkbuf[4];
1371 if (readlink (fname, readlinkbuf, 4) != 3
1372 || memcmp (readlinkbuf, "bar", 3) != 0)
1374 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1375 || memcmp (readlinkbuf, "bbar", 4) != 0)
1378 #if __USE_FORTIFY_LEVEL >= 1
1380 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1385 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1390 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1394 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1395 || memcmp (readlinkbuf, "bar", 3) != 0)
1397 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1399 || memcmp (readlinkbuf, "bbar", 4) != 0)
1402 #if __USE_FORTIFY_LEVEL >= 1
1404 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1410 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1418 char *cwd1 = getcwd (NULL, 0);
1422 char *cwd2 = getcwd (NULL, 250);
1428 if (strcmp (cwd1, cwd2) != 0)
1435 char *cwd3 = getcwd (NULL, 0);
1438 if (strcmp (fname, cwd3) != 0)
1439 printf ("getcwd after chdir is '%s' != '%s',"
1440 "get{c,}wd tests skipped\n", cwd3, fname);
1443 char getcwdbuf[sizeof fname - 3];
1445 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1446 if (cwd4 != getcwdbuf
1447 || strcmp (getcwdbuf, fname) != 0)
1450 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1451 if (cwd4 != getcwdbuf + 1
1452 || getcwdbuf[0] != fname[0]
1453 || strcmp (getcwdbuf + 1, fname) != 0)
1456 #if __USE_FORTIFY_LEVEL >= 1
1458 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1464 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1470 if (getwd (getcwdbuf) != getcwdbuf
1471 || strcmp (getcwdbuf, fname) != 0)
1474 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1475 || strcmp (getcwdbuf + 1, fname) != 0)
1478 #if __USE_FORTIFY_LEVEL >= 1
1480 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1486 if (chdir (cwd1) != 0)
1494 if (unlink (fname) != 0)
1498 if (rmdir (fname) != 0)
1503 char largebuf[PATH_MAX];
1504 char *realres = realpath (".", largebuf);
1505 if (realres != largebuf)
1508 # if __USE_FORTIFY_LEVEL >= 1
1511 realres = realpath (".", realbuf);
1512 if (realres != realbuf)
1518 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1520 assert (MB_CUR_MAX <= 10);
1522 /* First a simple test. */
1524 if (wctomb (enough, L'A') != 1)
1527 #if __USE_FORTIFY_LEVEL >= 1
1528 /* We know the wchar_t encoding is ISO 10646. So pick a
1529 character which has a multibyte representation which does not
1533 if (wctomb (smallbuf, L'\x100') != 2)
1539 memset (&s, '\0', sizeof (s));
1540 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1543 #if __USE_FORTIFY_LEVEL >= 1
1544 /* We know the wchar_t encoding is ISO 10646. So pick a
1545 character which has a multibyte representation which does not
1549 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1553 /* Same input with a large enough buffer and we're good. */
1554 char bigenoughbuf[2];
1555 if (wcrtomb (bigenoughbuf, L'\x100', &s) != 2)
1559 wchar_t wenough[10];
1560 memset (&s, '\0', sizeof (s));
1561 const char *cp = "A";
1562 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1563 || wcscmp (wenough, L"A") != 0)
1567 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1568 || wcscmp (wenough, L"BC") != 0)
1571 #if __USE_FORTIFY_LEVEL >= 1
1573 wchar_t wsmallbuf[2];
1575 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1579 /* Bug 29030 regression check */
1581 if (mbsrtowcs (NULL, &cp, (size_t)-1, &s) != 10)
1585 if (mbstowcs (wenough, cp, 10) != 1
1586 || wcscmp (wenough, L"A") != 0)
1590 if (mbstowcs (wenough, cp, l0 + 10) != 3
1591 || wcscmp (wenough, L"DEF") != 0)
1594 #if __USE_FORTIFY_LEVEL >= 1
1596 wchar_t wsmallbuf[2];
1598 mbstowcs (wsmallbuf, cp, 10);
1602 memset (&s, '\0', sizeof (s));
1604 wcscpy (wenough, L"DEF");
1605 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1606 || wcscmp (wenough, L"AEF") != 0)
1610 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1611 || wcscmp (wenough, L"IEF") != 0)
1614 #if __USE_FORTIFY_LEVEL >= 1
1616 wchar_t wsmallbuf[2];
1618 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1622 memset (&s, '\0', sizeof (s));
1623 const wchar_t *wcp = L"A";
1624 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1625 || strcmp (enough, "A") != 0)
1629 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1630 || strcmp (enough, "BC") != 0)
1633 #if __USE_FORTIFY_LEVEL >= 1
1637 wcsrtombs (smallbuf, &wcp, 10, &s);
1641 memset (enough, 'Z', sizeof (enough));
1643 if (wcstombs (enough, wcp, 10) != 2
1644 || strcmp (enough, "EF") != 0)
1648 if (wcstombs (enough, wcp, l0 + 10) != 1
1649 || strcmp (enough, "G") != 0)
1652 #if __USE_FORTIFY_LEVEL >= 1
1656 wcstombs (smallbuf, wcp, 10);
1660 memset (&s, '\0', sizeof (s));
1662 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1663 || strcmp (enough, "A") != 0)
1667 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1668 || strcmp (enough, "B") != 0)
1671 #if __USE_FORTIFY_LEVEL >= 1
1675 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1681 puts ("cannot set locale");
1688 fd = posix_openpt (O_RDWR);
1692 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1695 #if __USE_FORTIFY_LEVEL >= 1
1698 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1707 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1708 # if __USE_FORTIFY_LEVEL >= 1
1711 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1717 int ngr = getgroups (5, grpslarge);
1718 asm volatile ("" : : "r" (ngr));
1719 #if __USE_FORTIFY_LEVEL >= 1
1722 ngr = getgroups (5, (gid_t *) smallbuf);
1723 asm volatile ("" : : "r" (ngr));
1727 fd = open (_PATH_TTY, O_RDONLY);
1731 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1734 #if __USE_FORTIFY_LEVEL >= 1
1737 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1744 char hostnamelarge[1000];
1745 gethostname (hostnamelarge, sizeof (hostnamelarge));
1746 #if __USE_FORTIFY_LEVEL >= 1
1749 gethostname (smallbuf, sizeof (hostnamelarge));
1753 char loginlarge[1000];
1754 getlogin_r (loginlarge, sizeof (hostnamelarge));
1755 #if __USE_FORTIFY_LEVEL >= 1
1758 getlogin_r (smallbuf, sizeof (loginlarge));
1762 char domainnamelarge[1000];
1763 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1764 asm volatile ("" : : "r" (res));
1765 #if __USE_FORTIFY_LEVEL >= 1
1768 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1769 asm volatile ("" : : "r" (res));
1776 FD_SET (FD_SETSIZE - 1, &s);
1777 #if __USE_FORTIFY_LEVEL >= 1
1779 FD_SET (FD_SETSIZE, &s);
1783 FD_SET (l0 + FD_SETSIZE, &s);
1787 FD_CLR (FD_SETSIZE - 1, &s);
1788 #if __USE_FORTIFY_LEVEL >= 1
1790 FD_CLR (FD_SETSIZE, &s);
1794 FD_SET (l0 + FD_SETSIZE, &s);
1798 FD_ISSET (FD_SETSIZE - 1, &s);
1799 #if __USE_FORTIFY_LEVEL >= 1
1801 FD_ISSET (FD_SETSIZE, &s);
1805 FD_ISSET (l0 + FD_SETSIZE, &s);
1809 struct pollfd fds[1];
1810 fds[0].fd = STDOUT_FILENO;
1811 fds[0].events = POLLOUT;
1813 #if __USE_FORTIFY_LEVEL >= 1
1819 poll (fds, l0 + 2, 0);
1823 ppoll (fds, 1, NULL, NULL);
1824 # if __USE_FORTIFY_LEVEL >= 1
1826 ppoll (fds, 2, NULL, NULL);
1830 ppoll (fds, l0 + 2, NULL, NULL);
1838 #include <support/test-driver.c>