.
[glibc/history.git] / debug / tst-chk1.c
blobe03f3dba6d6836648af808360e1e2195bccb548c
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 #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/socket.h>
33 #include <sys/un.h>
35 #define obstack_chunk_alloc malloc
36 #define obstack_chunk_free free
38 char *temp_filename;
39 static void do_prepare (void);
40 static int do_test (void);
41 #define PREPARE(argc, argv) do_prepare ()
42 #define TEST_FUNCTION do_test ()
43 #include "../test-skeleton.c"
45 static void
46 do_prepare (void)
48 int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
49 if (temp_fd == -1)
51 printf ("cannot create temporary file: %m\n");
52 exit (1);
55 const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
56 if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
58 puts ("could not write test strings into file");
59 unlink (temp_filename);
60 exit (1);
64 volatile int chk_fail_ok;
65 volatile int ret;
66 jmp_buf chk_fail_buf;
68 static void
69 handler (int sig)
71 if (chk_fail_ok)
73 chk_fail_ok = 0;
74 longjmp (chk_fail_buf, 1);
76 else
77 _exit (127);
80 char buf[10];
81 wchar_t wbuf[10];
82 volatile size_t l0;
83 volatile char *p;
84 volatile wchar_t *wp;
85 const char *str1 = "JIHGFEDCBA";
86 const char *str2 = "F";
87 const char *str3 = "%s%n%s%n";
88 const char *str4 = "Hello, ";
89 const char *str5 = "World!\n";
90 const wchar_t *wstr1 = L"JIHGFEDCBA";
91 const wchar_t *wstr2 = L"F";
92 const wchar_t *wstr3 = L"%s%n%s%n";
93 const wchar_t *wstr4 = L"Hello, ";
94 const wchar_t *wstr5 = L"World!\n";
95 char buf2[10] = "%s";
96 int num1 = 67;
97 int num2 = 987654;
99 #define FAIL() \
100 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
101 #define CHK_FAIL_START \
102 chk_fail_ok = 1; \
103 if (! setjmp (chk_fail_buf)) \
105 #define CHK_FAIL_END \
106 chk_fail_ok = 0; \
107 FAIL (); \
109 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
110 #define CHK_FAIL2_START CHK_FAIL_START
111 #define CHK_FAIL2_END CHK_FAIL_END
112 #else
113 #define CHK_FAIL2_START
114 #define CHK_FAIL2_END
115 #endif
117 static int
118 do_test (void)
120 struct sigaction sa;
121 sa.sa_handler = handler;
122 sa.sa_flags = 0;
123 sigemptyset (&sa.sa_mask);
125 sigaction (SIGABRT, &sa, NULL);
127 /* Avoid all the buffer overflow messages on stderr. */
128 int fd = open (_PATH_DEVNULL, O_WRONLY);
129 if (fd == -1)
130 close (STDERR_FILENO);
131 else
133 dup2 (fd, STDERR_FILENO);
134 close (fd);
136 setenv ("LIBC_FATAL_STDERR_", "1", 1);
138 struct A { char buf1[9]; char buf2[1]; } a;
139 struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
141 printf ("Test checking routines at fortify level %d\n",
142 #ifdef __USE_FORTIFY_LEVEL
143 (int) __USE_FORTIFY_LEVEL
144 #else
146 #endif
149 #if defined __USE_FORTIFY_LEVEL && !defined __extern_always_inline
150 printf ("Test skipped");
151 if (l0 == 0)
152 return 0;
153 #endif
155 /* These ops can be done without runtime checking of object size. */
156 memcpy (buf, "abcdefghij", 10);
157 memmove (buf + 1, buf, 9);
158 if (memcmp (buf, "aabcdefghi", 10))
159 FAIL ();
161 if (mempcpy (buf + 5, "abcde", 5) != buf + 10
162 || memcmp (buf, "aabcdabcde", 10))
163 FAIL ();
165 memset (buf + 8, 'j', 2);
166 if (memcmp (buf, "aabcdabcjj", 10))
167 FAIL ();
169 strcpy (buf + 4, "EDCBA");
170 if (memcmp (buf, "aabcEDCBA", 10))
171 FAIL ();
173 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
174 FAIL ();
176 strncpy (buf + 6, "X", 4);
177 if (memcmp (buf, "aabcEDX\0\0", 10))
178 FAIL ();
180 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
181 FAIL ();
183 if (snprintf (buf + 7, 3, "%s", "987654") != 6
184 || memcmp (buf, "aabcEDX98", 10))
185 FAIL ();
187 /* These ops need runtime checking, but shouldn't __chk_fail. */
188 memcpy (buf, "abcdefghij", l0 + 10);
189 memmove (buf + 1, buf, l0 + 9);
190 if (memcmp (buf, "aabcdefghi", 10))
191 FAIL ();
193 if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
194 || memcmp (buf, "aabcdabcde", 10))
195 FAIL ();
197 memset (buf + 8, 'j', l0 + 2);
198 if (memcmp (buf, "aabcdabcjj", 10))
199 FAIL ();
201 strcpy (buf + 4, str1 + 5);
202 if (memcmp (buf, "aabcEDCBA", 10))
203 FAIL ();
205 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
206 FAIL ();
208 strncpy (buf + 6, "X", l0 + 4);
209 if (memcmp (buf, "aabcEDX\0\0", 10))
210 FAIL ();
212 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
213 || memcmp (buf, "aabcEcd\0\0", 10))
214 FAIL ();
216 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
217 FAIL ();
219 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
220 FAIL ();
222 buf[l0 + 8] = '\0';
223 strcat (buf, "A");
224 if (memcmp (buf, "aabcEcd9A", 10))
225 FAIL ();
227 buf[l0 + 7] = '\0';
228 strncat (buf, "ZYXWV", l0 + 2);
229 if (memcmp (buf, "aabcEcdZY", 10))
230 FAIL ();
232 memcpy (a.buf1, "abcdefghij", l0 + 10);
233 memmove (a.buf1 + 1, a.buf1, l0 + 9);
234 if (memcmp (a.buf1, "aabcdefghi", 10))
235 FAIL ();
237 if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
238 || memcmp (a.buf1, "aabcdabcde", 10))
239 FAIL ();
241 memset (a.buf1 + 8, 'j', l0 + 2);
242 if (memcmp (a.buf1, "aabcdabcjj", 10))
243 FAIL ();
245 #if __USE_FORTIFY_LEVEL < 2
246 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
247 and sufficient GCC support, as the string operations overflow
248 from a.buf1 into a.buf2. */
249 strcpy (a.buf1 + 4, str1 + 5);
250 if (memcmp (a.buf1, "aabcEDCBA", 10))
251 FAIL ();
253 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
254 || memcmp (a.buf1, "aabcEDCBF", 10))
255 FAIL ();
257 strncpy (a.buf1 + 6, "X", l0 + 4);
258 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
259 FAIL ();
261 if (sprintf (a.buf1 + 7, "%d", num1) != 2
262 || memcmp (a.buf1, "aabcEDX67", 10))
263 FAIL ();
265 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
266 || memcmp (a.buf1, "aabcEDX98", 10))
267 FAIL ();
269 a.buf1[l0 + 8] = '\0';
270 strcat (a.buf1, "A");
271 if (memcmp (a.buf1, "aabcEDX9A", 10))
272 FAIL ();
274 a.buf1[l0 + 7] = '\0';
275 strncat (a.buf1, "ZYXWV", l0 + 2);
276 if (memcmp (a.buf1, "aabcEDXZY", 10))
277 FAIL ();
279 #endif
281 #if __USE_FORTIFY_LEVEL >= 1
282 /* Now check if all buffer overflows are caught at runtime. */
284 CHK_FAIL_START
285 memcpy (buf + 1, "abcdefghij", l0 + 10);
286 CHK_FAIL_END
288 CHK_FAIL_START
289 memmove (buf + 2, buf + 1, l0 + 9);
290 CHK_FAIL_END
292 CHK_FAIL_START
293 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
294 CHK_FAIL_END
296 CHK_FAIL_START
297 memset (buf + 9, 'j', l0 + 2);
298 CHK_FAIL_END
300 CHK_FAIL_START
301 strcpy (buf + 5, str1 + 5);
302 CHK_FAIL_END
304 CHK_FAIL_START
305 p = stpcpy (buf + 9, str2);
306 CHK_FAIL_END
308 CHK_FAIL_START
309 strncpy (buf + 7, "X", l0 + 4);
310 CHK_FAIL_END
312 CHK_FAIL_START
313 stpncpy (buf + 6, "cd", l0 + 5);
314 CHK_FAIL_END
316 # if !defined __cplusplus || defined __va_arg_pack
317 CHK_FAIL_START
318 sprintf (buf + 8, "%d", num1);
319 CHK_FAIL_END
321 CHK_FAIL_START
322 snprintf (buf + 8, l0 + 3, "%d", num2);
323 CHK_FAIL_END
325 CHK_FAIL_START
326 swprintf (wbuf + 8, 3, L"%d", num1);
327 CHK_FAIL_END
329 CHK_FAIL_START
330 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
331 CHK_FAIL_END
332 # endif
334 memcpy (buf, str1 + 2, l0 + 9);
335 CHK_FAIL_START
336 strcat (buf, "AB");
337 CHK_FAIL_END
339 memcpy (buf, str1 + 3, l0 + 8);
340 CHK_FAIL_START
341 strncat (buf, "ZYXWV", l0 + 3);
342 CHK_FAIL_END
344 CHK_FAIL_START
345 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
346 CHK_FAIL_END
348 CHK_FAIL_START
349 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
350 CHK_FAIL_END
352 CHK_FAIL_START
353 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
354 CHK_FAIL_END
356 CHK_FAIL_START
357 memset (a.buf1 + 9, 'j', l0 + 2);
358 CHK_FAIL_END
360 # if __USE_FORTIFY_LEVEL >= 2
361 # define O 0
362 # else
363 # define O 1
364 # endif
366 CHK_FAIL_START
367 strcpy (a.buf1 + (O + 4), str1 + 5);
368 CHK_FAIL_END
370 CHK_FAIL_START
371 p = stpcpy (a.buf1 + (O + 8), str2);
372 CHK_FAIL_END
374 CHK_FAIL_START
375 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
376 CHK_FAIL_END
378 # if !defined __cplusplus || defined __va_arg_pack
379 CHK_FAIL_START
380 sprintf (a.buf1 + (O + 7), "%d", num1);
381 CHK_FAIL_END
383 CHK_FAIL_START
384 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
385 CHK_FAIL_END
386 # endif
388 memcpy (a.buf1, str1 + (3 - O), l0 + 8 + O);
389 CHK_FAIL_START
390 strcat (a.buf1, "AB");
391 CHK_FAIL_END
393 memcpy (a.buf1, str1 + (4 - O), l0 + 7 + O);
394 CHK_FAIL_START
395 strncat (a.buf1, "ZYXWV", l0 + 3);
396 CHK_FAIL_END
397 #endif
400 /* These ops can be done without runtime checking of object size. */
401 wmemcpy (wbuf, L"abcdefghij", 10);
402 wmemmove (wbuf + 1, wbuf, 9);
403 if (wmemcmp (wbuf, L"aabcdefghi", 10))
404 FAIL ();
406 if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
407 || wmemcmp (wbuf, L"aabcdabcde", 10))
408 FAIL ();
410 wmemset (wbuf + 8, L'j', 2);
411 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
412 FAIL ();
414 wcscpy (wbuf + 4, L"EDCBA");
415 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
416 FAIL ();
418 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
419 FAIL ();
421 wcsncpy (wbuf + 6, L"X", 4);
422 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
423 FAIL ();
425 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
426 || wmemcmp (wbuf, L"aabcEDX98", 10))
427 FAIL ();
429 if (swprintf (wbuf + 7, 3, L"64") != 2
430 || wmemcmp (wbuf, L"aabcEDX64", 10))
431 FAIL ();
433 /* These ops need runtime checking, but shouldn't __chk_fail. */
434 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
435 wmemmove (wbuf + 1, wbuf, l0 + 9);
436 if (wmemcmp (wbuf, L"aabcdefghi", 10))
437 FAIL ();
439 if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
440 || wmemcmp (wbuf, L"aabcdabcde", 10))
441 FAIL ();
443 wmemset (wbuf + 8, L'j', l0 + 2);
444 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
445 FAIL ();
447 wcscpy (wbuf + 4, wstr1 + 5);
448 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
449 FAIL ();
451 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
452 FAIL ();
454 wcsncpy (wbuf + 6, L"X", l0 + 4);
455 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
456 FAIL ();
458 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
459 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
460 FAIL ();
462 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
463 || wmemcmp (wbuf, L"aabcEcd98", 10))
464 FAIL ();
466 wbuf[l0 + 8] = L'\0';
467 wcscat (wbuf, L"A");
468 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
469 FAIL ();
471 wbuf[l0 + 7] = L'\0';
472 wcsncat (wbuf, L"ZYXWV", l0 + 2);
473 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
474 FAIL ();
476 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
477 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
478 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
479 FAIL ();
481 if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
482 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
483 FAIL ();
485 wmemset (wa.buf1 + 8, L'j', l0 + 2);
486 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
487 FAIL ();
489 #if __USE_FORTIFY_LEVEL < 2
490 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
491 and sufficient GCC support, as the string operations overflow
492 from a.buf1 into a.buf2. */
493 wcscpy (wa.buf1 + 4, wstr1 + 5);
494 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
495 FAIL ();
497 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
498 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
499 FAIL ();
501 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
502 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
503 FAIL ();
505 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
506 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
507 FAIL ();
509 wa.buf1[l0 + 8] = L'\0';
510 wcscat (wa.buf1, L"A");
511 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
512 FAIL ();
514 wa.buf1[l0 + 7] = L'\0';
515 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
516 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
517 FAIL ();
519 #endif
521 #if __USE_FORTIFY_LEVEL >= 1
522 /* Now check if all buffer overflows are caught at runtime. */
524 CHK_FAIL_START
525 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
526 CHK_FAIL_END
528 CHK_FAIL_START
529 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
530 CHK_FAIL_END
532 CHK_FAIL_START
533 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
534 CHK_FAIL_END
536 CHK_FAIL_START
537 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
538 CHK_FAIL_END
540 CHK_FAIL_START
541 wmemset (wbuf + 9, L'j', l0 + 2);
542 CHK_FAIL_END
544 CHK_FAIL_START
545 wcscpy (wbuf + 5, wstr1 + 5);
546 CHK_FAIL_END
548 CHK_FAIL_START
549 wp = wcpcpy (wbuf + 9, wstr2);
550 CHK_FAIL_END
552 CHK_FAIL_START
553 wcsncpy (wbuf + 7, L"X", l0 + 4);
554 CHK_FAIL_END
556 CHK_FAIL_START
557 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
558 CHK_FAIL_END
560 CHK_FAIL_START
561 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
562 CHK_FAIL_END
564 CHK_FAIL_START
565 wcpncpy (wbuf + 6, L"cd", l0 + 5);
566 CHK_FAIL_END
568 wmemcpy (wbuf, wstr1 + 2, l0 + 9);
569 CHK_FAIL_START
570 wcscat (wbuf, L"AB");
571 CHK_FAIL_END
573 wmemcpy (wbuf, wstr1 + 3, l0 + 8);
574 CHK_FAIL_START
575 wcsncat (wbuf, L"ZYXWV", l0 + 3);
576 CHK_FAIL_END
578 CHK_FAIL_START
579 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
580 CHK_FAIL_END
582 CHK_FAIL_START
583 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
584 CHK_FAIL_END
586 CHK_FAIL_START
587 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
588 CHK_FAIL_END
590 CHK_FAIL_START
591 wmemset (wa.buf1 + 9, L'j', l0 + 2);
592 CHK_FAIL_END
594 #if __USE_FORTIFY_LEVEL >= 2
595 # define O 0
596 #else
597 # define O 1
598 #endif
600 CHK_FAIL_START
601 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
602 CHK_FAIL_END
604 CHK_FAIL_START
605 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
606 CHK_FAIL_END
608 CHK_FAIL_START
609 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
610 CHK_FAIL_END
612 wmemcpy (wa.buf1, wstr1 + (3 - O), l0 + 8 + O);
613 CHK_FAIL_START
614 wcscat (wa.buf1, L"AB");
615 CHK_FAIL_END
617 wmemcpy (wa.buf1, wstr1 + (4 - O), l0 + 7 + O);
618 CHK_FAIL_START
619 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
620 CHK_FAIL_END
621 #endif
624 /* Now checks for %n protection. */
626 /* Constant literals passed directly are always ok
627 (even with warnings about possible bugs from GCC). */
628 int n1, n2;
629 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
630 || n1 != 1 || n2 != 2)
631 FAIL ();
633 /* In this case the format string is not known at compile time,
634 but resides in read-only memory, so is ok. */
635 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
636 || n1 != 1 || n2 != 2)
637 FAIL ();
639 strcpy (buf2 + 2, "%n%s%n");
640 /* When the format string is writable and contains %n,
641 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
642 CHK_FAIL2_START
643 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
644 FAIL ();
645 CHK_FAIL2_END
647 CHK_FAIL2_START
648 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
649 FAIL ();
650 CHK_FAIL2_END
652 /* But if there is no %n, even writable format string
653 should work. */
654 buf2[6] = '\0';
655 if (sprintf (buf, buf2 + 4, str2) != 1)
656 FAIL ();
658 /* Constant literals passed directly are always ok
659 (even with warnings about possible bugs from GCC). */
660 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
661 || n1 != 7 || n2 != 14)
662 FAIL ();
664 /* In this case the format string is not known at compile time,
665 but resides in read-only memory, so is ok. */
666 if (printf (str3, str4, &n1, str5, &n2) != 14
667 || n1 != 7 || n2 != 14)
668 FAIL ();
670 strcpy (buf2 + 2, "%n%s%n");
671 /* When the format string is writable and contains %n,
672 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
673 CHK_FAIL2_START
674 if (printf (buf2, str4, &n1, str5, &n1) != 14)
675 FAIL ();
676 CHK_FAIL2_END
678 /* But if there is no %n, even writable format string
679 should work. */
680 buf2[6] = '\0';
681 if (printf (buf2 + 4, str5) != 7)
682 FAIL ();
684 FILE *fp = stdout;
686 /* Constant literals passed directly are always ok
687 (even with warnings about possible bugs from GCC). */
688 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
689 || n1 != 7 || n2 != 14)
690 FAIL ();
692 /* In this case the format string is not known at compile time,
693 but resides in read-only memory, so is ok. */
694 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
695 || n1 != 7 || n2 != 14)
696 FAIL ();
698 strcpy (buf2 + 2, "%n%s%n");
699 /* When the format string is writable and contains %n,
700 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
701 CHK_FAIL2_START
702 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
703 FAIL ();
704 CHK_FAIL2_END
706 /* But if there is no %n, even writable format string
707 should work. */
708 buf2[6] = '\0';
709 if (fprintf (fp, buf2 + 4, str5) != 7)
710 FAIL ();
712 char *my_ptr = NULL;
713 strcpy (buf2 + 2, "%n%s%n");
714 /* When the format string is writable and contains %n,
715 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
716 CHK_FAIL2_START
717 if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
718 FAIL ();
719 else
720 free (my_ptr);
721 CHK_FAIL2_END
723 struct obstack obs;
724 obstack_init (&obs);
725 CHK_FAIL2_START
726 if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
727 FAIL ();
728 CHK_FAIL2_END
729 obstack_free (&obs, NULL);
731 my_ptr = NULL;
732 if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
733 FAIL ();
734 else
735 free (my_ptr);
737 obstack_init (&obs);
738 if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
739 FAIL ();
740 obstack_free (&obs, NULL);
742 if (freopen (temp_filename, "r", stdin) == NULL)
744 puts ("could not open temporary file");
745 exit (1);
748 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
749 FAIL ();
750 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
751 FAIL ();
753 #if __USE_FORTIFY_LEVEL >= 1
754 CHK_FAIL_START
755 if (gets (buf) != buf)
756 FAIL ();
757 CHK_FAIL_END
758 #endif
760 rewind (stdin);
762 if (fgets (buf, sizeof (buf), stdin) != buf
763 || memcmp (buf, "abcdefgh\n", 10))
764 FAIL ();
765 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
766 FAIL ();
768 rewind (stdin);
770 if (fgets (buf, l0 + sizeof (buf), stdin) != buf
771 || memcmp (buf, "abcdefgh\n", 10))
772 FAIL ();
774 #if __USE_FORTIFY_LEVEL >= 1
775 CHK_FAIL_START
776 if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
777 FAIL ();
778 CHK_FAIL_END
780 CHK_FAIL_START
781 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
782 FAIL ();
783 CHK_FAIL_END
784 #endif
786 rewind (stdin);
788 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
789 || memcmp (buf, "abcdefgh\n", 10))
790 FAIL ();
791 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
792 || memcmp (buf, "ABCDEFGHI", 10))
793 FAIL ();
795 rewind (stdin);
797 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
798 || memcmp (buf, "abcdefgh\n", 10))
799 FAIL ();
801 #if __USE_FORTIFY_LEVEL >= 1
802 CHK_FAIL_START
803 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
804 FAIL ();
805 CHK_FAIL_END
807 CHK_FAIL_START
808 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
809 FAIL ();
810 CHK_FAIL_END
811 #endif
813 rewind (stdin);
815 if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
816 || memcmp (buf, "abcdefgh\nA", 10))
817 FAIL ();
818 if (fread (buf, sizeof (buf), 1, stdin) != 1
819 || memcmp (buf, "BCDEFGHI\na", 10))
820 FAIL ();
822 rewind (stdin);
824 if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
825 || memcmp (buf, "abcdefgh\nA", 10))
826 FAIL ();
827 if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
828 || memcmp (buf, "BCDEFGHI\na", 10))
829 FAIL ();
831 #if __USE_FORTIFY_LEVEL >= 1
832 CHK_FAIL_START
833 if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
834 FAIL ();
835 CHK_FAIL_END
837 CHK_FAIL_START
838 if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
839 FAIL ();
840 CHK_FAIL_END
841 #endif
843 rewind (stdin);
845 if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
846 || memcmp (buf, "abcdefgh\nA", 10))
847 FAIL ();
848 if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
849 || memcmp (buf, "BCDEFGHI\na", 10))
850 FAIL ();
852 rewind (stdin);
854 if (fread_unlocked (buf, 1, 4, stdin) != 4
855 || memcmp (buf, "abcdFGHI\na", 10))
856 FAIL ();
857 if (fread_unlocked (buf, 4, 1, stdin) != 1
858 || memcmp (buf, "efghFGHI\na", 10))
859 FAIL ();
861 rewind (stdin);
863 if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
864 || memcmp (buf, "abcdefgh\nA", 10))
865 FAIL ();
866 if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
867 || memcmp (buf, "BCDEFGHI\na", 10))
868 FAIL ();
870 #if __USE_FORTIFY_LEVEL >= 1
871 CHK_FAIL_START
872 if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
873 FAIL ();
874 CHK_FAIL_END
876 CHK_FAIL_START
877 if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
878 FAIL ();
879 CHK_FAIL_END
880 #endif
882 lseek (fileno (stdin), 0, SEEK_SET);
884 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
885 || memcmp (buf, "abcdefgh\n", 9))
886 FAIL ();
887 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
888 || memcmp (buf, "ABCDEFGHI", 9))
889 FAIL ();
891 lseek (fileno (stdin), 0, SEEK_SET);
893 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
894 || memcmp (buf, "abcdefgh\n", 9))
895 FAIL ();
897 #if __USE_FORTIFY_LEVEL >= 1
898 CHK_FAIL_START
899 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
900 FAIL ();
901 CHK_FAIL_END
902 #endif
904 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
905 != sizeof (buf) - 1
906 || memcmp (buf, "\nABCDEFGH", 9))
907 FAIL ();
908 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
909 || memcmp (buf, "abcdefgh\n", 9))
910 FAIL ();
911 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
912 != sizeof (buf) - 1
913 || memcmp (buf, "h\nABCDEFG", 9))
914 FAIL ();
916 #if __USE_FORTIFY_LEVEL >= 1
917 CHK_FAIL_START
918 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
919 != sizeof (buf) + 1)
920 FAIL ();
921 CHK_FAIL_END
922 #endif
924 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
925 != sizeof (buf) - 1
926 || memcmp (buf, "\nABCDEFGH", 9))
927 FAIL ();
928 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
929 || memcmp (buf, "abcdefgh\n", 9))
930 FAIL ();
931 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
932 != sizeof (buf) - 1
933 || memcmp (buf, "h\nABCDEFG", 9))
934 FAIL ();
936 #if __USE_FORTIFY_LEVEL >= 1
937 CHK_FAIL_START
938 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
939 != sizeof (buf) + 1)
940 FAIL ();
941 CHK_FAIL_END
942 #endif
944 if (freopen (temp_filename, "r", stdin) == NULL)
946 puts ("could not open temporary file");
947 exit (1);
950 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
952 puts ("could not seek in test file");
953 exit (1);
956 #if __USE_FORTIFY_LEVEL >= 1
957 CHK_FAIL_START
958 if (gets (buf) != buf)
959 FAIL ();
960 CHK_FAIL_END
961 #endif
963 /* Check whether missing N$ formats are detected. */
964 CHK_FAIL2_START
965 printf ("%3$d\n", 1, 2, 3, 4);
966 CHK_FAIL2_END
968 CHK_FAIL2_START
969 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
970 CHK_FAIL2_END
972 CHK_FAIL2_START
973 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
974 CHK_FAIL2_END
976 CHK_FAIL2_START
977 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
978 CHK_FAIL2_END
980 int sp[2];
981 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
982 FAIL ();
983 else
985 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
986 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
987 != strlen (sendstr))
988 FAIL ();
990 char recvbuf[12];
991 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
992 != sizeof recvbuf
993 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
994 FAIL ();
996 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
997 != sizeof recvbuf - 7
998 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
999 FAIL ();
1001 #if __USE_FORTIFY_LEVEL >= 1
1002 CHK_FAIL_START
1003 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
1004 != sizeof recvbuf)
1005 FAIL ();
1006 CHK_FAIL_END
1008 CHK_FAIL_START
1009 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
1010 != sizeof recvbuf - 3)
1011 FAIL ();
1012 CHK_FAIL_END
1013 #endif
1015 socklen_t sl;
1016 struct sockaddr_un sa_un;
1018 sl = sizeof (sa_un);
1019 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
1020 (struct sockaddr *) &sa_un, &sl)
1021 != sizeof recvbuf
1022 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1023 FAIL ();
1025 sl = sizeof (sa_un);
1026 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
1027 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
1028 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1029 FAIL ();
1031 #if __USE_FORTIFY_LEVEL >= 1
1032 CHK_FAIL_START
1033 sl = sizeof (sa_un);
1034 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1035 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1036 FAIL ();
1037 CHK_FAIL_END
1039 CHK_FAIL_START
1040 sl = sizeof (sa_un);
1041 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1042 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1043 FAIL ();
1044 CHK_FAIL_END
1045 #endif
1047 close (sp[0]);
1048 close (sp[1]);
1051 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1052 char *enddir = strchr (fname, '\0');
1053 if (mkdtemp (fname) == NULL)
1055 printf ("mkdtemp failed: %m\n");
1056 return 1;
1058 *enddir = '/';
1059 if (symlink ("bar", fname) != 0)
1060 FAIL ();
1062 char readlinkbuf[4];
1063 if (readlink (fname, readlinkbuf, 4) != 3
1064 || memcmp (readlinkbuf, "bar", 3) != 0)
1065 FAIL ();
1066 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1067 || memcmp (readlinkbuf, "bbar", 4) != 0)
1068 FAIL ();
1070 #if __USE_FORTIFY_LEVEL >= 1
1071 CHK_FAIL_START
1072 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1073 FAIL ();
1074 CHK_FAIL_END
1076 CHK_FAIL_START
1077 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1078 FAIL ();
1079 CHK_FAIL_END
1080 #endif
1082 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1083 if (tmpfd < 0)
1084 FAIL ();
1086 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1087 || memcmp (readlinkbuf, "bar", 3) != 0)
1088 FAIL ();
1089 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1090 l0 + 3) != 3
1091 || memcmp (readlinkbuf, "bbar", 4) != 0)
1092 FAIL ();
1094 #if __USE_FORTIFY_LEVEL >= 1
1095 CHK_FAIL_START
1096 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1097 l0 + 3) != 3)
1098 FAIL ();
1099 CHK_FAIL_END
1101 CHK_FAIL_START
1102 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1103 4) != 3)
1104 FAIL ();
1105 CHK_FAIL_END
1106 #endif
1108 close (tmpfd);
1110 char *cwd1 = getcwd (NULL, 0);
1111 if (cwd1 == NULL)
1112 FAIL ();
1114 char *cwd2 = getcwd (NULL, 250);
1115 if (cwd2 == NULL)
1116 FAIL ();
1118 if (cwd1 && cwd2)
1120 if (strcmp (cwd1, cwd2) != 0)
1121 FAIL ();
1123 *enddir = '\0';
1124 if (chdir (fname))
1125 FAIL ();
1127 char *cwd3 = getcwd (NULL, 0);
1128 if (cwd3 == NULL)
1129 FAIL ();
1130 if (strcmp (fname, cwd3) != 0)
1131 printf ("getcwd after chdir is '%s' != '%s',"
1132 "get{c,}wd tests skipped\n", cwd3, fname);
1133 else
1135 char getcwdbuf[sizeof fname - 3];
1137 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1138 if (cwd4 != getcwdbuf
1139 || strcmp (getcwdbuf, fname) != 0)
1140 FAIL ();
1142 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1143 if (cwd4 != getcwdbuf + 1
1144 || getcwdbuf[0] != fname[0]
1145 || strcmp (getcwdbuf + 1, fname) != 0)
1146 FAIL ();
1148 #if __USE_FORTIFY_LEVEL >= 1
1149 CHK_FAIL_START
1150 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1151 != getcwdbuf + 2)
1152 FAIL ();
1153 CHK_FAIL_END
1155 CHK_FAIL_START
1156 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1157 != getcwdbuf + 2)
1158 FAIL ();
1159 CHK_FAIL_END
1160 #endif
1162 if (getwd (getcwdbuf) != getcwdbuf
1163 || strcmp (getcwdbuf, fname) != 0)
1164 FAIL ();
1166 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1167 || strcmp (getcwdbuf + 1, fname) != 0)
1168 FAIL ();
1170 #if __USE_FORTIFY_LEVEL >= 1
1171 CHK_FAIL_START
1172 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1173 FAIL ();
1174 CHK_FAIL_END
1175 #endif
1178 if (chdir (cwd1) != 0)
1179 FAIL ();
1180 free (cwd3);
1183 free (cwd1);
1184 free (cwd2);
1185 *enddir = '/';
1186 if (unlink (fname) != 0)
1187 FAIL ();
1189 *enddir = '\0';
1190 if (rmdir (fname) != 0)
1191 FAIL ();
1194 #if PATH_MAX > 0
1195 char largebuf[PATH_MAX];
1196 char *realres = realpath (".", largebuf);
1197 if (realres != largebuf)
1198 FAIL ();
1200 # if __USE_FORTIFY_LEVEL >= 1
1201 CHK_FAIL_START
1202 char realbuf[1];
1203 realres = realpath (".", realbuf);
1204 if (realres != realbuf)
1205 FAIL ();
1206 CHK_FAIL_END
1207 # endif
1208 #endif
1210 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1212 assert (MB_CUR_MAX <= 10);
1214 /* First a simple test. */
1215 char enough[10];
1216 if (wctomb (enough, L'A') != 1)
1217 FAIL ();
1219 #if __USE_FORTIFY_LEVEL >= 1
1220 /* We know the wchar_t encoding is ISO 10646. So pick a
1221 character which has a multibyte representation which does not
1222 fit. */
1223 CHK_FAIL_START
1224 char smallbuf[2];
1225 if (wctomb (smallbuf, L'\x100') != 2)
1226 FAIL ();
1227 CHK_FAIL_END
1228 #endif
1230 mbstate_t s;
1231 memset (&s, '\0', sizeof (s));
1232 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1233 FAIL ();
1235 #if __USE_FORTIFY_LEVEL >= 1
1236 /* We know the wchar_t encoding is ISO 10646. So pick a
1237 character which has a multibyte representation which does not
1238 fit. */
1239 CHK_FAIL_START
1240 char smallbuf[2];
1241 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1242 FAIL ();
1243 CHK_FAIL_END
1244 #endif
1246 wchar_t wenough[10];
1247 memset (&s, '\0', sizeof (s));
1248 const char *cp = "A";
1249 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1250 || wcscmp (wenough, L"A") != 0)
1251 FAIL ();
1253 cp = "BC";
1254 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1255 || wcscmp (wenough, L"BC") != 0)
1256 FAIL ();
1258 #if __USE_FORTIFY_LEVEL >= 1
1259 CHK_FAIL_START
1260 wchar_t wsmallbuf[2];
1261 cp = "ABC";
1262 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1263 CHK_FAIL_END
1264 #endif
1266 cp = "A";
1267 if (mbstowcs (wenough, cp, 10) != 1
1268 || wcscmp (wenough, L"A") != 0)
1269 FAIL ();
1271 cp = "DEF";
1272 if (mbstowcs (wenough, cp, l0 + 10) != 3
1273 || wcscmp (wenough, L"DEF") != 0)
1274 FAIL ();
1276 #if __USE_FORTIFY_LEVEL >= 1
1277 CHK_FAIL_START
1278 wchar_t wsmallbuf[2];
1279 cp = "ABC";
1280 mbstowcs (wsmallbuf, cp, 10);
1281 CHK_FAIL_END
1282 #endif
1284 memset (&s, '\0', sizeof (s));
1285 cp = "ABC";
1286 wcscpy (wenough, L"DEF");
1287 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1288 || wcscmp (wenough, L"AEF") != 0)
1289 FAIL ();
1291 cp = "IJ";
1292 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1293 || wcscmp (wenough, L"IEF") != 0)
1294 FAIL ();
1296 #if __USE_FORTIFY_LEVEL >= 1
1297 CHK_FAIL_START
1298 wchar_t wsmallbuf[2];
1299 cp = "ABC";
1300 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1301 CHK_FAIL_END
1302 #endif
1304 memset (&s, '\0', sizeof (s));
1305 const wchar_t *wcp = L"A";
1306 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1307 || strcmp (enough, "A") != 0)
1308 FAIL ();
1310 wcp = L"BC";
1311 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1312 || strcmp (enough, "BC") != 0)
1313 FAIL ();
1315 #if __USE_FORTIFY_LEVEL >= 1
1316 CHK_FAIL_START
1317 char smallbuf[2];
1318 wcp = L"ABC";
1319 wcsrtombs (smallbuf, &wcp, 10, &s);
1320 CHK_FAIL_END
1321 #endif
1323 memset (enough, 'Z', sizeof (enough));
1324 wcp = L"EF";
1325 if (wcstombs (enough, wcp, 10) != 2
1326 || strcmp (enough, "EF") != 0)
1327 FAIL ();
1329 wcp = L"G";
1330 if (wcstombs (enough, wcp, l0 + 10) != 1
1331 || strcmp (enough, "G") != 0)
1332 FAIL ();
1334 #if __USE_FORTIFY_LEVEL >= 1
1335 CHK_FAIL_START
1336 char smallbuf[2];
1337 wcp = L"ABC";
1338 wcstombs (smallbuf, wcp, 10);
1339 CHK_FAIL_END
1340 #endif
1342 memset (&s, '\0', sizeof (s));
1343 wcp = L"AB";
1344 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1345 || strcmp (enough, "A") != 0)
1346 FAIL ();
1348 wcp = L"BCD";
1349 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1350 || strcmp (enough, "B") != 0)
1351 FAIL ();
1353 #if __USE_FORTIFY_LEVEL >= 1
1354 CHK_FAIL_START
1355 char smallbuf[2];
1356 wcp = L"ABC";
1357 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1358 CHK_FAIL_END
1359 #endif
1361 else
1363 puts ("cannot set locale");
1364 ret = 1;
1367 fd = posix_openpt (O_RDWR);
1368 if (fd != -1)
1370 char enough[1000];
1371 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1372 FAIL ();
1374 #if __USE_FORTIFY_LEVEL >= 1
1375 CHK_FAIL_START
1376 char smallbuf[2];
1377 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1378 FAIL ();
1379 CHK_FAIL_END
1380 #endif
1381 close (fd);
1384 #if PATH_MAX > 0
1385 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1386 # if __USE_FORTIFY_LEVEL >= 1
1387 CHK_FAIL_START
1388 char smallbuf[1];
1389 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1390 CHK_FAIL_END
1391 # endif
1392 #endif
1394 gid_t grpslarge[5];
1395 int ngr = getgroups (5, grpslarge);
1396 asm volatile ("" : : "r" (ngr));
1397 #if __USE_FORTIFY_LEVEL >= 1
1398 CHK_FAIL_START
1399 char smallbuf[1];
1400 ngr = getgroups (5, (gid_t *) smallbuf);
1401 asm volatile ("" : : "r" (ngr));
1402 CHK_FAIL_END
1403 #endif
1405 fd = open (_PATH_TTY, O_RDONLY);
1406 if (fd != -1)
1408 char enough[1000];
1409 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1410 FAIL ();
1412 #if __USE_FORTIFY_LEVEL >= 1
1413 CHK_FAIL_START
1414 char smallbuf[2];
1415 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1416 FAIL ();
1417 CHK_FAIL_END
1418 #endif
1419 close (fd);
1422 char hostnamelarge[1000];
1423 gethostname (hostnamelarge, sizeof (hostnamelarge));
1424 #if __USE_FORTIFY_LEVEL >= 1
1425 CHK_FAIL_START
1426 char smallbuf[1];
1427 gethostname (smallbuf, sizeof (hostnamelarge));
1428 CHK_FAIL_END
1429 #endif
1431 char loginlarge[1000];
1432 getlogin_r (loginlarge, sizeof (hostnamelarge));
1433 #if __USE_FORTIFY_LEVEL >= 1
1434 CHK_FAIL_START
1435 char smallbuf[1];
1436 getlogin_r (smallbuf, sizeof (loginlarge));
1437 CHK_FAIL_END
1438 #endif
1440 char domainnamelarge[1000];
1441 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1442 asm volatile ("" : : "r" (res));
1443 #if __USE_FORTIFY_LEVEL >= 1
1444 CHK_FAIL_START
1445 char smallbuf[1];
1446 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1447 asm volatile ("" : : "r" (res));
1448 CHK_FAIL_END
1449 #endif
1451 return ret;