Updated to fedora-glibc-20071010T2047
[glibc.git] / debug / tst-chk1.c
blobe982409e7f1ebeac1ba3af2d05e4e7e2665e829c
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
18 02111-1307 USA. */
20 /* Hack: make sure GCC doesn't know __chk_fail () will not return. */
21 #define __noreturn__
23 #include <assert.h>
24 #include <fcntl.h>
25 #include <locale.h>
26 #include <paths.h>
27 #include <setjmp.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <wchar.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
37 char *temp_filename;
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"
44 static void
45 do_prepare (void)
47 int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
48 if (temp_fd == -1)
50 printf ("cannot create temporary file: %m\n");
51 exit (1);
54 const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
55 if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
57 puts ("could not write test strings into file");
58 unlink (temp_filename);
59 exit (1);
63 volatile int chk_fail_ok;
64 volatile int ret;
65 jmp_buf chk_fail_buf;
67 static void
68 handler (int sig)
70 if (chk_fail_ok)
72 chk_fail_ok = 0;
73 longjmp (chk_fail_buf, 1);
75 else
76 _exit (127);
79 char buf[10];
80 wchar_t wbuf[10];
81 volatile size_t l0;
82 volatile char *p;
83 volatile wchar_t *wp;
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";
94 char buf2[10] = "%s";
95 int num1 = 67;
96 int num2 = 987654;
98 #define FAIL() \
99 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
100 #define CHK_FAIL_START \
101 chk_fail_ok = 1; \
102 if (! setjmp (chk_fail_buf)) \
104 #define CHK_FAIL_END \
105 chk_fail_ok = 0; \
106 FAIL (); \
108 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
109 #define CHK_FAIL2_START CHK_FAIL_START
110 #define CHK_FAIL2_END CHK_FAIL_END
111 #else
112 #define CHK_FAIL2_START
113 #define CHK_FAIL2_END
114 #endif
116 static int
117 do_test (void)
119 struct sigaction sa;
120 sa.sa_handler = handler;
121 sa.sa_flags = 0;
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);
128 if (fd == -1)
129 close (STDERR_FILENO);
130 else
132 dup2 (fd, STDERR_FILENO);
133 close (fd);
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
143 #else
145 #endif
148 #if defined __USE_FORTIFY_LEVEL && !defined __extern_always_inline
149 printf ("Test skipped");
150 if (l0 == 0)
151 return 0;
152 #endif
154 /* These ops can be done without runtime checking of object size. */
155 memcpy (buf, "abcdefghij", 10);
156 memmove (buf + 1, buf, 9);
157 if (memcmp (buf, "aabcdefghi", 10))
158 FAIL ();
160 if (mempcpy (buf + 5, "abcde", 5) != buf + 10
161 || memcmp (buf, "aabcdabcde", 10))
162 FAIL ();
164 memset (buf + 8, 'j', 2);
165 if (memcmp (buf, "aabcdabcjj", 10))
166 FAIL ();
168 strcpy (buf + 4, "EDCBA");
169 if (memcmp (buf, "aabcEDCBA", 10))
170 FAIL ();
172 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
173 FAIL ();
175 strncpy (buf + 6, "X", 4);
176 if (memcmp (buf, "aabcEDX\0\0", 10))
177 FAIL ();
179 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
180 FAIL ();
182 if (snprintf (buf + 7, 3, "%s", "987654") != 6
183 || memcmp (buf, "aabcEDX98", 10))
184 FAIL ();
186 /* These ops need runtime checking, but shouldn't __chk_fail. */
187 memcpy (buf, "abcdefghij", l0 + 10);
188 memmove (buf + 1, buf, l0 + 9);
189 if (memcmp (buf, "aabcdefghi", 10))
190 FAIL ();
192 if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
193 || memcmp (buf, "aabcdabcde", 10))
194 FAIL ();
196 memset (buf + 8, 'j', l0 + 2);
197 if (memcmp (buf, "aabcdabcjj", 10))
198 FAIL ();
200 strcpy (buf + 4, str1 + 5);
201 if (memcmp (buf, "aabcEDCBA", 10))
202 FAIL ();
204 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
205 FAIL ();
207 strncpy (buf + 6, "X", l0 + 4);
208 if (memcmp (buf, "aabcEDX\0\0", 10))
209 FAIL ();
211 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
212 || memcmp (buf, "aabcEcd\0\0", 10))
213 FAIL ();
215 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
216 FAIL ();
218 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
219 FAIL ();
221 buf[l0 + 8] = '\0';
222 strcat (buf, "A");
223 if (memcmp (buf, "aabcEcd9A", 10))
224 FAIL ();
226 buf[l0 + 7] = '\0';
227 strncat (buf, "ZYXWV", l0 + 2);
228 if (memcmp (buf, "aabcEcdZY", 10))
229 FAIL ();
231 memcpy (a.buf1, "abcdefghij", l0 + 10);
232 memmove (a.buf1 + 1, a.buf1, l0 + 9);
233 if (memcmp (a.buf1, "aabcdefghi", 10))
234 FAIL ();
236 if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
237 || memcmp (a.buf1, "aabcdabcde", 10))
238 FAIL ();
240 memset (a.buf1 + 8, 'j', l0 + 2);
241 if (memcmp (a.buf1, "aabcdabcjj", 10))
242 FAIL ();
244 #if __USE_FORTIFY_LEVEL < 2 || !__GNUC_PREREQ (4, 0)
245 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
246 and sufficient GCC support, as the string operations overflow
247 from a.buf1 into a.buf2. */
248 strcpy (a.buf1 + 4, str1 + 5);
249 if (memcmp (a.buf1, "aabcEDCBA", 10))
250 FAIL ();
252 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
253 || memcmp (a.buf1, "aabcEDCBF", 10))
254 FAIL ();
256 strncpy (a.buf1 + 6, "X", l0 + 4);
257 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
258 FAIL ();
260 if (sprintf (a.buf1 + 7, "%d", num1) != 2
261 || memcmp (a.buf1, "aabcEDX67", 10))
262 FAIL ();
264 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
265 || memcmp (a.buf1, "aabcEDX98", 10))
266 FAIL ();
268 a.buf1[l0 + 8] = '\0';
269 strcat (a.buf1, "A");
270 if (memcmp (a.buf1, "aabcEDX9A", 10))
271 FAIL ();
273 a.buf1[l0 + 7] = '\0';
274 strncat (a.buf1, "ZYXWV", l0 + 2);
275 if (memcmp (a.buf1, "aabcEDXZY", 10))
276 FAIL ();
278 #endif
280 #if __USE_FORTIFY_LEVEL >= 1
281 /* Now check if all buffer overflows are caught at runtime. */
283 CHK_FAIL_START
284 memcpy (buf + 1, "abcdefghij", l0 + 10);
285 CHK_FAIL_END
287 CHK_FAIL_START
288 memmove (buf + 2, buf + 1, l0 + 9);
289 CHK_FAIL_END
291 CHK_FAIL_START
292 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
293 CHK_FAIL_END
295 CHK_FAIL_START
296 memset (buf + 9, 'j', l0 + 2);
297 CHK_FAIL_END
299 CHK_FAIL_START
300 strcpy (buf + 5, str1 + 5);
301 CHK_FAIL_END
303 CHK_FAIL_START
304 p = stpcpy (buf + 9, str2);
305 CHK_FAIL_END
307 CHK_FAIL_START
308 strncpy (buf + 7, "X", l0 + 4);
309 CHK_FAIL_END
311 CHK_FAIL_START
312 stpncpy (buf + 6, "cd", l0 + 5);
313 CHK_FAIL_END
315 # if !defined __cplusplus || defined __va_arg_pack
316 CHK_FAIL_START
317 sprintf (buf + 8, "%d", num1);
318 CHK_FAIL_END
320 CHK_FAIL_START
321 snprintf (buf + 8, l0 + 3, "%d", num2);
322 CHK_FAIL_END
324 CHK_FAIL_START
325 swprintf (wbuf + 8, 3, L"%d", num1);
326 CHK_FAIL_END
328 CHK_FAIL_START
329 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
330 CHK_FAIL_END
331 # endif
333 memcpy (buf, str1 + 2, l0 + 9);
334 CHK_FAIL_START
335 strcat (buf, "AB");
336 CHK_FAIL_END
338 memcpy (buf, str1 + 3, l0 + 8);
339 CHK_FAIL_START
340 strncat (buf, "ZYXWV", l0 + 3);
341 CHK_FAIL_END
343 CHK_FAIL_START
344 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
345 CHK_FAIL_END
347 CHK_FAIL_START
348 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
349 CHK_FAIL_END
351 CHK_FAIL_START
352 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
353 CHK_FAIL_END
355 CHK_FAIL_START
356 memset (a.buf1 + 9, 'j', l0 + 2);
357 CHK_FAIL_END
359 # if __USE_FORTIFY_LEVEL >= 2 && __GNUC_PREREQ (4, 0)
360 # define O 0
361 # else
362 # define O 1
363 # endif
365 CHK_FAIL_START
366 strcpy (a.buf1 + (O + 4), str1 + 5);
367 CHK_FAIL_END
369 CHK_FAIL_START
370 p = stpcpy (a.buf1 + (O + 8), str2);
371 CHK_FAIL_END
373 CHK_FAIL_START
374 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
375 CHK_FAIL_END
377 # if !defined __cplusplus || defined __va_arg_pack
378 CHK_FAIL_START
379 sprintf (a.buf1 + (O + 7), "%d", num1);
380 CHK_FAIL_END
382 CHK_FAIL_START
383 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
384 CHK_FAIL_END
385 # endif
387 memcpy (a.buf1, str1 + (3 - O), l0 + 8 + O);
388 CHK_FAIL_START
389 strcat (a.buf1, "AB");
390 CHK_FAIL_END
392 memcpy (a.buf1, str1 + (4 - O), l0 + 7 + O);
393 CHK_FAIL_START
394 strncat (a.buf1, "ZYXWV", l0 + 3);
395 CHK_FAIL_END
396 #endif
399 /* These ops can be done without runtime checking of object size. */
400 wmemcpy (wbuf, L"abcdefghij", 10);
401 wmemmove (wbuf + 1, wbuf, 9);
402 if (wmemcmp (wbuf, L"aabcdefghi", 10))
403 FAIL ();
405 if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
406 || wmemcmp (wbuf, L"aabcdabcde", 10))
407 FAIL ();
409 wmemset (wbuf + 8, L'j', 2);
410 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
411 FAIL ();
413 wcscpy (wbuf + 4, L"EDCBA");
414 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
415 FAIL ();
417 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
418 FAIL ();
420 wcsncpy (wbuf + 6, L"X", 4);
421 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
422 FAIL ();
424 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
425 || wmemcmp (wbuf, L"aabcEDX98", 10))
426 FAIL ();
428 if (swprintf (wbuf + 7, 3, L"64") != 2
429 || wmemcmp (wbuf, L"aabcEDX64", 10))
430 FAIL ();
432 /* These ops need runtime checking, but shouldn't __chk_fail. */
433 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
434 wmemmove (wbuf + 1, wbuf, l0 + 9);
435 if (wmemcmp (wbuf, L"aabcdefghi", 10))
436 FAIL ();
438 if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
439 || wmemcmp (wbuf, L"aabcdabcde", 10))
440 FAIL ();
442 wmemset (wbuf + 8, L'j', l0 + 2);
443 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
444 FAIL ();
446 wcscpy (wbuf + 4, wstr1 + 5);
447 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
448 FAIL ();
450 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
451 FAIL ();
453 wcsncpy (wbuf + 6, L"X", l0 + 4);
454 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
455 FAIL ();
457 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
458 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
459 FAIL ();
461 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
462 || wmemcmp (wbuf, L"aabcEcd98", 10))
463 FAIL ();
465 wbuf[l0 + 8] = L'\0';
466 wcscat (wbuf, L"A");
467 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
468 FAIL ();
470 wbuf[l0 + 7] = L'\0';
471 wcsncat (wbuf, L"ZYXWV", l0 + 2);
472 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
473 FAIL ();
475 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
476 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
477 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
478 FAIL ();
480 if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
481 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
482 FAIL ();
484 wmemset (wa.buf1 + 8, L'j', l0 + 2);
485 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
486 FAIL ();
488 #if __USE_FORTIFY_LEVEL < 2
489 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
490 and sufficient GCC support, as the string operations overflow
491 from a.buf1 into a.buf2. */
492 wcscpy (wa.buf1 + 4, wstr1 + 5);
493 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
494 FAIL ();
496 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
497 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
498 FAIL ();
500 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
501 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
502 FAIL ();
504 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
505 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
506 FAIL ();
508 wa.buf1[l0 + 8] = L'\0';
509 wcscat (wa.buf1, L"A");
510 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
511 FAIL ();
513 wa.buf1[l0 + 7] = L'\0';
514 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
515 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
516 FAIL ();
518 #endif
520 #if __USE_FORTIFY_LEVEL >= 1
521 /* Now check if all buffer overflows are caught at runtime. */
523 CHK_FAIL_START
524 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
525 CHK_FAIL_END
527 CHK_FAIL_START
528 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
529 CHK_FAIL_END
531 CHK_FAIL_START
532 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
533 CHK_FAIL_END
535 CHK_FAIL_START
536 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
537 CHK_FAIL_END
539 CHK_FAIL_START
540 wmemset (wbuf + 9, L'j', l0 + 2);
541 CHK_FAIL_END
543 CHK_FAIL_START
544 wcscpy (wbuf + 5, wstr1 + 5);
545 CHK_FAIL_END
547 CHK_FAIL_START
548 wp = wcpcpy (wbuf + 9, wstr2);
549 CHK_FAIL_END
551 CHK_FAIL_START
552 wcsncpy (wbuf + 7, L"X", l0 + 4);
553 CHK_FAIL_END
555 CHK_FAIL_START
556 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
557 CHK_FAIL_END
559 CHK_FAIL_START
560 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
561 CHK_FAIL_END
563 CHK_FAIL_START
564 wcpncpy (wbuf + 6, L"cd", l0 + 5);
565 CHK_FAIL_END
567 wmemcpy (wbuf, wstr1 + 2, l0 + 9);
568 CHK_FAIL_START
569 wcscat (wbuf, L"AB");
570 CHK_FAIL_END
572 wmemcpy (wbuf, wstr1 + 3, l0 + 8);
573 CHK_FAIL_START
574 wcsncat (wbuf, L"ZYXWV", l0 + 3);
575 CHK_FAIL_END
577 CHK_FAIL_START
578 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
579 CHK_FAIL_END
581 CHK_FAIL_START
582 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
583 CHK_FAIL_END
585 CHK_FAIL_START
586 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
587 CHK_FAIL_END
589 CHK_FAIL_START
590 wmemset (wa.buf1 + 9, L'j', l0 + 2);
591 CHK_FAIL_END
593 #if __USE_FORTIFY_LEVEL >= 2
594 # define O 0
595 #else
596 # define O 1
597 #endif
599 CHK_FAIL_START
600 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
601 CHK_FAIL_END
603 CHK_FAIL_START
604 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
605 CHK_FAIL_END
607 CHK_FAIL_START
608 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
609 CHK_FAIL_END
611 wmemcpy (wa.buf1, wstr1 + (3 - O), l0 + 8 + O);
612 CHK_FAIL_START
613 wcscat (wa.buf1, L"AB");
614 CHK_FAIL_END
616 wmemcpy (wa.buf1, wstr1 + (4 - O), l0 + 7 + O);
617 CHK_FAIL_START
618 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
619 CHK_FAIL_END
620 #endif
623 /* Now checks for %n protection. */
625 /* Constant literals passed directly are always ok
626 (even with warnings about possible bugs from GCC). */
627 int n1, n2;
628 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
629 || n1 != 1 || n2 != 2)
630 FAIL ();
632 /* In this case the format string is not known at compile time,
633 but resides in read-only memory, so is ok. */
634 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
635 || n1 != 1 || n2 != 2)
636 FAIL ();
638 strcpy (buf2 + 2, "%n%s%n");
639 /* When the format string is writable and contains %n,
640 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
641 CHK_FAIL2_START
642 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
643 FAIL ();
644 CHK_FAIL2_END
646 CHK_FAIL2_START
647 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
648 FAIL ();
649 CHK_FAIL2_END
651 /* But if there is no %n, even writable format string
652 should work. */
653 buf2[6] = '\0';
654 if (sprintf (buf, buf2 + 4, str2) != 1)
655 FAIL ();
657 /* Constant literals passed directly are always ok
658 (even with warnings about possible bugs from GCC). */
659 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
660 || n1 != 7 || n2 != 14)
661 FAIL ();
663 /* In this case the format string is not known at compile time,
664 but resides in read-only memory, so is ok. */
665 if (printf (str3, str4, &n1, str5, &n2) != 14
666 || n1 != 7 || n2 != 14)
667 FAIL ();
669 strcpy (buf2 + 2, "%n%s%n");
670 /* When the format string is writable and contains %n,
671 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
672 CHK_FAIL2_START
673 if (printf (buf2, str4, &n1, str5, &n1) != 14)
674 FAIL ();
675 CHK_FAIL2_END
677 /* But if there is no %n, even writable format string
678 should work. */
679 buf2[6] = '\0';
680 if (printf (buf2 + 4, str5) != 7)
681 FAIL ();
683 FILE *fp = stdout;
685 /* Constant literals passed directly are always ok
686 (even with warnings about possible bugs from GCC). */
687 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
688 || n1 != 7 || n2 != 14)
689 FAIL ();
691 /* In this case the format string is not known at compile time,
692 but resides in read-only memory, so is ok. */
693 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
694 || n1 != 7 || n2 != 14)
695 FAIL ();
697 strcpy (buf2 + 2, "%n%s%n");
698 /* When the format string is writable and contains %n,
699 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
700 CHK_FAIL2_START
701 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
702 FAIL ();
703 CHK_FAIL2_END
705 /* But if there is no %n, even writable format string
706 should work. */
707 buf2[6] = '\0';
708 if (fprintf (fp, buf2 + 4, str5) != 7)
709 FAIL ();
711 if (freopen (temp_filename, "r", stdin) == NULL)
713 puts ("could not open temporary file");
714 exit (1);
717 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
718 FAIL ();
719 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
720 FAIL ();
722 #if __USE_FORTIFY_LEVEL >= 1
723 CHK_FAIL_START
724 if (gets (buf) != buf)
725 FAIL ();
726 CHK_FAIL_END
727 #endif
729 rewind (stdin);
731 if (fgets (buf, sizeof (buf), stdin) != buf
732 || memcmp (buf, "abcdefgh\n", 10))
733 FAIL ();
734 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
735 FAIL ();
737 rewind (stdin);
739 if (fgets (buf, l0 + sizeof (buf), stdin) != buf
740 || memcmp (buf, "abcdefgh\n", 10))
741 FAIL ();
743 #if __USE_FORTIFY_LEVEL >= 1
744 CHK_FAIL_START
745 if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
746 FAIL ();
747 CHK_FAIL_END
749 CHK_FAIL_START
750 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
751 FAIL ();
752 CHK_FAIL_END
753 #endif
755 rewind (stdin);
757 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
758 || memcmp (buf, "abcdefgh\n", 10))
759 FAIL ();
760 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
761 || memcmp (buf, "ABCDEFGHI", 10))
762 FAIL ();
764 rewind (stdin);
766 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
767 || memcmp (buf, "abcdefgh\n", 10))
768 FAIL ();
770 #if __USE_FORTIFY_LEVEL >= 1
771 CHK_FAIL_START
772 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
773 FAIL ();
774 CHK_FAIL_END
776 CHK_FAIL_START
777 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
778 FAIL ();
779 CHK_FAIL_END
780 #endif
782 rewind (stdin);
784 if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
785 || memcmp (buf, "abcdefgh\nA", 10))
786 FAIL ();
787 if (fread (buf, sizeof (buf), 1, stdin) != 1
788 || memcmp (buf, "BCDEFGHI\na", 10))
789 FAIL ();
791 rewind (stdin);
793 if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
794 || memcmp (buf, "abcdefgh\nA", 10))
795 FAIL ();
796 if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
797 || memcmp (buf, "BCDEFGHI\na", 10))
798 FAIL ();
800 #if __USE_FORTIFY_LEVEL >= 1
801 CHK_FAIL_START
802 if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
803 FAIL ();
804 CHK_FAIL_END
806 CHK_FAIL_START
807 if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
808 FAIL ();
809 CHK_FAIL_END
810 #endif
812 rewind (stdin);
814 if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
815 || memcmp (buf, "abcdefgh\nA", 10))
816 FAIL ();
817 if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
818 || memcmp (buf, "BCDEFGHI\na", 10))
819 FAIL ();
821 rewind (stdin);
823 if (fread_unlocked (buf, 1, 4, stdin) != 4
824 || memcmp (buf, "abcdFGHI\na", 10))
825 FAIL ();
826 if (fread_unlocked (buf, 4, 1, stdin) != 1
827 || memcmp (buf, "efghFGHI\na", 10))
828 FAIL ();
830 rewind (stdin);
832 if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
833 || memcmp (buf, "abcdefgh\nA", 10))
834 FAIL ();
835 if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
836 || memcmp (buf, "BCDEFGHI\na", 10))
837 FAIL ();
839 #if __USE_FORTIFY_LEVEL >= 1
840 CHK_FAIL_START
841 if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
842 FAIL ();
843 CHK_FAIL_END
845 CHK_FAIL_START
846 if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
847 FAIL ();
848 CHK_FAIL_END
849 #endif
851 lseek (fileno (stdin), 0, SEEK_SET);
853 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
854 || memcmp (buf, "abcdefgh\n", 9))
855 FAIL ();
856 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
857 || memcmp (buf, "ABCDEFGHI", 9))
858 FAIL ();
860 lseek (fileno (stdin), 0, SEEK_SET);
862 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
863 || memcmp (buf, "abcdefgh\n", 9))
864 FAIL ();
866 #if __USE_FORTIFY_LEVEL >= 1
867 CHK_FAIL_START
868 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
869 FAIL ();
870 CHK_FAIL_END
871 #endif
873 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
874 != sizeof (buf) - 1
875 || memcmp (buf, "\nABCDEFGH", 9))
876 FAIL ();
877 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
878 || memcmp (buf, "abcdefgh\n", 9))
879 FAIL ();
880 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
881 != sizeof (buf) - 1
882 || memcmp (buf, "h\nABCDEFG", 9))
883 FAIL ();
885 #if __USE_FORTIFY_LEVEL >= 1
886 CHK_FAIL_START
887 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
888 != sizeof (buf) + 1)
889 FAIL ();
890 CHK_FAIL_END
891 #endif
893 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
894 != sizeof (buf) - 1
895 || memcmp (buf, "\nABCDEFGH", 9))
896 FAIL ();
897 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
898 || memcmp (buf, "abcdefgh\n", 9))
899 FAIL ();
900 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
901 != sizeof (buf) - 1
902 || memcmp (buf, "h\nABCDEFG", 9))
903 FAIL ();
905 #if __USE_FORTIFY_LEVEL >= 1
906 CHK_FAIL_START
907 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
908 != sizeof (buf) + 1)
909 FAIL ();
910 CHK_FAIL_END
911 #endif
913 if (freopen (temp_filename, "r", stdin) == NULL)
915 puts ("could not open temporary file");
916 exit (1);
919 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
921 puts ("could not seek in test file");
922 exit (1);
925 #if __USE_FORTIFY_LEVEL >= 1
926 CHK_FAIL_START
927 if (gets (buf) != buf)
928 FAIL ();
929 CHK_FAIL_END
930 #endif
932 /* Check whether missing N$ formats are detected. */
933 CHK_FAIL2_START
934 printf ("%3$d\n", 1, 2, 3, 4);
935 CHK_FAIL2_END
937 CHK_FAIL2_START
938 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
939 CHK_FAIL2_END
941 CHK_FAIL2_START
942 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
943 CHK_FAIL2_END
945 CHK_FAIL2_START
946 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
947 CHK_FAIL2_END
949 int sp[2];
950 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
951 FAIL ();
952 else
954 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
955 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
956 != strlen (sendstr))
957 FAIL ();
959 char recvbuf[12];
960 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
961 != sizeof recvbuf
962 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
963 FAIL ();
965 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
966 != sizeof recvbuf - 7
967 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
968 FAIL ();
970 #if __USE_FORTIFY_LEVEL >= 1
971 CHK_FAIL_START
972 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
973 != sizeof recvbuf)
974 FAIL ();
975 CHK_FAIL_END
977 CHK_FAIL_START
978 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
979 != sizeof recvbuf - 3)
980 FAIL ();
981 CHK_FAIL_END
982 #endif
984 socklen_t sl;
985 struct sockaddr_un sa_un;
987 sl = sizeof (sa_un);
988 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
989 (struct sockaddr *) &sa_un, &sl)
990 != sizeof recvbuf
991 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
992 FAIL ();
994 sl = sizeof (sa_un);
995 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
996 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
997 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
998 FAIL ();
1000 #if __USE_FORTIFY_LEVEL >= 1
1001 CHK_FAIL_START
1002 sl = sizeof (sa_un);
1003 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1004 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1005 FAIL ();
1006 CHK_FAIL_END
1008 CHK_FAIL_START
1009 sl = sizeof (sa_un);
1010 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1011 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1012 FAIL ();
1013 CHK_FAIL_END
1014 #endif
1016 close (sp[0]);
1017 close (sp[1]);
1020 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1021 char *enddir = strchr (fname, '\0');
1022 if (mkdtemp (fname) == NULL)
1024 printf ("mkdtemp failed: %m\n");
1025 return 1;
1027 *enddir = '/';
1028 if (symlink ("bar", fname) != 0)
1029 FAIL ();
1031 char readlinkbuf[4];
1032 if (readlink (fname, readlinkbuf, 4) != 3
1033 || memcmp (readlinkbuf, "bar", 3) != 0)
1034 FAIL ();
1035 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1036 || memcmp (readlinkbuf, "bbar", 4) != 0)
1037 FAIL ();
1039 #if __USE_FORTIFY_LEVEL >= 1
1040 CHK_FAIL_START
1041 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1042 FAIL ();
1043 CHK_FAIL_END
1045 CHK_FAIL_START
1046 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1047 FAIL ();
1048 CHK_FAIL_END
1049 #endif
1051 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1052 if (tmpfd < 0)
1053 FAIL ();
1055 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1056 || memcmp (readlinkbuf, "bar", 3) != 0)
1057 FAIL ();
1058 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1059 l0 + 3) != 3
1060 || memcmp (readlinkbuf, "bbar", 4) != 0)
1061 FAIL ();
1063 #if __USE_FORTIFY_LEVEL >= 1
1064 CHK_FAIL_START
1065 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1066 l0 + 3) != 3)
1067 FAIL ();
1068 CHK_FAIL_END
1070 CHK_FAIL_START
1071 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1072 4) != 3)
1073 FAIL ();
1074 CHK_FAIL_END
1075 #endif
1077 close (tmpfd);
1079 char *cwd1 = getcwd (NULL, 0);
1080 if (cwd1 == NULL)
1081 FAIL ();
1083 char *cwd2 = getcwd (NULL, 250);
1084 if (cwd2 == NULL)
1085 FAIL ();
1087 if (cwd1 && cwd2)
1089 if (strcmp (cwd1, cwd2) != 0)
1090 FAIL ();
1092 *enddir = '\0';
1093 if (chdir (fname))
1094 FAIL ();
1096 char *cwd3 = getcwd (NULL, 0);
1097 if (cwd3 == NULL)
1098 FAIL ();
1099 if (strcmp (fname, cwd3) != 0)
1100 printf ("getcwd after chdir is '%s' != '%s',"
1101 "get{c,}wd tests skipped\n", cwd3, fname);
1102 else
1104 char getcwdbuf[sizeof fname - 3];
1106 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1107 if (cwd4 != getcwdbuf
1108 || strcmp (getcwdbuf, fname) != 0)
1109 FAIL ();
1111 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1112 if (cwd4 != getcwdbuf + 1
1113 || getcwdbuf[0] != fname[0]
1114 || strcmp (getcwdbuf + 1, fname) != 0)
1115 FAIL ();
1117 #if __USE_FORTIFY_LEVEL >= 1
1118 CHK_FAIL_START
1119 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1120 != getcwdbuf + 2)
1121 FAIL ();
1122 CHK_FAIL_END
1124 CHK_FAIL_START
1125 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1126 != getcwdbuf + 2)
1127 FAIL ();
1128 CHK_FAIL_END
1129 #endif
1131 if (getwd (getcwdbuf) != getcwdbuf
1132 || strcmp (getcwdbuf, fname) != 0)
1133 FAIL ();
1135 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1136 || strcmp (getcwdbuf + 1, fname) != 0)
1137 FAIL ();
1139 #if __USE_FORTIFY_LEVEL >= 1
1140 CHK_FAIL_START
1141 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1142 FAIL ();
1143 CHK_FAIL_END
1144 #endif
1147 if (chdir (cwd1) != 0)
1148 FAIL ();
1149 free (cwd3);
1152 free (cwd1);
1153 free (cwd2);
1154 *enddir = '/';
1155 if (unlink (fname) != 0)
1156 FAIL ();
1158 *enddir = '\0';
1159 if (rmdir (fname) != 0)
1160 FAIL ();
1163 #if PATH_MAX > 0
1164 char largebuf[PATH_MAX];
1165 char *realres = realpath (".", largebuf);
1166 if (realres != largebuf)
1167 FAIL ();
1169 # if __USE_FORTIFY_LEVEL >= 1
1170 CHK_FAIL_START
1171 char realbuf[1];
1172 realres = realpath (".", realbuf);
1173 if (realres != realbuf)
1174 FAIL ();
1175 CHK_FAIL_END
1176 # endif
1177 #endif
1179 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1181 assert (MB_CUR_MAX <= 10);
1183 /* First a simple test. */
1184 char enough[10];
1185 if (wctomb (enough, L'A') != 1)
1186 FAIL ();
1188 #if __USE_FORTIFY_LEVEL >= 1
1189 /* We know the wchar_t encoding is ISO 10646. So pick a
1190 character which has a multibyte representation which does not
1191 fit. */
1192 CHK_FAIL_START
1193 char smallbuf[2];
1194 if (wctomb (smallbuf, L'\x100') != 2)
1195 FAIL ();
1196 CHK_FAIL_END
1197 #endif
1199 mbstate_t s;
1200 memset (&s, '\0', sizeof (s));
1201 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1202 FAIL ();
1204 #if __USE_FORTIFY_LEVEL >= 1
1205 /* We know the wchar_t encoding is ISO 10646. So pick a
1206 character which has a multibyte representation which does not
1207 fit. */
1208 CHK_FAIL_START
1209 char smallbuf[2];
1210 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1211 FAIL ();
1212 CHK_FAIL_END
1213 #endif
1215 wchar_t wenough[10];
1216 memset (&s, '\0', sizeof (s));
1217 const char *cp = "A";
1218 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1219 || wcscmp (wenough, L"A") != 0)
1220 FAIL ();
1222 cp = "BC";
1223 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1224 || wcscmp (wenough, L"BC") != 0)
1225 FAIL ();
1227 #if __USE_FORTIFY_LEVEL >= 1
1228 CHK_FAIL_START
1229 wchar_t wsmallbuf[2];
1230 cp = "ABC";
1231 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1232 CHK_FAIL_END
1233 #endif
1235 cp = "A";
1236 if (mbstowcs (wenough, cp, 10) != 1
1237 || wcscmp (wenough, L"A") != 0)
1238 FAIL ();
1240 cp = "DEF";
1241 if (mbstowcs (wenough, cp, l0 + 10) != 3
1242 || wcscmp (wenough, L"DEF") != 0)
1243 FAIL ();
1245 #if __USE_FORTIFY_LEVEL >= 1
1246 CHK_FAIL_START
1247 wchar_t wsmallbuf[2];
1248 cp = "ABC";
1249 mbstowcs (wsmallbuf, cp, 10);
1250 CHK_FAIL_END
1251 #endif
1253 memset (&s, '\0', sizeof (s));
1254 cp = "ABC";
1255 wcscpy (wenough, L"DEF");
1256 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1257 || wcscmp (wenough, L"AEF") != 0)
1258 FAIL ();
1260 cp = "IJ";
1261 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1262 || wcscmp (wenough, L"IEF") != 0)
1263 FAIL ();
1265 #if __USE_FORTIFY_LEVEL >= 1
1266 CHK_FAIL_START
1267 wchar_t wsmallbuf[2];
1268 cp = "ABC";
1269 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1270 CHK_FAIL_END
1271 #endif
1273 memset (&s, '\0', sizeof (s));
1274 const wchar_t *wcp = L"A";
1275 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1276 || strcmp (enough, "A") != 0)
1277 FAIL ();
1279 wcp = L"BC";
1280 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1281 || strcmp (enough, "BC") != 0)
1282 FAIL ();
1284 #if __USE_FORTIFY_LEVEL >= 1
1285 CHK_FAIL_START
1286 char smallbuf[2];
1287 wcp = L"ABC";
1288 wcsrtombs (smallbuf, &wcp, 10, &s);
1289 CHK_FAIL_END
1290 #endif
1292 memset (enough, 'Z', sizeof (enough));
1293 wcp = L"EF";
1294 if (wcstombs (enough, wcp, 10) != 2
1295 || strcmp (enough, "EF") != 0)
1296 FAIL ();
1298 wcp = L"G";
1299 if (wcstombs (enough, wcp, l0 + 10) != 1
1300 || strcmp (enough, "G") != 0)
1301 FAIL ();
1303 #if __USE_FORTIFY_LEVEL >= 1
1304 CHK_FAIL_START
1305 char smallbuf[2];
1306 wcp = L"ABC";
1307 wcstombs (smallbuf, wcp, 10);
1308 CHK_FAIL_END
1309 #endif
1311 memset (&s, '\0', sizeof (s));
1312 wcp = L"AB";
1313 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1314 || strcmp (enough, "A") != 0)
1315 FAIL ();
1317 wcp = L"BCD";
1318 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1319 || strcmp (enough, "B") != 0)
1320 FAIL ();
1322 #if __USE_FORTIFY_LEVEL >= 1
1323 CHK_FAIL_START
1324 char smallbuf[2];
1325 wcp = L"ABC";
1326 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1327 CHK_FAIL_END
1328 #endif
1330 else
1332 puts ("cannot set locale");
1333 ret = 1;
1336 fd = posix_openpt (O_RDWR);
1337 if (fd != -1)
1339 char enough[1000];
1340 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1341 FAIL ();
1343 #if __USE_FORTIFY_LEVEL >= 1
1344 CHK_FAIL_START
1345 char smallbuf[2];
1346 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1347 FAIL ();
1348 CHK_FAIL_END
1349 #endif
1350 close (fd);
1353 #if PATH_MAX > 0
1354 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1355 # if __USE_FORTIFY_LEVEL >= 1
1356 CHK_FAIL_START
1357 char smallbuf[1];
1358 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1359 CHK_FAIL_END
1360 # endif
1361 #endif
1363 gid_t grpslarge[5];
1364 int ngr = getgroups (5, grpslarge);
1365 asm volatile ("" : : "r" (ngr));
1366 #if __USE_FORTIFY_LEVEL >= 1
1367 CHK_FAIL_START
1368 char smallbuf[1];
1369 ngr = getgroups (5, (gid_t *) smallbuf);
1370 asm volatile ("" : : "r" (ngr));
1371 CHK_FAIL_END
1372 #endif
1374 fd = open (_PATH_TTY, O_RDONLY);
1375 if (fd != -1)
1377 char enough[1000];
1378 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1379 FAIL ();
1381 #if __USE_FORTIFY_LEVEL >= 1
1382 CHK_FAIL_START
1383 char smallbuf[2];
1384 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1385 FAIL ();
1386 CHK_FAIL_END
1387 #endif
1388 close (fd);
1391 char hostnamelarge[1000];
1392 gethostname (hostnamelarge, sizeof (hostnamelarge));
1393 #if __USE_FORTIFY_LEVEL >= 1
1394 CHK_FAIL_START
1395 char smallbuf[1];
1396 gethostname (smallbuf, sizeof (hostnamelarge));
1397 CHK_FAIL_END
1398 #endif
1400 char loginlarge[1000];
1401 getlogin_r (loginlarge, sizeof (hostnamelarge));
1402 #if __USE_FORTIFY_LEVEL >= 1
1403 CHK_FAIL_START
1404 char smallbuf[1];
1405 getlogin_r (smallbuf, sizeof (loginlarge));
1406 CHK_FAIL_END
1407 #endif
1409 char domainnamelarge[1000];
1410 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1411 asm volatile ("" : : "r" (res));
1412 #if __USE_FORTIFY_LEVEL >= 1
1413 CHK_FAIL_START
1414 char smallbuf[1];
1415 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1416 asm volatile ("" : : "r" (res));
1417 CHK_FAIL_END
1418 #endif
1420 return ret;