Fix AIO when thread creation failed
[glibc.git] / debug / tst-chk1.c
blob9b692329217db8968b62886cf2b8b1a28b097440
1 /* Copyright (C) 2004, 2005, 2006, 2007, 2008 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 <obstack.h>
27 #include <paths.h>
28 #include <setjmp.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <wchar.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
38 #define obstack_chunk_alloc malloc
39 #define obstack_chunk_free free
41 char *temp_filename;
42 static void do_prepare (void);
43 static int do_test (void);
44 #define PREPARE(argc, argv) do_prepare ()
45 #define TEST_FUNCTION do_test ()
46 #include "../test-skeleton.c"
48 static void
49 do_prepare (void)
51 int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
52 if (temp_fd == -1)
54 printf ("cannot create temporary file: %m\n");
55 exit (1);
58 const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
59 if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
61 puts ("could not write test strings into file");
62 unlink (temp_filename);
63 exit (1);
67 volatile int chk_fail_ok;
68 volatile int ret;
69 jmp_buf chk_fail_buf;
71 static void
72 handler (int sig)
74 if (chk_fail_ok)
76 chk_fail_ok = 0;
77 longjmp (chk_fail_buf, 1);
79 else
80 _exit (127);
83 char buf[10];
84 wchar_t wbuf[10];
85 volatile size_t l0;
86 volatile char *p;
87 volatile wchar_t *wp;
88 const char *str1 = "JIHGFEDCBA";
89 const char *str2 = "F";
90 const char *str3 = "%s%n%s%n";
91 const char *str4 = "Hello, ";
92 const char *str5 = "World!\n";
93 const wchar_t *wstr1 = L"JIHGFEDCBA";
94 const wchar_t *wstr2 = L"F";
95 const wchar_t *wstr3 = L"%s%n%s%n";
96 const wchar_t *wstr4 = L"Hello, ";
97 const wchar_t *wstr5 = L"World!\n";
98 char buf2[10] = "%s";
99 int num1 = 67;
100 int num2 = 987654;
102 #define FAIL() \
103 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
104 #define CHK_FAIL_START \
105 chk_fail_ok = 1; \
106 if (! setjmp (chk_fail_buf)) \
108 #define CHK_FAIL_END \
109 chk_fail_ok = 0; \
110 FAIL (); \
112 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
113 #define CHK_FAIL2_START CHK_FAIL_START
114 #define CHK_FAIL2_END CHK_FAIL_END
115 #else
116 #define CHK_FAIL2_START
117 #define CHK_FAIL2_END
118 #endif
120 static int
121 do_test (void)
123 struct sigaction sa;
124 sa.sa_handler = handler;
125 sa.sa_flags = 0;
126 sigemptyset (&sa.sa_mask);
128 sigaction (SIGABRT, &sa, NULL);
130 /* Avoid all the buffer overflow messages on stderr. */
131 int fd = open (_PATH_DEVNULL, O_WRONLY);
132 if (fd == -1)
133 close (STDERR_FILENO);
134 else
136 dup2 (fd, STDERR_FILENO);
137 close (fd);
139 setenv ("LIBC_FATAL_STDERR_", "1", 1);
141 struct A { char buf1[9]; char buf2[1]; } a;
142 struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
144 printf ("Test checking routines at fortify level %d\n",
145 #ifdef __USE_FORTIFY_LEVEL
146 (int) __USE_FORTIFY_LEVEL
147 #else
149 #endif
152 #if defined __USE_FORTIFY_LEVEL && !defined __extern_always_inline
153 printf ("Test skipped");
154 if (l0 == 0)
155 return 0;
156 #endif
158 /* These ops can be done without runtime checking of object size. */
159 memcpy (buf, "abcdefghij", 10);
160 memmove (buf + 1, buf, 9);
161 if (memcmp (buf, "aabcdefghi", 10))
162 FAIL ();
164 if (mempcpy (buf + 5, "abcde", 5) != buf + 10
165 || memcmp (buf, "aabcdabcde", 10))
166 FAIL ();
168 memset (buf + 8, 'j', 2);
169 if (memcmp (buf, "aabcdabcjj", 10))
170 FAIL ();
172 strcpy (buf + 4, "EDCBA");
173 if (memcmp (buf, "aabcEDCBA", 10))
174 FAIL ();
176 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
177 FAIL ();
179 strncpy (buf + 6, "X", 4);
180 if (memcmp (buf, "aabcEDX\0\0", 10))
181 FAIL ();
183 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
184 FAIL ();
186 if (snprintf (buf + 7, 3, "%s", "987654") != 6
187 || memcmp (buf, "aabcEDX98", 10))
188 FAIL ();
190 /* These ops need runtime checking, but shouldn't __chk_fail. */
191 memcpy (buf, "abcdefghij", l0 + 10);
192 memmove (buf + 1, buf, l0 + 9);
193 if (memcmp (buf, "aabcdefghi", 10))
194 FAIL ();
196 if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
197 || memcmp (buf, "aabcdabcde", 10))
198 FAIL ();
200 memset (buf + 8, 'j', l0 + 2);
201 if (memcmp (buf, "aabcdabcjj", 10))
202 FAIL ();
204 strcpy (buf + 4, str1 + 5);
205 if (memcmp (buf, "aabcEDCBA", 10))
206 FAIL ();
208 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
209 FAIL ();
211 strncpy (buf + 6, "X", l0 + 4);
212 if (memcmp (buf, "aabcEDX\0\0", 10))
213 FAIL ();
215 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
216 || memcmp (buf, "aabcEcd\0\0", 10))
217 FAIL ();
219 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
220 FAIL ();
222 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
223 FAIL ();
225 buf[l0 + 8] = '\0';
226 strcat (buf, "A");
227 if (memcmp (buf, "aabcEcd9A", 10))
228 FAIL ();
230 buf[l0 + 7] = '\0';
231 strncat (buf, "ZYXWV", l0 + 2);
232 if (memcmp (buf, "aabcEcdZY", 10))
233 FAIL ();
235 memcpy (a.buf1, "abcdefghij", l0 + 10);
236 memmove (a.buf1 + 1, a.buf1, l0 + 9);
237 if (memcmp (a.buf1, "aabcdefghi", 10))
238 FAIL ();
240 if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
241 || memcmp (a.buf1, "aabcdabcde", 10))
242 FAIL ();
244 memset (a.buf1 + 8, 'j', l0 + 2);
245 if (memcmp (a.buf1, "aabcdabcjj", 10))
246 FAIL ();
248 #if __USE_FORTIFY_LEVEL < 2 || !__GNUC_PREREQ (4, 0)
249 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
250 and sufficient GCC support, as the string operations overflow
251 from a.buf1 into a.buf2. */
252 strcpy (a.buf1 + 4, str1 + 5);
253 if (memcmp (a.buf1, "aabcEDCBA", 10))
254 FAIL ();
256 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
257 || memcmp (a.buf1, "aabcEDCBF", 10))
258 FAIL ();
260 strncpy (a.buf1 + 6, "X", l0 + 4);
261 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
262 FAIL ();
264 if (sprintf (a.buf1 + 7, "%d", num1) != 2
265 || memcmp (a.buf1, "aabcEDX67", 10))
266 FAIL ();
268 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
269 || memcmp (a.buf1, "aabcEDX98", 10))
270 FAIL ();
272 a.buf1[l0 + 8] = '\0';
273 strcat (a.buf1, "A");
274 if (memcmp (a.buf1, "aabcEDX9A", 10))
275 FAIL ();
277 a.buf1[l0 + 7] = '\0';
278 strncat (a.buf1, "ZYXWV", l0 + 2);
279 if (memcmp (a.buf1, "aabcEDXZY", 10))
280 FAIL ();
282 #endif
284 #if __USE_FORTIFY_LEVEL >= 1
285 /* Now check if all buffer overflows are caught at runtime. */
287 CHK_FAIL_START
288 memcpy (buf + 1, "abcdefghij", l0 + 10);
289 CHK_FAIL_END
291 CHK_FAIL_START
292 memmove (buf + 2, buf + 1, l0 + 9);
293 CHK_FAIL_END
295 CHK_FAIL_START
296 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
297 CHK_FAIL_END
299 CHK_FAIL_START
300 memset (buf + 9, 'j', l0 + 2);
301 CHK_FAIL_END
303 CHK_FAIL_START
304 strcpy (buf + 5, str1 + 5);
305 CHK_FAIL_END
307 CHK_FAIL_START
308 p = stpcpy (buf + 9, str2);
309 CHK_FAIL_END
311 CHK_FAIL_START
312 strncpy (buf + 7, "X", l0 + 4);
313 CHK_FAIL_END
315 CHK_FAIL_START
316 stpncpy (buf + 6, "cd", l0 + 5);
317 CHK_FAIL_END
319 # if !defined __cplusplus || defined __va_arg_pack
320 CHK_FAIL_START
321 sprintf (buf + 8, "%d", num1);
322 CHK_FAIL_END
324 CHK_FAIL_START
325 snprintf (buf + 8, l0 + 3, "%d", num2);
326 CHK_FAIL_END
328 CHK_FAIL_START
329 swprintf (wbuf + 8, 3, L"%d", num1);
330 CHK_FAIL_END
332 CHK_FAIL_START
333 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
334 CHK_FAIL_END
335 # endif
337 memcpy (buf, str1 + 2, l0 + 9);
338 CHK_FAIL_START
339 strcat (buf, "AB");
340 CHK_FAIL_END
342 memcpy (buf, str1 + 3, l0 + 8);
343 CHK_FAIL_START
344 strncat (buf, "ZYXWV", l0 + 3);
345 CHK_FAIL_END
347 CHK_FAIL_START
348 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
349 CHK_FAIL_END
351 CHK_FAIL_START
352 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
353 CHK_FAIL_END
355 CHK_FAIL_START
356 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
357 CHK_FAIL_END
359 CHK_FAIL_START
360 memset (a.buf1 + 9, 'j', l0 + 2);
361 CHK_FAIL_END
363 # if __USE_FORTIFY_LEVEL >= 2 && __GNUC_PREREQ (4, 0)
364 # define O 0
365 # else
366 # define O 1
367 # endif
369 CHK_FAIL_START
370 strcpy (a.buf1 + (O + 4), str1 + 5);
371 CHK_FAIL_END
373 CHK_FAIL_START
374 p = stpcpy (a.buf1 + (O + 8), str2);
375 CHK_FAIL_END
377 CHK_FAIL_START
378 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
379 CHK_FAIL_END
381 # if !defined __cplusplus || defined __va_arg_pack
382 CHK_FAIL_START
383 sprintf (a.buf1 + (O + 7), "%d", num1);
384 CHK_FAIL_END
386 CHK_FAIL_START
387 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
388 CHK_FAIL_END
389 # endif
391 memcpy (a.buf1, str1 + (3 - O), l0 + 8 + O);
392 CHK_FAIL_START
393 strcat (a.buf1, "AB");
394 CHK_FAIL_END
396 memcpy (a.buf1, str1 + (4 - O), l0 + 7 + O);
397 CHK_FAIL_START
398 strncat (a.buf1, "ZYXWV", l0 + 3);
399 CHK_FAIL_END
400 #endif
403 /* These ops can be done without runtime checking of object size. */
404 wmemcpy (wbuf, L"abcdefghij", 10);
405 wmemmove (wbuf + 1, wbuf, 9);
406 if (wmemcmp (wbuf, L"aabcdefghi", 10))
407 FAIL ();
409 if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
410 || wmemcmp (wbuf, L"aabcdabcde", 10))
411 FAIL ();
413 wmemset (wbuf + 8, L'j', 2);
414 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
415 FAIL ();
417 wcscpy (wbuf + 4, L"EDCBA");
418 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
419 FAIL ();
421 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
422 FAIL ();
424 wcsncpy (wbuf + 6, L"X", 4);
425 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
426 FAIL ();
428 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
429 || wmemcmp (wbuf, L"aabcEDX98", 10))
430 FAIL ();
432 if (swprintf (wbuf + 7, 3, L"64") != 2
433 || wmemcmp (wbuf, L"aabcEDX64", 10))
434 FAIL ();
436 /* These ops need runtime checking, but shouldn't __chk_fail. */
437 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
438 wmemmove (wbuf + 1, wbuf, l0 + 9);
439 if (wmemcmp (wbuf, L"aabcdefghi", 10))
440 FAIL ();
442 if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
443 || wmemcmp (wbuf, L"aabcdabcde", 10))
444 FAIL ();
446 wmemset (wbuf + 8, L'j', l0 + 2);
447 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
448 FAIL ();
450 wcscpy (wbuf + 4, wstr1 + 5);
451 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
452 FAIL ();
454 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
455 FAIL ();
457 wcsncpy (wbuf + 6, L"X", l0 + 4);
458 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
459 FAIL ();
461 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
462 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
463 FAIL ();
465 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
466 || wmemcmp (wbuf, L"aabcEcd98", 10))
467 FAIL ();
469 wbuf[l0 + 8] = L'\0';
470 wcscat (wbuf, L"A");
471 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
472 FAIL ();
474 wbuf[l0 + 7] = L'\0';
475 wcsncat (wbuf, L"ZYXWV", l0 + 2);
476 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
477 FAIL ();
479 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
480 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
481 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
482 FAIL ();
484 if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
485 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
486 FAIL ();
488 wmemset (wa.buf1 + 8, L'j', l0 + 2);
489 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
490 FAIL ();
492 #if __USE_FORTIFY_LEVEL < 2
493 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
494 and sufficient GCC support, as the string operations overflow
495 from a.buf1 into a.buf2. */
496 wcscpy (wa.buf1 + 4, wstr1 + 5);
497 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
498 FAIL ();
500 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
501 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
502 FAIL ();
504 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
505 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
506 FAIL ();
508 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
509 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
510 FAIL ();
512 wa.buf1[l0 + 8] = L'\0';
513 wcscat (wa.buf1, L"A");
514 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
515 FAIL ();
517 wa.buf1[l0 + 7] = L'\0';
518 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
519 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
520 FAIL ();
522 #endif
524 #if __USE_FORTIFY_LEVEL >= 1
525 /* Now check if all buffer overflows are caught at runtime. */
527 CHK_FAIL_START
528 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
529 CHK_FAIL_END
531 CHK_FAIL_START
532 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
533 CHK_FAIL_END
535 CHK_FAIL_START
536 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
537 CHK_FAIL_END
539 CHK_FAIL_START
540 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
541 CHK_FAIL_END
543 CHK_FAIL_START
544 wmemset (wbuf + 9, L'j', l0 + 2);
545 CHK_FAIL_END
547 CHK_FAIL_START
548 wcscpy (wbuf + 5, wstr1 + 5);
549 CHK_FAIL_END
551 CHK_FAIL_START
552 wp = wcpcpy (wbuf + 9, wstr2);
553 CHK_FAIL_END
555 CHK_FAIL_START
556 wcsncpy (wbuf + 7, L"X", l0 + 4);
557 CHK_FAIL_END
559 CHK_FAIL_START
560 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
561 CHK_FAIL_END
563 CHK_FAIL_START
564 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
565 CHK_FAIL_END
567 CHK_FAIL_START
568 wcpncpy (wbuf + 6, L"cd", l0 + 5);
569 CHK_FAIL_END
571 wmemcpy (wbuf, wstr1 + 2, l0 + 9);
572 CHK_FAIL_START
573 wcscat (wbuf, L"AB");
574 CHK_FAIL_END
576 wmemcpy (wbuf, wstr1 + 3, l0 + 8);
577 CHK_FAIL_START
578 wcsncat (wbuf, L"ZYXWV", l0 + 3);
579 CHK_FAIL_END
581 CHK_FAIL_START
582 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
583 CHK_FAIL_END
585 CHK_FAIL_START
586 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
587 CHK_FAIL_END
589 CHK_FAIL_START
590 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
591 CHK_FAIL_END
593 CHK_FAIL_START
594 wmemset (wa.buf1 + 9, L'j', l0 + 2);
595 CHK_FAIL_END
597 #if __USE_FORTIFY_LEVEL >= 2
598 # define O 0
599 #else
600 # define O 1
601 #endif
603 CHK_FAIL_START
604 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
605 CHK_FAIL_END
607 CHK_FAIL_START
608 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
609 CHK_FAIL_END
611 CHK_FAIL_START
612 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
613 CHK_FAIL_END
615 wmemcpy (wa.buf1, wstr1 + (3 - O), l0 + 8 + O);
616 CHK_FAIL_START
617 wcscat (wa.buf1, L"AB");
618 CHK_FAIL_END
620 wmemcpy (wa.buf1, wstr1 + (4 - O), l0 + 7 + O);
621 CHK_FAIL_START
622 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
623 CHK_FAIL_END
624 #endif
627 /* Now checks for %n protection. */
629 /* Constant literals passed directly are always ok
630 (even with warnings about possible bugs from GCC). */
631 int n1, n2;
632 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
633 || n1 != 1 || n2 != 2)
634 FAIL ();
636 /* In this case the format string is not known at compile time,
637 but resides in read-only memory, so is ok. */
638 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
639 || n1 != 1 || n2 != 2)
640 FAIL ();
642 strcpy (buf2 + 2, "%n%s%n");
643 /* When the format string is writable and contains %n,
644 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
645 CHK_FAIL2_START
646 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
647 FAIL ();
648 CHK_FAIL2_END
650 CHK_FAIL2_START
651 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
652 FAIL ();
653 CHK_FAIL2_END
655 /* But if there is no %n, even writable format string
656 should work. */
657 buf2[6] = '\0';
658 if (sprintf (buf, buf2 + 4, str2) != 1)
659 FAIL ();
661 /* Constant literals passed directly are always ok
662 (even with warnings about possible bugs from GCC). */
663 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
664 || n1 != 7 || n2 != 14)
665 FAIL ();
667 /* In this case the format string is not known at compile time,
668 but resides in read-only memory, so is ok. */
669 if (printf (str3, str4, &n1, str5, &n2) != 14
670 || n1 != 7 || n2 != 14)
671 FAIL ();
673 strcpy (buf2 + 2, "%n%s%n");
674 /* When the format string is writable and contains %n,
675 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
676 CHK_FAIL2_START
677 if (printf (buf2, str4, &n1, str5, &n1) != 14)
678 FAIL ();
679 CHK_FAIL2_END
681 /* But if there is no %n, even writable format string
682 should work. */
683 buf2[6] = '\0';
684 if (printf (buf2 + 4, str5) != 7)
685 FAIL ();
687 FILE *fp = stdout;
689 /* Constant literals passed directly are always ok
690 (even with warnings about possible bugs from GCC). */
691 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
692 || n1 != 7 || n2 != 14)
693 FAIL ();
695 /* In this case the format string is not known at compile time,
696 but resides in read-only memory, so is ok. */
697 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
698 || n1 != 7 || n2 != 14)
699 FAIL ();
701 strcpy (buf2 + 2, "%n%s%n");
702 /* When the format string is writable and contains %n,
703 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
704 CHK_FAIL2_START
705 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
706 FAIL ();
707 CHK_FAIL2_END
709 /* But if there is no %n, even writable format string
710 should work. */
711 buf2[6] = '\0';
712 if (fprintf (fp, buf2 + 4, str5) != 7)
713 FAIL ();
715 char *my_ptr = NULL;
716 strcpy (buf2 + 2, "%n%s%n");
717 /* When the format string is writable and contains %n,
718 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
719 CHK_FAIL2_START
720 if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
721 FAIL ();
722 else
723 free (my_ptr);
724 CHK_FAIL2_END
726 struct obstack obs;
727 obstack_init (&obs);
728 CHK_FAIL2_START
729 if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
730 FAIL ();
731 CHK_FAIL2_END
732 obstack_free (&obs, NULL);
734 my_ptr = NULL;
735 if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
736 FAIL ();
737 else
738 free (my_ptr);
740 obstack_init (&obs);
741 if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
742 FAIL ();
743 obstack_free (&obs, NULL);
745 if (freopen (temp_filename, "r", stdin) == NULL)
747 puts ("could not open temporary file");
748 exit (1);
751 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
752 FAIL ();
753 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
754 FAIL ();
756 #if __USE_FORTIFY_LEVEL >= 1
757 CHK_FAIL_START
758 if (gets (buf) != buf)
759 FAIL ();
760 CHK_FAIL_END
761 #endif
763 rewind (stdin);
765 if (fgets (buf, sizeof (buf), stdin) != buf
766 || memcmp (buf, "abcdefgh\n", 10))
767 FAIL ();
768 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
769 FAIL ();
771 rewind (stdin);
773 if (fgets (buf, l0 + sizeof (buf), stdin) != buf
774 || memcmp (buf, "abcdefgh\n", 10))
775 FAIL ();
777 #if __USE_FORTIFY_LEVEL >= 1
778 CHK_FAIL_START
779 if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
780 FAIL ();
781 CHK_FAIL_END
783 CHK_FAIL_START
784 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
785 FAIL ();
786 CHK_FAIL_END
787 #endif
789 rewind (stdin);
791 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
792 || memcmp (buf, "abcdefgh\n", 10))
793 FAIL ();
794 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
795 || memcmp (buf, "ABCDEFGHI", 10))
796 FAIL ();
798 rewind (stdin);
800 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
801 || memcmp (buf, "abcdefgh\n", 10))
802 FAIL ();
804 #if __USE_FORTIFY_LEVEL >= 1
805 CHK_FAIL_START
806 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
807 FAIL ();
808 CHK_FAIL_END
810 CHK_FAIL_START
811 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
812 FAIL ();
813 CHK_FAIL_END
814 #endif
816 rewind (stdin);
818 if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
819 || memcmp (buf, "abcdefgh\nA", 10))
820 FAIL ();
821 if (fread (buf, sizeof (buf), 1, stdin) != 1
822 || memcmp (buf, "BCDEFGHI\na", 10))
823 FAIL ();
825 rewind (stdin);
827 if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
828 || memcmp (buf, "abcdefgh\nA", 10))
829 FAIL ();
830 if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
831 || memcmp (buf, "BCDEFGHI\na", 10))
832 FAIL ();
834 #if __USE_FORTIFY_LEVEL >= 1
835 CHK_FAIL_START
836 if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
837 FAIL ();
838 CHK_FAIL_END
840 CHK_FAIL_START
841 if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
842 FAIL ();
843 CHK_FAIL_END
844 #endif
846 rewind (stdin);
848 if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
849 || memcmp (buf, "abcdefgh\nA", 10))
850 FAIL ();
851 if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
852 || memcmp (buf, "BCDEFGHI\na", 10))
853 FAIL ();
855 rewind (stdin);
857 if (fread_unlocked (buf, 1, 4, stdin) != 4
858 || memcmp (buf, "abcdFGHI\na", 10))
859 FAIL ();
860 if (fread_unlocked (buf, 4, 1, stdin) != 1
861 || memcmp (buf, "efghFGHI\na", 10))
862 FAIL ();
864 rewind (stdin);
866 if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
867 || memcmp (buf, "abcdefgh\nA", 10))
868 FAIL ();
869 if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
870 || memcmp (buf, "BCDEFGHI\na", 10))
871 FAIL ();
873 #if __USE_FORTIFY_LEVEL >= 1
874 CHK_FAIL_START
875 if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
876 FAIL ();
877 CHK_FAIL_END
879 CHK_FAIL_START
880 if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
881 FAIL ();
882 CHK_FAIL_END
883 #endif
885 lseek (fileno (stdin), 0, SEEK_SET);
887 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
888 || memcmp (buf, "abcdefgh\n", 9))
889 FAIL ();
890 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
891 || memcmp (buf, "ABCDEFGHI", 9))
892 FAIL ();
894 lseek (fileno (stdin), 0, SEEK_SET);
896 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
897 || memcmp (buf, "abcdefgh\n", 9))
898 FAIL ();
900 #if __USE_FORTIFY_LEVEL >= 1
901 CHK_FAIL_START
902 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
903 FAIL ();
904 CHK_FAIL_END
905 #endif
907 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
908 != sizeof (buf) - 1
909 || memcmp (buf, "\nABCDEFGH", 9))
910 FAIL ();
911 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
912 || memcmp (buf, "abcdefgh\n", 9))
913 FAIL ();
914 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
915 != sizeof (buf) - 1
916 || memcmp (buf, "h\nABCDEFG", 9))
917 FAIL ();
919 #if __USE_FORTIFY_LEVEL >= 1
920 CHK_FAIL_START
921 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
922 != sizeof (buf) + 1)
923 FAIL ();
924 CHK_FAIL_END
925 #endif
927 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
928 != sizeof (buf) - 1
929 || memcmp (buf, "\nABCDEFGH", 9))
930 FAIL ();
931 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
932 || memcmp (buf, "abcdefgh\n", 9))
933 FAIL ();
934 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
935 != sizeof (buf) - 1
936 || memcmp (buf, "h\nABCDEFG", 9))
937 FAIL ();
939 #if __USE_FORTIFY_LEVEL >= 1
940 CHK_FAIL_START
941 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
942 != sizeof (buf) + 1)
943 FAIL ();
944 CHK_FAIL_END
945 #endif
947 if (freopen (temp_filename, "r", stdin) == NULL)
949 puts ("could not open temporary file");
950 exit (1);
953 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
955 puts ("could not seek in test file");
956 exit (1);
959 #if __USE_FORTIFY_LEVEL >= 1
960 CHK_FAIL_START
961 if (gets (buf) != buf)
962 FAIL ();
963 CHK_FAIL_END
964 #endif
966 /* Check whether missing N$ formats are detected. */
967 CHK_FAIL2_START
968 printf ("%3$d\n", 1, 2, 3, 4);
969 CHK_FAIL2_END
971 CHK_FAIL2_START
972 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
973 CHK_FAIL2_END
975 CHK_FAIL2_START
976 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
977 CHK_FAIL2_END
979 CHK_FAIL2_START
980 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
981 CHK_FAIL2_END
983 int sp[2];
984 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
985 FAIL ();
986 else
988 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
989 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
990 != strlen (sendstr))
991 FAIL ();
993 char recvbuf[12];
994 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
995 != sizeof recvbuf
996 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
997 FAIL ();
999 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
1000 != sizeof recvbuf - 7
1001 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1002 FAIL ();
1004 #if __USE_FORTIFY_LEVEL >= 1
1005 CHK_FAIL_START
1006 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
1007 != sizeof recvbuf)
1008 FAIL ();
1009 CHK_FAIL_END
1011 CHK_FAIL_START
1012 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
1013 != sizeof recvbuf - 3)
1014 FAIL ();
1015 CHK_FAIL_END
1016 #endif
1018 socklen_t sl;
1019 struct sockaddr_un sa_un;
1021 sl = sizeof (sa_un);
1022 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
1023 (struct sockaddr *) &sa_un, &sl)
1024 != sizeof recvbuf
1025 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1026 FAIL ();
1028 sl = sizeof (sa_un);
1029 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
1030 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
1031 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1032 FAIL ();
1034 #if __USE_FORTIFY_LEVEL >= 1
1035 CHK_FAIL_START
1036 sl = sizeof (sa_un);
1037 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1038 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1039 FAIL ();
1040 CHK_FAIL_END
1042 CHK_FAIL_START
1043 sl = sizeof (sa_un);
1044 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1045 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1046 FAIL ();
1047 CHK_FAIL_END
1048 #endif
1050 close (sp[0]);
1051 close (sp[1]);
1054 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1055 char *enddir = strchr (fname, '\0');
1056 if (mkdtemp (fname) == NULL)
1058 printf ("mkdtemp failed: %m\n");
1059 return 1;
1061 *enddir = '/';
1062 if (symlink ("bar", fname) != 0)
1063 FAIL ();
1065 char readlinkbuf[4];
1066 if (readlink (fname, readlinkbuf, 4) != 3
1067 || memcmp (readlinkbuf, "bar", 3) != 0)
1068 FAIL ();
1069 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1070 || memcmp (readlinkbuf, "bbar", 4) != 0)
1071 FAIL ();
1073 #if __USE_FORTIFY_LEVEL >= 1
1074 CHK_FAIL_START
1075 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1076 FAIL ();
1077 CHK_FAIL_END
1079 CHK_FAIL_START
1080 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1081 FAIL ();
1082 CHK_FAIL_END
1083 #endif
1085 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1086 if (tmpfd < 0)
1087 FAIL ();
1089 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1090 || memcmp (readlinkbuf, "bar", 3) != 0)
1091 FAIL ();
1092 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1093 l0 + 3) != 3
1094 || memcmp (readlinkbuf, "bbar", 4) != 0)
1095 FAIL ();
1097 #if __USE_FORTIFY_LEVEL >= 1
1098 CHK_FAIL_START
1099 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1100 l0 + 3) != 3)
1101 FAIL ();
1102 CHK_FAIL_END
1104 CHK_FAIL_START
1105 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1106 4) != 3)
1107 FAIL ();
1108 CHK_FAIL_END
1109 #endif
1111 close (tmpfd);
1113 char *cwd1 = getcwd (NULL, 0);
1114 if (cwd1 == NULL)
1115 FAIL ();
1117 char *cwd2 = getcwd (NULL, 250);
1118 if (cwd2 == NULL)
1119 FAIL ();
1121 if (cwd1 && cwd2)
1123 if (strcmp (cwd1, cwd2) != 0)
1124 FAIL ();
1126 *enddir = '\0';
1127 if (chdir (fname))
1128 FAIL ();
1130 char *cwd3 = getcwd (NULL, 0);
1131 if (cwd3 == NULL)
1132 FAIL ();
1133 if (strcmp (fname, cwd3) != 0)
1134 printf ("getcwd after chdir is '%s' != '%s',"
1135 "get{c,}wd tests skipped\n", cwd3, fname);
1136 else
1138 char getcwdbuf[sizeof fname - 3];
1140 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1141 if (cwd4 != getcwdbuf
1142 || strcmp (getcwdbuf, fname) != 0)
1143 FAIL ();
1145 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1146 if (cwd4 != getcwdbuf + 1
1147 || getcwdbuf[0] != fname[0]
1148 || strcmp (getcwdbuf + 1, fname) != 0)
1149 FAIL ();
1151 #if __USE_FORTIFY_LEVEL >= 1
1152 CHK_FAIL_START
1153 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1154 != getcwdbuf + 2)
1155 FAIL ();
1156 CHK_FAIL_END
1158 CHK_FAIL_START
1159 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1160 != getcwdbuf + 2)
1161 FAIL ();
1162 CHK_FAIL_END
1163 #endif
1165 if (getwd (getcwdbuf) != getcwdbuf
1166 || strcmp (getcwdbuf, fname) != 0)
1167 FAIL ();
1169 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1170 || strcmp (getcwdbuf + 1, fname) != 0)
1171 FAIL ();
1173 #if __USE_FORTIFY_LEVEL >= 1
1174 CHK_FAIL_START
1175 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1176 FAIL ();
1177 CHK_FAIL_END
1178 #endif
1181 if (chdir (cwd1) != 0)
1182 FAIL ();
1183 free (cwd3);
1186 free (cwd1);
1187 free (cwd2);
1188 *enddir = '/';
1189 if (unlink (fname) != 0)
1190 FAIL ();
1192 *enddir = '\0';
1193 if (rmdir (fname) != 0)
1194 FAIL ();
1197 #if PATH_MAX > 0
1198 char largebuf[PATH_MAX];
1199 char *realres = realpath (".", largebuf);
1200 if (realres != largebuf)
1201 FAIL ();
1203 # if __USE_FORTIFY_LEVEL >= 1
1204 CHK_FAIL_START
1205 char realbuf[1];
1206 realres = realpath (".", realbuf);
1207 if (realres != realbuf)
1208 FAIL ();
1209 CHK_FAIL_END
1210 # endif
1211 #endif
1213 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1215 assert (MB_CUR_MAX <= 10);
1217 /* First a simple test. */
1218 char enough[10];
1219 if (wctomb (enough, L'A') != 1)
1220 FAIL ();
1222 #if __USE_FORTIFY_LEVEL >= 1
1223 /* We know the wchar_t encoding is ISO 10646. So pick a
1224 character which has a multibyte representation which does not
1225 fit. */
1226 CHK_FAIL_START
1227 char smallbuf[2];
1228 if (wctomb (smallbuf, L'\x100') != 2)
1229 FAIL ();
1230 CHK_FAIL_END
1231 #endif
1233 mbstate_t s;
1234 memset (&s, '\0', sizeof (s));
1235 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1236 FAIL ();
1238 #if __USE_FORTIFY_LEVEL >= 1
1239 /* We know the wchar_t encoding is ISO 10646. So pick a
1240 character which has a multibyte representation which does not
1241 fit. */
1242 CHK_FAIL_START
1243 char smallbuf[2];
1244 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1245 FAIL ();
1246 CHK_FAIL_END
1247 #endif
1249 wchar_t wenough[10];
1250 memset (&s, '\0', sizeof (s));
1251 const char *cp = "A";
1252 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1253 || wcscmp (wenough, L"A") != 0)
1254 FAIL ();
1256 cp = "BC";
1257 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1258 || wcscmp (wenough, L"BC") != 0)
1259 FAIL ();
1261 #if __USE_FORTIFY_LEVEL >= 1
1262 CHK_FAIL_START
1263 wchar_t wsmallbuf[2];
1264 cp = "ABC";
1265 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1266 CHK_FAIL_END
1267 #endif
1269 cp = "A";
1270 if (mbstowcs (wenough, cp, 10) != 1
1271 || wcscmp (wenough, L"A") != 0)
1272 FAIL ();
1274 cp = "DEF";
1275 if (mbstowcs (wenough, cp, l0 + 10) != 3
1276 || wcscmp (wenough, L"DEF") != 0)
1277 FAIL ();
1279 #if __USE_FORTIFY_LEVEL >= 1
1280 CHK_FAIL_START
1281 wchar_t wsmallbuf[2];
1282 cp = "ABC";
1283 mbstowcs (wsmallbuf, cp, 10);
1284 CHK_FAIL_END
1285 #endif
1287 memset (&s, '\0', sizeof (s));
1288 cp = "ABC";
1289 wcscpy (wenough, L"DEF");
1290 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1291 || wcscmp (wenough, L"AEF") != 0)
1292 FAIL ();
1294 cp = "IJ";
1295 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1296 || wcscmp (wenough, L"IEF") != 0)
1297 FAIL ();
1299 #if __USE_FORTIFY_LEVEL >= 1
1300 CHK_FAIL_START
1301 wchar_t wsmallbuf[2];
1302 cp = "ABC";
1303 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1304 CHK_FAIL_END
1305 #endif
1307 memset (&s, '\0', sizeof (s));
1308 const wchar_t *wcp = L"A";
1309 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1310 || strcmp (enough, "A") != 0)
1311 FAIL ();
1313 wcp = L"BC";
1314 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1315 || strcmp (enough, "BC") != 0)
1316 FAIL ();
1318 #if __USE_FORTIFY_LEVEL >= 1
1319 CHK_FAIL_START
1320 char smallbuf[2];
1321 wcp = L"ABC";
1322 wcsrtombs (smallbuf, &wcp, 10, &s);
1323 CHK_FAIL_END
1324 #endif
1326 memset (enough, 'Z', sizeof (enough));
1327 wcp = L"EF";
1328 if (wcstombs (enough, wcp, 10) != 2
1329 || strcmp (enough, "EF") != 0)
1330 FAIL ();
1332 wcp = L"G";
1333 if (wcstombs (enough, wcp, l0 + 10) != 1
1334 || strcmp (enough, "G") != 0)
1335 FAIL ();
1337 #if __USE_FORTIFY_LEVEL >= 1
1338 CHK_FAIL_START
1339 char smallbuf[2];
1340 wcp = L"ABC";
1341 wcstombs (smallbuf, wcp, 10);
1342 CHK_FAIL_END
1343 #endif
1345 memset (&s, '\0', sizeof (s));
1346 wcp = L"AB";
1347 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1348 || strcmp (enough, "A") != 0)
1349 FAIL ();
1351 wcp = L"BCD";
1352 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1353 || strcmp (enough, "B") != 0)
1354 FAIL ();
1356 #if __USE_FORTIFY_LEVEL >= 1
1357 CHK_FAIL_START
1358 char smallbuf[2];
1359 wcp = L"ABC";
1360 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1361 CHK_FAIL_END
1362 #endif
1364 else
1366 puts ("cannot set locale");
1367 ret = 1;
1370 fd = posix_openpt (O_RDWR);
1371 if (fd != -1)
1373 char enough[1000];
1374 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1375 FAIL ();
1377 #if __USE_FORTIFY_LEVEL >= 1
1378 CHK_FAIL_START
1379 char smallbuf[2];
1380 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1381 FAIL ();
1382 CHK_FAIL_END
1383 #endif
1384 close (fd);
1387 #if PATH_MAX > 0
1388 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1389 # if __USE_FORTIFY_LEVEL >= 1
1390 CHK_FAIL_START
1391 char smallbuf[1];
1392 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1393 CHK_FAIL_END
1394 # endif
1395 #endif
1397 gid_t grpslarge[5];
1398 int ngr = getgroups (5, grpslarge);
1399 asm volatile ("" : : "r" (ngr));
1400 #if __USE_FORTIFY_LEVEL >= 1
1401 CHK_FAIL_START
1402 char smallbuf[1];
1403 ngr = getgroups (5, (gid_t *) smallbuf);
1404 asm volatile ("" : : "r" (ngr));
1405 CHK_FAIL_END
1406 #endif
1408 fd = open (_PATH_TTY, O_RDONLY);
1409 if (fd != -1)
1411 char enough[1000];
1412 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1413 FAIL ();
1415 #if __USE_FORTIFY_LEVEL >= 1
1416 CHK_FAIL_START
1417 char smallbuf[2];
1418 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1419 FAIL ();
1420 CHK_FAIL_END
1421 #endif
1422 close (fd);
1425 char hostnamelarge[1000];
1426 gethostname (hostnamelarge, sizeof (hostnamelarge));
1427 #if __USE_FORTIFY_LEVEL >= 1
1428 CHK_FAIL_START
1429 char smallbuf[1];
1430 gethostname (smallbuf, sizeof (hostnamelarge));
1431 CHK_FAIL_END
1432 #endif
1434 char loginlarge[1000];
1435 getlogin_r (loginlarge, sizeof (hostnamelarge));
1436 #if __USE_FORTIFY_LEVEL >= 1
1437 CHK_FAIL_START
1438 char smallbuf[1];
1439 getlogin_r (smallbuf, sizeof (loginlarge));
1440 CHK_FAIL_END
1441 #endif
1443 char domainnamelarge[1000];
1444 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1445 asm volatile ("" : : "r" (res));
1446 #if __USE_FORTIFY_LEVEL >= 1
1447 CHK_FAIL_START
1448 char smallbuf[1];
1449 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1450 asm volatile ("" : : "r" (res));
1451 CHK_FAIL_END
1452 #endif
1454 return ret;