Use O_CLOEXEC in tzfile handling
[glibc.git] / debug / tst-chk1.c
blob0ec8ab07d9e29c9b2fb6ce9fa90c9f0d20fd331b
1 /* Copyright (C) 2004,2005,2006,2007,2008,2011 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 #include <assert.h>
21 #include <fcntl.h>
22 #include <locale.h>
23 #include <obstack.h>
24 #include <paths.h>
25 #include <setjmp.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <wchar.h>
32 #include <sys/select.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
36 #define obstack_chunk_alloc malloc
37 #define obstack_chunk_free free
39 char *temp_filename;
40 static void do_prepare (void);
41 static int do_test (void);
42 #define PREPARE(argc, argv) do_prepare ()
43 #define TEST_FUNCTION do_test ()
44 #include "../test-skeleton.c"
46 static void
47 do_prepare (void)
49 int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
50 if (temp_fd == -1)
52 printf ("cannot create temporary file: %m\n");
53 exit (1);
56 const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
57 if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
59 puts ("could not write test strings into file");
60 unlink (temp_filename);
61 exit (1);
65 volatile int chk_fail_ok;
66 volatile int ret;
67 jmp_buf chk_fail_buf;
69 static void
70 handler (int sig)
72 if (chk_fail_ok)
74 chk_fail_ok = 0;
75 longjmp (chk_fail_buf, 1);
77 else
78 _exit (127);
81 char buf[10];
82 wchar_t wbuf[10];
83 volatile size_t l0;
84 volatile char *p;
85 volatile wchar_t *wp;
86 const char *str1 = "JIHGFEDCBA";
87 const char *str2 = "F";
88 const char *str3 = "%s%n%s%n";
89 const char *str4 = "Hello, ";
90 const char *str5 = "World!\n";
91 const wchar_t *wstr1 = L"JIHGFEDCBA";
92 const wchar_t *wstr2 = L"F";
93 const wchar_t *wstr3 = L"%s%n%s%n";
94 const wchar_t *wstr4 = L"Hello, ";
95 const wchar_t *wstr5 = L"World!\n";
96 char buf2[10] = "%s";
97 int num1 = 67;
98 int num2 = 987654;
100 #define FAIL() \
101 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
102 #define CHK_FAIL_START \
103 chk_fail_ok = 1; \
104 if (! setjmp (chk_fail_buf)) \
106 #define CHK_FAIL_END \
107 chk_fail_ok = 0; \
108 FAIL (); \
110 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
111 # define CHK_FAIL2_START CHK_FAIL_START
112 # define CHK_FAIL2_END CHK_FAIL_END
113 #else
114 # define CHK_FAIL2_START
115 # define CHK_FAIL2_END
116 #endif
118 static int
119 do_test (void)
121 struct sigaction sa;
122 sa.sa_handler = handler;
123 sa.sa_flags = 0;
124 sigemptyset (&sa.sa_mask);
126 sigaction (SIGABRT, &sa, NULL);
128 /* Avoid all the buffer overflow messages on stderr. */
129 int fd = open (_PATH_DEVNULL, O_WRONLY);
130 if (fd == -1)
131 close (STDERR_FILENO);
132 else
134 dup2 (fd, STDERR_FILENO);
135 close (fd);
137 setenv ("LIBC_FATAL_STDERR_", "1", 1);
139 struct A { char buf1[9]; char buf2[1]; } a;
140 struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
142 printf ("Test checking routines at fortify level %d\n",
143 #ifdef __USE_FORTIFY_LEVEL
144 (int) __USE_FORTIFY_LEVEL
145 #else
147 #endif
150 #if defined __USE_FORTIFY_LEVEL && !defined __extern_always_inline
151 printf ("Test skipped");
152 if (l0 == 0)
153 return 0;
154 #endif
156 /* These ops can be done without runtime checking of object size. */
157 memcpy (buf, "abcdefghij", 10);
158 memmove (buf + 1, buf, 9);
159 if (memcmp (buf, "aabcdefghi", 10))
160 FAIL ();
162 if (mempcpy (buf + 5, "abcde", 5) != buf + 10
163 || memcmp (buf, "aabcdabcde", 10))
164 FAIL ();
166 memset (buf + 8, 'j', 2);
167 if (memcmp (buf, "aabcdabcjj", 10))
168 FAIL ();
170 strcpy (buf + 4, "EDCBA");
171 if (memcmp (buf, "aabcEDCBA", 10))
172 FAIL ();
174 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
175 FAIL ();
177 strncpy (buf + 6, "X", 4);
178 if (memcmp (buf, "aabcEDX\0\0", 10))
179 FAIL ();
181 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
182 FAIL ();
184 if (snprintf (buf + 7, 3, "%s", "987654") != 6
185 || memcmp (buf, "aabcEDX98", 10))
186 FAIL ();
188 /* These ops need runtime checking, but shouldn't __chk_fail. */
189 memcpy (buf, "abcdefghij", l0 + 10);
190 memmove (buf + 1, buf, l0 + 9);
191 if (memcmp (buf, "aabcdefghi", 10))
192 FAIL ();
194 if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
195 || memcmp (buf, "aabcdabcde", 10))
196 FAIL ();
198 memset (buf + 8, 'j', l0 + 2);
199 if (memcmp (buf, "aabcdabcjj", 10))
200 FAIL ();
202 strcpy (buf + 4, str1 + 5);
203 if (memcmp (buf, "aabcEDCBA", 10))
204 FAIL ();
206 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
207 FAIL ();
209 strncpy (buf + 6, "X", l0 + 4);
210 if (memcmp (buf, "aabcEDX\0\0", 10))
211 FAIL ();
213 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
214 || memcmp (buf, "aabcEcd\0\0", 10))
215 FAIL ();
217 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
218 FAIL ();
220 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
221 FAIL ();
223 buf[l0 + 8] = '\0';
224 strcat (buf, "A");
225 if (memcmp (buf, "aabcEcd9A", 10))
226 FAIL ();
228 buf[l0 + 7] = '\0';
229 strncat (buf, "ZYXWV", l0 + 2);
230 if (memcmp (buf, "aabcEcdZY", 10))
231 FAIL ();
233 memcpy (a.buf1, "abcdefghij", l0 + 10);
234 memmove (a.buf1 + 1, a.buf1, l0 + 9);
235 if (memcmp (a.buf1, "aabcdefghi", 10))
236 FAIL ();
238 if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
239 || memcmp (a.buf1, "aabcdabcde", 10))
240 FAIL ();
242 memset (a.buf1 + 8, 'j', l0 + 2);
243 if (memcmp (a.buf1, "aabcdabcjj", 10))
244 FAIL ();
246 #if __USE_FORTIFY_LEVEL < 2
247 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
248 and sufficient GCC support, as the string operations overflow
249 from a.buf1 into a.buf2. */
250 strcpy (a.buf1 + 4, str1 + 5);
251 if (memcmp (a.buf1, "aabcEDCBA", 10))
252 FAIL ();
254 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
255 || memcmp (a.buf1, "aabcEDCBF", 10))
256 FAIL ();
258 strncpy (a.buf1 + 6, "X", l0 + 4);
259 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
260 FAIL ();
262 if (sprintf (a.buf1 + 7, "%d", num1) != 2
263 || memcmp (a.buf1, "aabcEDX67", 10))
264 FAIL ();
266 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
267 || memcmp (a.buf1, "aabcEDX98", 10))
268 FAIL ();
270 a.buf1[l0 + 8] = '\0';
271 strcat (a.buf1, "A");
272 if (memcmp (a.buf1, "aabcEDX9A", 10))
273 FAIL ();
275 a.buf1[l0 + 7] = '\0';
276 strncat (a.buf1, "ZYXWV", l0 + 2);
277 if (memcmp (a.buf1, "aabcEDXZY", 10))
278 FAIL ();
280 #endif
282 #if __USE_FORTIFY_LEVEL >= 1
283 /* Now check if all buffer overflows are caught at runtime. */
285 CHK_FAIL_START
286 memcpy (buf + 1, "abcdefghij", l0 + 10);
287 CHK_FAIL_END
289 CHK_FAIL_START
290 memmove (buf + 2, buf + 1, l0 + 9);
291 CHK_FAIL_END
293 CHK_FAIL_START
294 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
295 CHK_FAIL_END
297 CHK_FAIL_START
298 memset (buf + 9, 'j', l0 + 2);
299 CHK_FAIL_END
301 CHK_FAIL_START
302 strcpy (buf + 5, str1 + 5);
303 CHK_FAIL_END
305 CHK_FAIL_START
306 p = stpcpy (buf + 9, str2);
307 CHK_FAIL_END
309 CHK_FAIL_START
310 strncpy (buf + 7, "X", l0 + 4);
311 CHK_FAIL_END
313 CHK_FAIL_START
314 stpncpy (buf + 6, "cd", l0 + 5);
315 CHK_FAIL_END
317 # if !defined __cplusplus || defined __va_arg_pack
318 CHK_FAIL_START
319 sprintf (buf + 8, "%d", num1);
320 CHK_FAIL_END
322 CHK_FAIL_START
323 snprintf (buf + 8, l0 + 3, "%d", num2);
324 CHK_FAIL_END
326 CHK_FAIL_START
327 swprintf (wbuf + 8, 3, L"%d", num1);
328 CHK_FAIL_END
330 CHK_FAIL_START
331 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
332 CHK_FAIL_END
333 # endif
335 memcpy (buf, str1 + 2, l0 + 9);
336 CHK_FAIL_START
337 strcat (buf, "AB");
338 CHK_FAIL_END
340 memcpy (buf, str1 + 3, l0 + 8);
341 CHK_FAIL_START
342 strncat (buf, "ZYXWV", l0 + 3);
343 CHK_FAIL_END
345 CHK_FAIL_START
346 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
347 CHK_FAIL_END
349 CHK_FAIL_START
350 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
351 CHK_FAIL_END
353 CHK_FAIL_START
354 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
355 CHK_FAIL_END
357 CHK_FAIL_START
358 memset (a.buf1 + 9, 'j', l0 + 2);
359 CHK_FAIL_END
361 # if __USE_FORTIFY_LEVEL >= 2
362 # define O 0
363 # else
364 # define O 1
365 # endif
367 CHK_FAIL_START
368 strcpy (a.buf1 + (O + 4), str1 + 5);
369 CHK_FAIL_END
371 CHK_FAIL_START
372 p = stpcpy (a.buf1 + (O + 8), str2);
373 CHK_FAIL_END
375 CHK_FAIL_START
376 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
377 CHK_FAIL_END
379 # if !defined __cplusplus || defined __va_arg_pack
380 CHK_FAIL_START
381 sprintf (a.buf1 + (O + 7), "%d", num1);
382 CHK_FAIL_END
384 CHK_FAIL_START
385 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
386 CHK_FAIL_END
387 # endif
389 memcpy (a.buf1, str1 + (3 - O), l0 + 8 + O);
390 CHK_FAIL_START
391 strcat (a.buf1, "AB");
392 CHK_FAIL_END
394 memcpy (a.buf1, str1 + (4 - O), l0 + 7 + O);
395 CHK_FAIL_START
396 strncat (a.buf1, "ZYXWV", l0 + 3);
397 CHK_FAIL_END
398 #endif
401 /* These ops can be done without runtime checking of object size. */
402 wmemcpy (wbuf, L"abcdefghij", 10);
403 wmemmove (wbuf + 1, wbuf, 9);
404 if (wmemcmp (wbuf, L"aabcdefghi", 10))
405 FAIL ();
407 if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
408 || wmemcmp (wbuf, L"aabcdabcde", 10))
409 FAIL ();
411 wmemset (wbuf + 8, L'j', 2);
412 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
413 FAIL ();
415 wcscpy (wbuf + 4, L"EDCBA");
416 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
417 FAIL ();
419 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
420 FAIL ();
422 wcsncpy (wbuf + 6, L"X", 4);
423 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
424 FAIL ();
426 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
427 || wmemcmp (wbuf, L"aabcEDX98", 10))
428 FAIL ();
430 if (swprintf (wbuf + 7, 3, L"64") != 2
431 || wmemcmp (wbuf, L"aabcEDX64", 10))
432 FAIL ();
434 /* These ops need runtime checking, but shouldn't __chk_fail. */
435 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
436 wmemmove (wbuf + 1, wbuf, l0 + 9);
437 if (wmemcmp (wbuf, L"aabcdefghi", 10))
438 FAIL ();
440 if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
441 || wmemcmp (wbuf, L"aabcdabcde", 10))
442 FAIL ();
444 wmemset (wbuf + 8, L'j', l0 + 2);
445 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
446 FAIL ();
448 wcscpy (wbuf + 4, wstr1 + 5);
449 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
450 FAIL ();
452 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
453 FAIL ();
455 wcsncpy (wbuf + 6, L"X", l0 + 4);
456 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
457 FAIL ();
459 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
460 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
461 FAIL ();
463 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
464 || wmemcmp (wbuf, L"aabcEcd98", 10))
465 FAIL ();
467 wbuf[l0 + 8] = L'\0';
468 wcscat (wbuf, L"A");
469 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
470 FAIL ();
472 wbuf[l0 + 7] = L'\0';
473 wcsncat (wbuf, L"ZYXWV", l0 + 2);
474 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
475 FAIL ();
477 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
478 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
479 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
480 FAIL ();
482 if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
483 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
484 FAIL ();
486 wmemset (wa.buf1 + 8, L'j', l0 + 2);
487 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
488 FAIL ();
490 #if __USE_FORTIFY_LEVEL < 2
491 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
492 and sufficient GCC support, as the string operations overflow
493 from a.buf1 into a.buf2. */
494 wcscpy (wa.buf1 + 4, wstr1 + 5);
495 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
496 FAIL ();
498 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
499 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
500 FAIL ();
502 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
503 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
504 FAIL ();
506 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
507 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
508 FAIL ();
510 wa.buf1[l0 + 8] = L'\0';
511 wcscat (wa.buf1, L"A");
512 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
513 FAIL ();
515 wa.buf1[l0 + 7] = L'\0';
516 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
517 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
518 FAIL ();
520 #endif
522 #if __USE_FORTIFY_LEVEL >= 1
523 /* Now check if all buffer overflows are caught at runtime. */
525 CHK_FAIL_START
526 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
527 CHK_FAIL_END
529 CHK_FAIL_START
530 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
531 CHK_FAIL_END
533 CHK_FAIL_START
534 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
535 CHK_FAIL_END
537 CHK_FAIL_START
538 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
539 CHK_FAIL_END
541 CHK_FAIL_START
542 wmemset (wbuf + 9, L'j', l0 + 2);
543 CHK_FAIL_END
545 CHK_FAIL_START
546 wcscpy (wbuf + 5, wstr1 + 5);
547 CHK_FAIL_END
549 CHK_FAIL_START
550 wp = wcpcpy (wbuf + 9, wstr2);
551 CHK_FAIL_END
553 CHK_FAIL_START
554 wcsncpy (wbuf + 7, L"X", l0 + 4);
555 CHK_FAIL_END
557 CHK_FAIL_START
558 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
559 CHK_FAIL_END
561 CHK_FAIL_START
562 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
563 CHK_FAIL_END
565 CHK_FAIL_START
566 wcpncpy (wbuf + 6, L"cd", l0 + 5);
567 CHK_FAIL_END
569 wmemcpy (wbuf, wstr1 + 2, l0 + 9);
570 CHK_FAIL_START
571 wcscat (wbuf, L"AB");
572 CHK_FAIL_END
574 wmemcpy (wbuf, wstr1 + 3, l0 + 8);
575 CHK_FAIL_START
576 wcsncat (wbuf, L"ZYXWV", l0 + 3);
577 CHK_FAIL_END
579 CHK_FAIL_START
580 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
581 CHK_FAIL_END
583 CHK_FAIL_START
584 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
585 CHK_FAIL_END
587 CHK_FAIL_START
588 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
589 CHK_FAIL_END
591 CHK_FAIL_START
592 wmemset (wa.buf1 + 9, L'j', l0 + 2);
593 CHK_FAIL_END
595 #if __USE_FORTIFY_LEVEL >= 2
596 # define O 0
597 #else
598 # define O 1
599 #endif
601 CHK_FAIL_START
602 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
603 CHK_FAIL_END
605 CHK_FAIL_START
606 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
607 CHK_FAIL_END
609 CHK_FAIL_START
610 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
611 CHK_FAIL_END
613 wmemcpy (wa.buf1, wstr1 + (3 - O), l0 + 8 + O);
614 CHK_FAIL_START
615 wcscat (wa.buf1, L"AB");
616 CHK_FAIL_END
618 wmemcpy (wa.buf1, wstr1 + (4 - O), l0 + 7 + O);
619 CHK_FAIL_START
620 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
621 CHK_FAIL_END
622 #endif
625 /* Now checks for %n protection. */
627 /* Constant literals passed directly are always ok
628 (even with warnings about possible bugs from GCC). */
629 int n1, n2;
630 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
631 || n1 != 1 || n2 != 2)
632 FAIL ();
634 /* In this case the format string is not known at compile time,
635 but resides in read-only memory, so is ok. */
636 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
637 || n1 != 1 || n2 != 2)
638 FAIL ();
640 strcpy (buf2 + 2, "%n%s%n");
641 /* When the format string is writable and contains %n,
642 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
643 CHK_FAIL2_START
644 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
645 FAIL ();
646 CHK_FAIL2_END
648 CHK_FAIL2_START
649 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
650 FAIL ();
651 CHK_FAIL2_END
653 /* But if there is no %n, even writable format string
654 should work. */
655 buf2[6] = '\0';
656 if (sprintf (buf, buf2 + 4, str2) != 1)
657 FAIL ();
659 /* Constant literals passed directly are always ok
660 (even with warnings about possible bugs from GCC). */
661 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
662 || n1 != 7 || n2 != 14)
663 FAIL ();
665 /* In this case the format string is not known at compile time,
666 but resides in read-only memory, so is ok. */
667 if (printf (str3, str4, &n1, str5, &n2) != 14
668 || n1 != 7 || n2 != 14)
669 FAIL ();
671 strcpy (buf2 + 2, "%n%s%n");
672 /* When the format string is writable and contains %n,
673 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
674 CHK_FAIL2_START
675 if (printf (buf2, str4, &n1, str5, &n1) != 14)
676 FAIL ();
677 CHK_FAIL2_END
679 /* But if there is no %n, even writable format string
680 should work. */
681 buf2[6] = '\0';
682 if (printf (buf2 + 4, str5) != 7)
683 FAIL ();
685 FILE *fp = stdout;
687 /* Constant literals passed directly are always ok
688 (even with warnings about possible bugs from GCC). */
689 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
690 || n1 != 7 || n2 != 14)
691 FAIL ();
693 /* In this case the format string is not known at compile time,
694 but resides in read-only memory, so is ok. */
695 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
696 || n1 != 7 || n2 != 14)
697 FAIL ();
699 strcpy (buf2 + 2, "%n%s%n");
700 /* When the format string is writable and contains %n,
701 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
702 CHK_FAIL2_START
703 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
704 FAIL ();
705 CHK_FAIL2_END
707 /* But if there is no %n, even writable format string
708 should work. */
709 buf2[6] = '\0';
710 if (fprintf (fp, buf2 + 4, str5) != 7)
711 FAIL ();
713 char *my_ptr = NULL;
714 strcpy (buf2 + 2, "%n%s%n");
715 /* When the format string is writable and contains %n,
716 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
717 CHK_FAIL2_START
718 if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
719 FAIL ();
720 else
721 free (my_ptr);
722 CHK_FAIL2_END
724 struct obstack obs;
725 obstack_init (&obs);
726 CHK_FAIL2_START
727 if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
728 FAIL ();
729 CHK_FAIL2_END
730 obstack_free (&obs, NULL);
732 my_ptr = NULL;
733 if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
734 FAIL ();
735 else
736 free (my_ptr);
738 obstack_init (&obs);
739 if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
740 FAIL ();
741 obstack_free (&obs, NULL);
743 if (freopen (temp_filename, "r", stdin) == NULL)
745 puts ("could not open temporary file");
746 exit (1);
749 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
750 FAIL ();
751 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
752 FAIL ();
754 #if __USE_FORTIFY_LEVEL >= 1
755 CHK_FAIL_START
756 if (gets (buf) != buf)
757 FAIL ();
758 CHK_FAIL_END
759 #endif
761 rewind (stdin);
763 if (fgets (buf, sizeof (buf), stdin) != buf
764 || memcmp (buf, "abcdefgh\n", 10))
765 FAIL ();
766 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
767 FAIL ();
769 rewind (stdin);
771 if (fgets (buf, l0 + sizeof (buf), stdin) != buf
772 || memcmp (buf, "abcdefgh\n", 10))
773 FAIL ();
775 #if __USE_FORTIFY_LEVEL >= 1
776 CHK_FAIL_START
777 if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
778 FAIL ();
779 CHK_FAIL_END
781 CHK_FAIL_START
782 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
783 FAIL ();
784 CHK_FAIL_END
785 #endif
787 rewind (stdin);
789 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
790 || memcmp (buf, "abcdefgh\n", 10))
791 FAIL ();
792 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
793 || memcmp (buf, "ABCDEFGHI", 10))
794 FAIL ();
796 rewind (stdin);
798 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
799 || memcmp (buf, "abcdefgh\n", 10))
800 FAIL ();
802 #if __USE_FORTIFY_LEVEL >= 1
803 CHK_FAIL_START
804 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
805 FAIL ();
806 CHK_FAIL_END
808 CHK_FAIL_START
809 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
810 FAIL ();
811 CHK_FAIL_END
812 #endif
814 rewind (stdin);
816 if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
817 || memcmp (buf, "abcdefgh\nA", 10))
818 FAIL ();
819 if (fread (buf, sizeof (buf), 1, stdin) != 1
820 || memcmp (buf, "BCDEFGHI\na", 10))
821 FAIL ();
823 rewind (stdin);
825 if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
826 || memcmp (buf, "abcdefgh\nA", 10))
827 FAIL ();
828 if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
829 || memcmp (buf, "BCDEFGHI\na", 10))
830 FAIL ();
832 #if __USE_FORTIFY_LEVEL >= 1
833 CHK_FAIL_START
834 if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
835 FAIL ();
836 CHK_FAIL_END
838 CHK_FAIL_START
839 if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
840 FAIL ();
841 CHK_FAIL_END
842 #endif
844 rewind (stdin);
846 if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
847 || memcmp (buf, "abcdefgh\nA", 10))
848 FAIL ();
849 if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
850 || memcmp (buf, "BCDEFGHI\na", 10))
851 FAIL ();
853 rewind (stdin);
855 if (fread_unlocked (buf, 1, 4, stdin) != 4
856 || memcmp (buf, "abcdFGHI\na", 10))
857 FAIL ();
858 if (fread_unlocked (buf, 4, 1, stdin) != 1
859 || memcmp (buf, "efghFGHI\na", 10))
860 FAIL ();
862 rewind (stdin);
864 if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
865 || memcmp (buf, "abcdefgh\nA", 10))
866 FAIL ();
867 if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
868 || memcmp (buf, "BCDEFGHI\na", 10))
869 FAIL ();
871 #if __USE_FORTIFY_LEVEL >= 1
872 CHK_FAIL_START
873 if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
874 FAIL ();
875 CHK_FAIL_END
877 CHK_FAIL_START
878 if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
879 FAIL ();
880 CHK_FAIL_END
881 #endif
883 lseek (fileno (stdin), 0, SEEK_SET);
885 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
886 || memcmp (buf, "abcdefgh\n", 9))
887 FAIL ();
888 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
889 || memcmp (buf, "ABCDEFGHI", 9))
890 FAIL ();
892 lseek (fileno (stdin), 0, SEEK_SET);
894 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
895 || memcmp (buf, "abcdefgh\n", 9))
896 FAIL ();
898 #if __USE_FORTIFY_LEVEL >= 1
899 CHK_FAIL_START
900 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
901 FAIL ();
902 CHK_FAIL_END
903 #endif
905 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
906 != sizeof (buf) - 1
907 || memcmp (buf, "\nABCDEFGH", 9))
908 FAIL ();
909 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
910 || memcmp (buf, "abcdefgh\n", 9))
911 FAIL ();
912 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
913 != sizeof (buf) - 1
914 || memcmp (buf, "h\nABCDEFG", 9))
915 FAIL ();
917 #if __USE_FORTIFY_LEVEL >= 1
918 CHK_FAIL_START
919 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
920 != sizeof (buf) + 1)
921 FAIL ();
922 CHK_FAIL_END
923 #endif
925 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
926 != sizeof (buf) - 1
927 || memcmp (buf, "\nABCDEFGH", 9))
928 FAIL ();
929 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
930 || memcmp (buf, "abcdefgh\n", 9))
931 FAIL ();
932 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
933 != sizeof (buf) - 1
934 || memcmp (buf, "h\nABCDEFG", 9))
935 FAIL ();
937 #if __USE_FORTIFY_LEVEL >= 1
938 CHK_FAIL_START
939 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
940 != sizeof (buf) + 1)
941 FAIL ();
942 CHK_FAIL_END
943 #endif
945 if (freopen (temp_filename, "r", stdin) == NULL)
947 puts ("could not open temporary file");
948 exit (1);
951 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
953 puts ("could not seek in test file");
954 exit (1);
957 #if __USE_FORTIFY_LEVEL >= 1
958 CHK_FAIL_START
959 if (gets (buf) != buf)
960 FAIL ();
961 CHK_FAIL_END
962 #endif
964 /* Check whether missing N$ formats are detected. */
965 CHK_FAIL2_START
966 printf ("%3$d\n", 1, 2, 3, 4);
967 CHK_FAIL2_END
969 CHK_FAIL2_START
970 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
971 CHK_FAIL2_END
973 CHK_FAIL2_START
974 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
975 CHK_FAIL2_END
977 CHK_FAIL2_START
978 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
979 CHK_FAIL2_END
981 int sp[2];
982 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
983 FAIL ();
984 else
986 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
987 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
988 != strlen (sendstr))
989 FAIL ();
991 char recvbuf[12];
992 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
993 != sizeof recvbuf
994 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
995 FAIL ();
997 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
998 != sizeof recvbuf - 7
999 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1000 FAIL ();
1002 #if __USE_FORTIFY_LEVEL >= 1
1003 CHK_FAIL_START
1004 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
1005 != sizeof recvbuf)
1006 FAIL ();
1007 CHK_FAIL_END
1009 CHK_FAIL_START
1010 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
1011 != sizeof recvbuf - 3)
1012 FAIL ();
1013 CHK_FAIL_END
1014 #endif
1016 socklen_t sl;
1017 struct sockaddr_un sa_un;
1019 sl = sizeof (sa_un);
1020 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
1021 (struct sockaddr *) &sa_un, &sl)
1022 != sizeof recvbuf
1023 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1024 FAIL ();
1026 sl = sizeof (sa_un);
1027 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
1028 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
1029 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1030 FAIL ();
1032 #if __USE_FORTIFY_LEVEL >= 1
1033 CHK_FAIL_START
1034 sl = sizeof (sa_un);
1035 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1036 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1037 FAIL ();
1038 CHK_FAIL_END
1040 CHK_FAIL_START
1041 sl = sizeof (sa_un);
1042 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1043 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1044 FAIL ();
1045 CHK_FAIL_END
1046 #endif
1048 close (sp[0]);
1049 close (sp[1]);
1052 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1053 char *enddir = strchr (fname, '\0');
1054 if (mkdtemp (fname) == NULL)
1056 printf ("mkdtemp failed: %m\n");
1057 return 1;
1059 *enddir = '/';
1060 if (symlink ("bar", fname) != 0)
1061 FAIL ();
1063 char readlinkbuf[4];
1064 if (readlink (fname, readlinkbuf, 4) != 3
1065 || memcmp (readlinkbuf, "bar", 3) != 0)
1066 FAIL ();
1067 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1068 || memcmp (readlinkbuf, "bbar", 4) != 0)
1069 FAIL ();
1071 #if __USE_FORTIFY_LEVEL >= 1
1072 CHK_FAIL_START
1073 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1074 FAIL ();
1075 CHK_FAIL_END
1077 CHK_FAIL_START
1078 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1079 FAIL ();
1080 CHK_FAIL_END
1081 #endif
1083 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1084 if (tmpfd < 0)
1085 FAIL ();
1087 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1088 || memcmp (readlinkbuf, "bar", 3) != 0)
1089 FAIL ();
1090 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1091 l0 + 3) != 3
1092 || memcmp (readlinkbuf, "bbar", 4) != 0)
1093 FAIL ();
1095 #if __USE_FORTIFY_LEVEL >= 1
1096 CHK_FAIL_START
1097 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1098 l0 + 3) != 3)
1099 FAIL ();
1100 CHK_FAIL_END
1102 CHK_FAIL_START
1103 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1104 4) != 3)
1105 FAIL ();
1106 CHK_FAIL_END
1107 #endif
1109 close (tmpfd);
1111 char *cwd1 = getcwd (NULL, 0);
1112 if (cwd1 == NULL)
1113 FAIL ();
1115 char *cwd2 = getcwd (NULL, 250);
1116 if (cwd2 == NULL)
1117 FAIL ();
1119 if (cwd1 && cwd2)
1121 if (strcmp (cwd1, cwd2) != 0)
1122 FAIL ();
1124 *enddir = '\0';
1125 if (chdir (fname))
1126 FAIL ();
1128 char *cwd3 = getcwd (NULL, 0);
1129 if (cwd3 == NULL)
1130 FAIL ();
1131 if (strcmp (fname, cwd3) != 0)
1132 printf ("getcwd after chdir is '%s' != '%s',"
1133 "get{c,}wd tests skipped\n", cwd3, fname);
1134 else
1136 char getcwdbuf[sizeof fname - 3];
1138 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1139 if (cwd4 != getcwdbuf
1140 || strcmp (getcwdbuf, fname) != 0)
1141 FAIL ();
1143 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1144 if (cwd4 != getcwdbuf + 1
1145 || getcwdbuf[0] != fname[0]
1146 || strcmp (getcwdbuf + 1, fname) != 0)
1147 FAIL ();
1149 #if __USE_FORTIFY_LEVEL >= 1
1150 CHK_FAIL_START
1151 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1152 != getcwdbuf + 2)
1153 FAIL ();
1154 CHK_FAIL_END
1156 CHK_FAIL_START
1157 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1158 != getcwdbuf + 2)
1159 FAIL ();
1160 CHK_FAIL_END
1161 #endif
1163 if (getwd (getcwdbuf) != getcwdbuf
1164 || strcmp (getcwdbuf, fname) != 0)
1165 FAIL ();
1167 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1168 || strcmp (getcwdbuf + 1, fname) != 0)
1169 FAIL ();
1171 #if __USE_FORTIFY_LEVEL >= 1
1172 CHK_FAIL_START
1173 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1174 FAIL ();
1175 CHK_FAIL_END
1176 #endif
1179 if (chdir (cwd1) != 0)
1180 FAIL ();
1181 free (cwd3);
1184 free (cwd1);
1185 free (cwd2);
1186 *enddir = '/';
1187 if (unlink (fname) != 0)
1188 FAIL ();
1190 *enddir = '\0';
1191 if (rmdir (fname) != 0)
1192 FAIL ();
1195 #if PATH_MAX > 0
1196 char largebuf[PATH_MAX];
1197 char *realres = realpath (".", largebuf);
1198 if (realres != largebuf)
1199 FAIL ();
1201 # if __USE_FORTIFY_LEVEL >= 1
1202 CHK_FAIL_START
1203 char realbuf[1];
1204 realres = realpath (".", realbuf);
1205 if (realres != realbuf)
1206 FAIL ();
1207 CHK_FAIL_END
1208 # endif
1209 #endif
1211 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1213 assert (MB_CUR_MAX <= 10);
1215 /* First a simple test. */
1216 char enough[10];
1217 if (wctomb (enough, L'A') != 1)
1218 FAIL ();
1220 #if __USE_FORTIFY_LEVEL >= 1
1221 /* We know the wchar_t encoding is ISO 10646. So pick a
1222 character which has a multibyte representation which does not
1223 fit. */
1224 CHK_FAIL_START
1225 char smallbuf[2];
1226 if (wctomb (smallbuf, L'\x100') != 2)
1227 FAIL ();
1228 CHK_FAIL_END
1229 #endif
1231 mbstate_t s;
1232 memset (&s, '\0', sizeof (s));
1233 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1234 FAIL ();
1236 #if __USE_FORTIFY_LEVEL >= 1
1237 /* We know the wchar_t encoding is ISO 10646. So pick a
1238 character which has a multibyte representation which does not
1239 fit. */
1240 CHK_FAIL_START
1241 char smallbuf[2];
1242 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1243 FAIL ();
1244 CHK_FAIL_END
1245 #endif
1247 wchar_t wenough[10];
1248 memset (&s, '\0', sizeof (s));
1249 const char *cp = "A";
1250 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1251 || wcscmp (wenough, L"A") != 0)
1252 FAIL ();
1254 cp = "BC";
1255 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1256 || wcscmp (wenough, L"BC") != 0)
1257 FAIL ();
1259 #if __USE_FORTIFY_LEVEL >= 1
1260 CHK_FAIL_START
1261 wchar_t wsmallbuf[2];
1262 cp = "ABC";
1263 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1264 CHK_FAIL_END
1265 #endif
1267 cp = "A";
1268 if (mbstowcs (wenough, cp, 10) != 1
1269 || wcscmp (wenough, L"A") != 0)
1270 FAIL ();
1272 cp = "DEF";
1273 if (mbstowcs (wenough, cp, l0 + 10) != 3
1274 || wcscmp (wenough, L"DEF") != 0)
1275 FAIL ();
1277 #if __USE_FORTIFY_LEVEL >= 1
1278 CHK_FAIL_START
1279 wchar_t wsmallbuf[2];
1280 cp = "ABC";
1281 mbstowcs (wsmallbuf, cp, 10);
1282 CHK_FAIL_END
1283 #endif
1285 memset (&s, '\0', sizeof (s));
1286 cp = "ABC";
1287 wcscpy (wenough, L"DEF");
1288 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1289 || wcscmp (wenough, L"AEF") != 0)
1290 FAIL ();
1292 cp = "IJ";
1293 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1294 || wcscmp (wenough, L"IEF") != 0)
1295 FAIL ();
1297 #if __USE_FORTIFY_LEVEL >= 1
1298 CHK_FAIL_START
1299 wchar_t wsmallbuf[2];
1300 cp = "ABC";
1301 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1302 CHK_FAIL_END
1303 #endif
1305 memset (&s, '\0', sizeof (s));
1306 const wchar_t *wcp = L"A";
1307 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1308 || strcmp (enough, "A") != 0)
1309 FAIL ();
1311 wcp = L"BC";
1312 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1313 || strcmp (enough, "BC") != 0)
1314 FAIL ();
1316 #if __USE_FORTIFY_LEVEL >= 1
1317 CHK_FAIL_START
1318 char smallbuf[2];
1319 wcp = L"ABC";
1320 wcsrtombs (smallbuf, &wcp, 10, &s);
1321 CHK_FAIL_END
1322 #endif
1324 memset (enough, 'Z', sizeof (enough));
1325 wcp = L"EF";
1326 if (wcstombs (enough, wcp, 10) != 2
1327 || strcmp (enough, "EF") != 0)
1328 FAIL ();
1330 wcp = L"G";
1331 if (wcstombs (enough, wcp, l0 + 10) != 1
1332 || strcmp (enough, "G") != 0)
1333 FAIL ();
1335 #if __USE_FORTIFY_LEVEL >= 1
1336 CHK_FAIL_START
1337 char smallbuf[2];
1338 wcp = L"ABC";
1339 wcstombs (smallbuf, wcp, 10);
1340 CHK_FAIL_END
1341 #endif
1343 memset (&s, '\0', sizeof (s));
1344 wcp = L"AB";
1345 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1346 || strcmp (enough, "A") != 0)
1347 FAIL ();
1349 wcp = L"BCD";
1350 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1351 || strcmp (enough, "B") != 0)
1352 FAIL ();
1354 #if __USE_FORTIFY_LEVEL >= 1
1355 CHK_FAIL_START
1356 char smallbuf[2];
1357 wcp = L"ABC";
1358 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1359 CHK_FAIL_END
1360 #endif
1362 else
1364 puts ("cannot set locale");
1365 ret = 1;
1368 fd = posix_openpt (O_RDWR);
1369 if (fd != -1)
1371 char enough[1000];
1372 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1373 FAIL ();
1375 #if __USE_FORTIFY_LEVEL >= 1
1376 CHK_FAIL_START
1377 char smallbuf[2];
1378 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1379 FAIL ();
1380 CHK_FAIL_END
1381 #endif
1382 close (fd);
1385 #if PATH_MAX > 0
1386 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1387 # if __USE_FORTIFY_LEVEL >= 1
1388 CHK_FAIL_START
1389 char smallbuf[1];
1390 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1391 CHK_FAIL_END
1392 # endif
1393 #endif
1395 gid_t grpslarge[5];
1396 int ngr = getgroups (5, grpslarge);
1397 asm volatile ("" : : "r" (ngr));
1398 #if __USE_FORTIFY_LEVEL >= 1
1399 CHK_FAIL_START
1400 char smallbuf[1];
1401 ngr = getgroups (5, (gid_t *) smallbuf);
1402 asm volatile ("" : : "r" (ngr));
1403 CHK_FAIL_END
1404 #endif
1406 fd = open (_PATH_TTY, O_RDONLY);
1407 if (fd != -1)
1409 char enough[1000];
1410 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1411 FAIL ();
1413 #if __USE_FORTIFY_LEVEL >= 1
1414 CHK_FAIL_START
1415 char smallbuf[2];
1416 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1417 FAIL ();
1418 CHK_FAIL_END
1419 #endif
1420 close (fd);
1423 char hostnamelarge[1000];
1424 gethostname (hostnamelarge, sizeof (hostnamelarge));
1425 #if __USE_FORTIFY_LEVEL >= 1
1426 CHK_FAIL_START
1427 char smallbuf[1];
1428 gethostname (smallbuf, sizeof (hostnamelarge));
1429 CHK_FAIL_END
1430 #endif
1432 char loginlarge[1000];
1433 getlogin_r (loginlarge, sizeof (hostnamelarge));
1434 #if __USE_FORTIFY_LEVEL >= 1
1435 CHK_FAIL_START
1436 char smallbuf[1];
1437 getlogin_r (smallbuf, sizeof (loginlarge));
1438 CHK_FAIL_END
1439 #endif
1441 char domainnamelarge[1000];
1442 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1443 asm volatile ("" : : "r" (res));
1444 #if __USE_FORTIFY_LEVEL >= 1
1445 CHK_FAIL_START
1446 char smallbuf[1];
1447 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1448 asm volatile ("" : : "r" (res));
1449 CHK_FAIL_END
1450 #endif
1452 fd_set s;
1453 FD_ZERO (&s);
1454 FD_SET (FD_SETSIZE - 1, &s);
1455 #if __USE_FORTIFY_LEVEL >= 1
1456 CHK_FAIL_START
1457 FD_SET (FD_SETSIZE, &s);
1458 CHK_FAIL_END
1459 #endif
1460 FD_CLR (FD_SETSIZE - 1, &s);
1461 #if __USE_FORTIFY_LEVEL >= 1
1462 CHK_FAIL_START
1463 FD_CLR (FD_SETSIZE, &s);
1464 CHK_FAIL_END
1465 #endif
1466 FD_ISSET (FD_SETSIZE - 1, &s);
1467 #if __USE_FORTIFY_LEVEL >= 1
1468 CHK_FAIL_START
1469 FD_ISSET (FD_SETSIZE, &s);
1470 CHK_FAIL_END
1471 #endif
1473 return ret;