Update copyright dates with scripts/update-copyrights.
[glibc.git] / debug / tst-chk1.c
blob53559e6bcaedd023cac1a7f3cb5114b7457156d0
1 /* Copyright (C) 2004-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <fcntl.h>
21 #include <locale.h>
22 #include <obstack.h>
23 #include <setjmp.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <wchar.h>
30 #include <sys/poll.h>
31 #include <sys/select.h>
32 #include <sys/socket.h>
33 #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 set_fortify_handler (handler);
123 struct A { char buf1[9]; char buf2[1]; } a;
124 struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
126 printf ("Test checking routines at fortify level %d\n",
127 #ifdef __USE_FORTIFY_LEVEL
128 (int) __USE_FORTIFY_LEVEL
129 #else
131 #endif
134 #if defined __USE_FORTIFY_LEVEL && !defined __fortify_function
135 printf ("Test skipped");
136 if (l0 == 0)
137 return 0;
138 #endif
140 /* These ops can be done without runtime checking of object size. */
141 memcpy (buf, "abcdefghij", 10);
142 memmove (buf + 1, buf, 9);
143 if (memcmp (buf, "aabcdefghi", 10))
144 FAIL ();
146 if (mempcpy (buf + 5, "abcde", 5) != buf + 10
147 || memcmp (buf, "aabcdabcde", 10))
148 FAIL ();
150 memset (buf + 8, 'j', 2);
151 if (memcmp (buf, "aabcdabcjj", 10))
152 FAIL ();
154 strcpy (buf + 4, "EDCBA");
155 if (memcmp (buf, "aabcEDCBA", 10))
156 FAIL ();
158 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
159 FAIL ();
161 strncpy (buf + 6, "X", 4);
162 if (memcmp (buf, "aabcEDX\0\0", 10))
163 FAIL ();
165 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
166 FAIL ();
168 if (snprintf (buf + 7, 3, "%s", "987654") != 6
169 || memcmp (buf, "aabcEDX98", 10))
170 FAIL ();
172 /* These ops need runtime checking, but shouldn't __chk_fail. */
173 memcpy (buf, "abcdefghij", l0 + 10);
174 memmove (buf + 1, buf, l0 + 9);
175 if (memcmp (buf, "aabcdefghi", 10))
176 FAIL ();
178 if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
179 || memcmp (buf, "aabcdabcde", 10))
180 FAIL ();
182 memset (buf + 8, 'j', l0 + 2);
183 if (memcmp (buf, "aabcdabcjj", 10))
184 FAIL ();
186 strcpy (buf + 4, str1 + 5);
187 if (memcmp (buf, "aabcEDCBA", 10))
188 FAIL ();
190 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
191 FAIL ();
193 strncpy (buf + 6, "X", l0 + 4);
194 if (memcmp (buf, "aabcEDX\0\0", 10))
195 FAIL ();
197 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
198 || memcmp (buf, "aabcEcd\0\0", 10))
199 FAIL ();
201 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
202 FAIL ();
204 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
205 FAIL ();
207 buf[l0 + 8] = '\0';
208 strcat (buf, "A");
209 if (memcmp (buf, "aabcEcd9A", 10))
210 FAIL ();
212 buf[l0 + 7] = '\0';
213 strncat (buf, "ZYXWV", l0 + 2);
214 if (memcmp (buf, "aabcEcdZY", 10))
215 FAIL ();
217 memcpy (a.buf1, "abcdefghij", l0 + 10);
218 memmove (a.buf1 + 1, a.buf1, l0 + 9);
219 if (memcmp (a.buf1, "aabcdefghi", 10))
220 FAIL ();
222 if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
223 || memcmp (a.buf1, "aabcdabcde", 10))
224 FAIL ();
226 memset (a.buf1 + 8, 'j', l0 + 2);
227 if (memcmp (a.buf1, "aabcdabcjj", 10))
228 FAIL ();
230 #if __USE_FORTIFY_LEVEL < 2
231 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
232 and sufficient GCC support, as the string operations overflow
233 from a.buf1 into a.buf2. */
234 strcpy (a.buf1 + 4, str1 + 5);
235 if (memcmp (a.buf1, "aabcEDCBA", 10))
236 FAIL ();
238 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
239 || memcmp (a.buf1, "aabcEDCBF", 10))
240 FAIL ();
242 strncpy (a.buf1 + 6, "X", l0 + 4);
243 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
244 FAIL ();
246 if (sprintf (a.buf1 + 7, "%d", num1) != 2
247 || memcmp (a.buf1, "aabcEDX67", 10))
248 FAIL ();
250 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
251 || memcmp (a.buf1, "aabcEDX98", 10))
252 FAIL ();
254 a.buf1[l0 + 8] = '\0';
255 strcat (a.buf1, "A");
256 if (memcmp (a.buf1, "aabcEDX9A", 10))
257 FAIL ();
259 a.buf1[l0 + 7] = '\0';
260 strncat (a.buf1, "ZYXWV", l0 + 2);
261 if (memcmp (a.buf1, "aabcEDXZY", 10))
262 FAIL ();
264 #endif
266 #if __USE_FORTIFY_LEVEL >= 1
267 /* Now check if all buffer overflows are caught at runtime. */
269 CHK_FAIL_START
270 memcpy (buf + 1, "abcdefghij", l0 + 10);
271 CHK_FAIL_END
273 CHK_FAIL_START
274 memmove (buf + 2, buf + 1, l0 + 9);
275 CHK_FAIL_END
277 CHK_FAIL_START
278 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
279 CHK_FAIL_END
281 CHK_FAIL_START
282 memset (buf + 9, 'j', l0 + 2);
283 CHK_FAIL_END
285 CHK_FAIL_START
286 strcpy (buf + 5, str1 + 5);
287 CHK_FAIL_END
289 CHK_FAIL_START
290 p = stpcpy (buf + 9, str2);
291 CHK_FAIL_END
293 CHK_FAIL_START
294 strncpy (buf + 7, "X", l0 + 4);
295 CHK_FAIL_END
297 CHK_FAIL_START
298 stpncpy (buf + 6, "cd", l0 + 5);
299 CHK_FAIL_END
301 # if !defined __cplusplus || defined __va_arg_pack
302 CHK_FAIL_START
303 sprintf (buf + 8, "%d", num1);
304 CHK_FAIL_END
306 CHK_FAIL_START
307 snprintf (buf + 8, l0 + 3, "%d", num2);
308 CHK_FAIL_END
310 CHK_FAIL_START
311 swprintf (wbuf + 8, 3, L"%d", num1);
312 CHK_FAIL_END
314 CHK_FAIL_START
315 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
316 CHK_FAIL_END
317 # endif
319 memcpy (buf, str1 + 2, l0 + 9);
320 CHK_FAIL_START
321 strcat (buf, "AB");
322 CHK_FAIL_END
324 memcpy (buf, str1 + 3, l0 + 8);
325 CHK_FAIL_START
326 strncat (buf, "ZYXWV", l0 + 3);
327 CHK_FAIL_END
329 CHK_FAIL_START
330 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
331 CHK_FAIL_END
333 CHK_FAIL_START
334 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
335 CHK_FAIL_END
337 CHK_FAIL_START
338 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
339 CHK_FAIL_END
341 CHK_FAIL_START
342 memset (a.buf1 + 9, 'j', l0 + 2);
343 CHK_FAIL_END
345 # if __USE_FORTIFY_LEVEL >= 2
346 # define O 0
347 # else
348 # define O 1
349 # endif
351 CHK_FAIL_START
352 strcpy (a.buf1 + (O + 4), str1 + 5);
353 CHK_FAIL_END
355 CHK_FAIL_START
356 p = stpcpy (a.buf1 + (O + 8), str2);
357 CHK_FAIL_END
359 CHK_FAIL_START
360 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
361 CHK_FAIL_END
363 # if !defined __cplusplus || defined __va_arg_pack
364 CHK_FAIL_START
365 sprintf (a.buf1 + (O + 7), "%d", num1);
366 CHK_FAIL_END
368 CHK_FAIL_START
369 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
370 CHK_FAIL_END
371 # endif
373 memcpy (a.buf1, str1 + (3 - O), l0 + 8 + O);
374 CHK_FAIL_START
375 strcat (a.buf1, "AB");
376 CHK_FAIL_END
378 memcpy (a.buf1, str1 + (4 - O), l0 + 7 + O);
379 CHK_FAIL_START
380 strncat (a.buf1, "ZYXWV", l0 + 3);
381 CHK_FAIL_END
382 #endif
385 /* These ops can be done without runtime checking of object size. */
386 wmemcpy (wbuf, L"abcdefghij", 10);
387 wmemmove (wbuf + 1, wbuf, 9);
388 if (wmemcmp (wbuf, L"aabcdefghi", 10))
389 FAIL ();
391 if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
392 || wmemcmp (wbuf, L"aabcdabcde", 10))
393 FAIL ();
395 wmemset (wbuf + 8, L'j', 2);
396 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
397 FAIL ();
399 wcscpy (wbuf + 4, L"EDCBA");
400 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
401 FAIL ();
403 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
404 FAIL ();
406 wcsncpy (wbuf + 6, L"X", 4);
407 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
408 FAIL ();
410 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
411 || wmemcmp (wbuf, L"aabcEDX98", 10))
412 FAIL ();
414 if (swprintf (wbuf + 7, 3, L"64") != 2
415 || wmemcmp (wbuf, L"aabcEDX64", 10))
416 FAIL ();
418 /* These ops need runtime checking, but shouldn't __chk_fail. */
419 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
420 wmemmove (wbuf + 1, wbuf, l0 + 9);
421 if (wmemcmp (wbuf, L"aabcdefghi", 10))
422 FAIL ();
424 if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
425 || wmemcmp (wbuf, L"aabcdabcde", 10))
426 FAIL ();
428 wmemset (wbuf + 8, L'j', l0 + 2);
429 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
430 FAIL ();
432 wcscpy (wbuf + 4, wstr1 + 5);
433 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
434 FAIL ();
436 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
437 FAIL ();
439 wcsncpy (wbuf + 6, L"X", l0 + 4);
440 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
441 FAIL ();
443 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
444 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
445 FAIL ();
447 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
448 || wmemcmp (wbuf, L"aabcEcd98", 10))
449 FAIL ();
451 wbuf[l0 + 8] = L'\0';
452 wcscat (wbuf, L"A");
453 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
454 FAIL ();
456 wbuf[l0 + 7] = L'\0';
457 wcsncat (wbuf, L"ZYXWV", l0 + 2);
458 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
459 FAIL ();
461 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
462 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
463 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
464 FAIL ();
466 if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
467 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
468 FAIL ();
470 wmemset (wa.buf1 + 8, L'j', l0 + 2);
471 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
472 FAIL ();
474 #if __USE_FORTIFY_LEVEL < 2
475 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
476 and sufficient GCC support, as the string operations overflow
477 from a.buf1 into a.buf2. */
478 wcscpy (wa.buf1 + 4, wstr1 + 5);
479 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
480 FAIL ();
482 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
483 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
484 FAIL ();
486 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
487 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
488 FAIL ();
490 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
491 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
492 FAIL ();
494 wa.buf1[l0 + 8] = L'\0';
495 wcscat (wa.buf1, L"A");
496 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
497 FAIL ();
499 wa.buf1[l0 + 7] = L'\0';
500 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
501 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
502 FAIL ();
504 #endif
506 #if __USE_FORTIFY_LEVEL >= 1
507 /* Now check if all buffer overflows are caught at runtime. */
509 CHK_FAIL_START
510 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
511 CHK_FAIL_END
513 CHK_FAIL_START
514 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
515 CHK_FAIL_END
517 CHK_FAIL_START
518 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
519 CHK_FAIL_END
521 CHK_FAIL_START
522 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
523 CHK_FAIL_END
525 CHK_FAIL_START
526 wmemset (wbuf + 9, L'j', l0 + 2);
527 CHK_FAIL_END
529 CHK_FAIL_START
530 wcscpy (wbuf + 5, wstr1 + 5);
531 CHK_FAIL_END
533 CHK_FAIL_START
534 wp = wcpcpy (wbuf + 9, wstr2);
535 CHK_FAIL_END
537 CHK_FAIL_START
538 wcsncpy (wbuf + 7, L"X", l0 + 4);
539 CHK_FAIL_END
541 CHK_FAIL_START
542 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
543 CHK_FAIL_END
545 CHK_FAIL_START
546 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
547 CHK_FAIL_END
549 CHK_FAIL_START
550 wcpncpy (wbuf + 6, L"cd", l0 + 5);
551 CHK_FAIL_END
553 wmemcpy (wbuf, wstr1 + 2, l0 + 9);
554 CHK_FAIL_START
555 wcscat (wbuf, L"AB");
556 CHK_FAIL_END
558 wmemcpy (wbuf, wstr1 + 3, l0 + 8);
559 CHK_FAIL_START
560 wcsncat (wbuf, L"ZYXWV", l0 + 3);
561 CHK_FAIL_END
563 CHK_FAIL_START
564 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
565 CHK_FAIL_END
567 CHK_FAIL_START
568 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
569 CHK_FAIL_END
571 CHK_FAIL_START
572 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
573 CHK_FAIL_END
575 CHK_FAIL_START
576 wmemset (wa.buf1 + 9, L'j', l0 + 2);
577 CHK_FAIL_END
579 #if __USE_FORTIFY_LEVEL >= 2
580 # define O 0
581 #else
582 # define O 1
583 #endif
585 CHK_FAIL_START
586 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
587 CHK_FAIL_END
589 CHK_FAIL_START
590 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
591 CHK_FAIL_END
593 CHK_FAIL_START
594 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
595 CHK_FAIL_END
597 wmemcpy (wa.buf1, wstr1 + (3 - O), l0 + 8 + O);
598 CHK_FAIL_START
599 wcscat (wa.buf1, L"AB");
600 CHK_FAIL_END
602 wmemcpy (wa.buf1, wstr1 + (4 - O), l0 + 7 + O);
603 CHK_FAIL_START
604 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
605 CHK_FAIL_END
606 #endif
609 /* Now checks for %n protection. */
611 /* Constant literals passed directly are always ok
612 (even with warnings about possible bugs from GCC). */
613 int n1, n2;
614 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
615 || n1 != 1 || n2 != 2)
616 FAIL ();
618 /* In this case the format string is not known at compile time,
619 but resides in read-only memory, so is ok. */
620 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
621 || n1 != 1 || n2 != 2)
622 FAIL ();
624 strcpy (buf2 + 2, "%n%s%n");
625 /* When the format string is writable and contains %n,
626 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
627 CHK_FAIL2_START
628 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
629 FAIL ();
630 CHK_FAIL2_END
632 CHK_FAIL2_START
633 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
634 FAIL ();
635 CHK_FAIL2_END
637 /* But if there is no %n, even writable format string
638 should work. */
639 buf2[6] = '\0';
640 if (sprintf (buf, buf2 + 4, str2) != 1)
641 FAIL ();
643 /* Constant literals passed directly are always ok
644 (even with warnings about possible bugs from GCC). */
645 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
646 || n1 != 7 || n2 != 14)
647 FAIL ();
649 /* In this case the format string is not known at compile time,
650 but resides in read-only memory, so is ok. */
651 if (printf (str3, str4, &n1, str5, &n2) != 14
652 || n1 != 7 || n2 != 14)
653 FAIL ();
655 strcpy (buf2 + 2, "%n%s%n");
656 /* When the format string is writable and contains %n,
657 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
658 CHK_FAIL2_START
659 if (printf (buf2, str4, &n1, str5, &n1) != 14)
660 FAIL ();
661 CHK_FAIL2_END
663 /* But if there is no %n, even writable format string
664 should work. */
665 buf2[6] = '\0';
666 if (printf (buf2 + 4, str5) != 7)
667 FAIL ();
669 FILE *fp = stdout;
671 /* Constant literals passed directly are always ok
672 (even with warnings about possible bugs from GCC). */
673 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
674 || n1 != 7 || n2 != 14)
675 FAIL ();
677 /* In this case the format string is not known at compile time,
678 but resides in read-only memory, so is ok. */
679 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
680 || n1 != 7 || n2 != 14)
681 FAIL ();
683 strcpy (buf2 + 2, "%n%s%n");
684 /* When the format string is writable and contains %n,
685 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
686 CHK_FAIL2_START
687 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
688 FAIL ();
689 CHK_FAIL2_END
691 /* But if there is no %n, even writable format string
692 should work. */
693 buf2[6] = '\0';
694 if (fprintf (fp, buf2 + 4, str5) != 7)
695 FAIL ();
697 char *my_ptr = NULL;
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 (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
703 FAIL ();
704 else
705 free (my_ptr);
706 CHK_FAIL2_END
708 struct obstack obs;
709 obstack_init (&obs);
710 CHK_FAIL2_START
711 if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
712 FAIL ();
713 CHK_FAIL2_END
714 obstack_free (&obs, NULL);
716 my_ptr = NULL;
717 if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
718 FAIL ();
719 else
720 free (my_ptr);
722 obstack_init (&obs);
723 if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
724 FAIL ();
725 obstack_free (&obs, NULL);
727 if (freopen (temp_filename, "r", stdin) == NULL)
729 puts ("could not open temporary file");
730 exit (1);
733 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
734 FAIL ();
735 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
736 FAIL ();
738 #if __USE_FORTIFY_LEVEL >= 1
739 CHK_FAIL_START
740 if (gets (buf) != buf)
741 FAIL ();
742 CHK_FAIL_END
743 #endif
745 rewind (stdin);
747 if (fgets (buf, sizeof (buf), stdin) != buf
748 || memcmp (buf, "abcdefgh\n", 10))
749 FAIL ();
750 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
751 FAIL ();
753 rewind (stdin);
755 if (fgets (buf, l0 + sizeof (buf), stdin) != buf
756 || memcmp (buf, "abcdefgh\n", 10))
757 FAIL ();
759 #if __USE_FORTIFY_LEVEL >= 1
760 CHK_FAIL_START
761 if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
762 FAIL ();
763 CHK_FAIL_END
765 CHK_FAIL_START
766 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
767 FAIL ();
768 CHK_FAIL_END
769 #endif
771 rewind (stdin);
773 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
774 || memcmp (buf, "abcdefgh\n", 10))
775 FAIL ();
776 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
777 || memcmp (buf, "ABCDEFGHI", 10))
778 FAIL ();
780 rewind (stdin);
782 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
783 || memcmp (buf, "abcdefgh\n", 10))
784 FAIL ();
786 #if __USE_FORTIFY_LEVEL >= 1
787 CHK_FAIL_START
788 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
789 FAIL ();
790 CHK_FAIL_END
792 CHK_FAIL_START
793 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
794 FAIL ();
795 CHK_FAIL_END
796 #endif
798 rewind (stdin);
800 if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
801 || memcmp (buf, "abcdefgh\nA", 10))
802 FAIL ();
803 if (fread (buf, sizeof (buf), 1, stdin) != 1
804 || memcmp (buf, "BCDEFGHI\na", 10))
805 FAIL ();
807 rewind (stdin);
809 if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
810 || memcmp (buf, "abcdefgh\nA", 10))
811 FAIL ();
812 if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
813 || memcmp (buf, "BCDEFGHI\na", 10))
814 FAIL ();
816 #if __USE_FORTIFY_LEVEL >= 1
817 CHK_FAIL_START
818 if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
819 FAIL ();
820 CHK_FAIL_END
822 CHK_FAIL_START
823 if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
824 FAIL ();
825 CHK_FAIL_END
826 #endif
828 rewind (stdin);
830 if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
831 || memcmp (buf, "abcdefgh\nA", 10))
832 FAIL ();
833 if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
834 || memcmp (buf, "BCDEFGHI\na", 10))
835 FAIL ();
837 rewind (stdin);
839 if (fread_unlocked (buf, 1, 4, stdin) != 4
840 || memcmp (buf, "abcdFGHI\na", 10))
841 FAIL ();
842 if (fread_unlocked (buf, 4, 1, stdin) != 1
843 || memcmp (buf, "efghFGHI\na", 10))
844 FAIL ();
846 rewind (stdin);
848 if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
849 || memcmp (buf, "abcdefgh\nA", 10))
850 FAIL ();
851 if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
852 || memcmp (buf, "BCDEFGHI\na", 10))
853 FAIL ();
855 #if __USE_FORTIFY_LEVEL >= 1
856 CHK_FAIL_START
857 if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
858 FAIL ();
859 CHK_FAIL_END
861 CHK_FAIL_START
862 if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
863 FAIL ();
864 CHK_FAIL_END
865 #endif
867 lseek (fileno (stdin), 0, SEEK_SET);
869 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
870 || memcmp (buf, "abcdefgh\n", 9))
871 FAIL ();
872 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
873 || memcmp (buf, "ABCDEFGHI", 9))
874 FAIL ();
876 lseek (fileno (stdin), 0, SEEK_SET);
878 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
879 || memcmp (buf, "abcdefgh\n", 9))
880 FAIL ();
882 #if __USE_FORTIFY_LEVEL >= 1
883 CHK_FAIL_START
884 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
885 FAIL ();
886 CHK_FAIL_END
887 #endif
889 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
890 != sizeof (buf) - 1
891 || memcmp (buf, "\nABCDEFGH", 9))
892 FAIL ();
893 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
894 || memcmp (buf, "abcdefgh\n", 9))
895 FAIL ();
896 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
897 != sizeof (buf) - 1
898 || memcmp (buf, "h\nABCDEFG", 9))
899 FAIL ();
901 #if __USE_FORTIFY_LEVEL >= 1
902 CHK_FAIL_START
903 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
904 != sizeof (buf) + 1)
905 FAIL ();
906 CHK_FAIL_END
907 #endif
909 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
910 != sizeof (buf) - 1
911 || memcmp (buf, "\nABCDEFGH", 9))
912 FAIL ();
913 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
914 || memcmp (buf, "abcdefgh\n", 9))
915 FAIL ();
916 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
917 != sizeof (buf) - 1
918 || memcmp (buf, "h\nABCDEFG", 9))
919 FAIL ();
921 #if __USE_FORTIFY_LEVEL >= 1
922 CHK_FAIL_START
923 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
924 != sizeof (buf) + 1)
925 FAIL ();
926 CHK_FAIL_END
927 #endif
929 if (freopen (temp_filename, "r", stdin) == NULL)
931 puts ("could not open temporary file");
932 exit (1);
935 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
937 puts ("could not seek in test file");
938 exit (1);
941 #if __USE_FORTIFY_LEVEL >= 1
942 CHK_FAIL_START
943 if (gets (buf) != buf)
944 FAIL ();
945 CHK_FAIL_END
946 #endif
948 /* Check whether missing N$ formats are detected. */
949 CHK_FAIL2_START
950 printf ("%3$d\n", 1, 2, 3, 4);
951 CHK_FAIL2_END
953 CHK_FAIL2_START
954 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
955 CHK_FAIL2_END
957 CHK_FAIL2_START
958 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
959 CHK_FAIL2_END
961 CHK_FAIL2_START
962 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
963 CHK_FAIL2_END
965 int sp[2];
966 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
967 FAIL ();
968 else
970 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
971 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
972 != strlen (sendstr))
973 FAIL ();
975 char recvbuf[12];
976 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
977 != sizeof recvbuf
978 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
979 FAIL ();
981 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
982 != sizeof recvbuf - 7
983 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
984 FAIL ();
986 #if __USE_FORTIFY_LEVEL >= 1
987 CHK_FAIL_START
988 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
989 != sizeof recvbuf)
990 FAIL ();
991 CHK_FAIL_END
993 CHK_FAIL_START
994 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
995 != sizeof recvbuf - 3)
996 FAIL ();
997 CHK_FAIL_END
998 #endif
1000 socklen_t sl;
1001 struct sockaddr_un sa_un;
1003 sl = sizeof (sa_un);
1004 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
1005 (struct sockaddr *) &sa_un, &sl)
1006 != sizeof recvbuf
1007 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1008 FAIL ();
1010 sl = sizeof (sa_un);
1011 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
1012 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
1013 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1014 FAIL ();
1016 #if __USE_FORTIFY_LEVEL >= 1
1017 CHK_FAIL_START
1018 sl = sizeof (sa_un);
1019 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1020 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1021 FAIL ();
1022 CHK_FAIL_END
1024 CHK_FAIL_START
1025 sl = sizeof (sa_un);
1026 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1027 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1028 FAIL ();
1029 CHK_FAIL_END
1030 #endif
1032 close (sp[0]);
1033 close (sp[1]);
1036 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1037 char *enddir = strchr (fname, '\0');
1038 if (mkdtemp (fname) == NULL)
1040 printf ("mkdtemp failed: %m\n");
1041 return 1;
1043 *enddir = '/';
1044 if (symlink ("bar", fname) != 0)
1045 FAIL ();
1047 char readlinkbuf[4];
1048 if (readlink (fname, readlinkbuf, 4) != 3
1049 || memcmp (readlinkbuf, "bar", 3) != 0)
1050 FAIL ();
1051 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1052 || memcmp (readlinkbuf, "bbar", 4) != 0)
1053 FAIL ();
1055 #if __USE_FORTIFY_LEVEL >= 1
1056 CHK_FAIL_START
1057 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1058 FAIL ();
1059 CHK_FAIL_END
1061 CHK_FAIL_START
1062 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1063 FAIL ();
1064 CHK_FAIL_END
1065 #endif
1067 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1068 if (tmpfd < 0)
1069 FAIL ();
1071 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1072 || memcmp (readlinkbuf, "bar", 3) != 0)
1073 FAIL ();
1074 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1075 l0 + 3) != 3
1076 || memcmp (readlinkbuf, "bbar", 4) != 0)
1077 FAIL ();
1079 #if __USE_FORTIFY_LEVEL >= 1
1080 CHK_FAIL_START
1081 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1082 l0 + 3) != 3)
1083 FAIL ();
1084 CHK_FAIL_END
1086 CHK_FAIL_START
1087 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1088 4) != 3)
1089 FAIL ();
1090 CHK_FAIL_END
1091 #endif
1093 close (tmpfd);
1095 char *cwd1 = getcwd (NULL, 0);
1096 if (cwd1 == NULL)
1097 FAIL ();
1099 char *cwd2 = getcwd (NULL, 250);
1100 if (cwd2 == NULL)
1101 FAIL ();
1103 if (cwd1 && cwd2)
1105 if (strcmp (cwd1, cwd2) != 0)
1106 FAIL ();
1108 *enddir = '\0';
1109 if (chdir (fname))
1110 FAIL ();
1112 char *cwd3 = getcwd (NULL, 0);
1113 if (cwd3 == NULL)
1114 FAIL ();
1115 if (strcmp (fname, cwd3) != 0)
1116 printf ("getcwd after chdir is '%s' != '%s',"
1117 "get{c,}wd tests skipped\n", cwd3, fname);
1118 else
1120 char getcwdbuf[sizeof fname - 3];
1122 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1123 if (cwd4 != getcwdbuf
1124 || strcmp (getcwdbuf, fname) != 0)
1125 FAIL ();
1127 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1128 if (cwd4 != getcwdbuf + 1
1129 || getcwdbuf[0] != fname[0]
1130 || strcmp (getcwdbuf + 1, fname) != 0)
1131 FAIL ();
1133 #if __USE_FORTIFY_LEVEL >= 1
1134 CHK_FAIL_START
1135 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1136 != getcwdbuf + 2)
1137 FAIL ();
1138 CHK_FAIL_END
1140 CHK_FAIL_START
1141 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1142 != getcwdbuf + 2)
1143 FAIL ();
1144 CHK_FAIL_END
1145 #endif
1147 if (getwd (getcwdbuf) != getcwdbuf
1148 || strcmp (getcwdbuf, fname) != 0)
1149 FAIL ();
1151 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1152 || strcmp (getcwdbuf + 1, fname) != 0)
1153 FAIL ();
1155 #if __USE_FORTIFY_LEVEL >= 1
1156 CHK_FAIL_START
1157 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1158 FAIL ();
1159 CHK_FAIL_END
1160 #endif
1163 if (chdir (cwd1) != 0)
1164 FAIL ();
1165 free (cwd3);
1168 free (cwd1);
1169 free (cwd2);
1170 *enddir = '/';
1171 if (unlink (fname) != 0)
1172 FAIL ();
1174 *enddir = '\0';
1175 if (rmdir (fname) != 0)
1176 FAIL ();
1179 #if PATH_MAX > 0
1180 char largebuf[PATH_MAX];
1181 char *realres = realpath (".", largebuf);
1182 if (realres != largebuf)
1183 FAIL ();
1185 # if __USE_FORTIFY_LEVEL >= 1
1186 CHK_FAIL_START
1187 char realbuf[1];
1188 realres = realpath (".", realbuf);
1189 if (realres != realbuf)
1190 FAIL ();
1191 CHK_FAIL_END
1192 # endif
1193 #endif
1195 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1197 assert (MB_CUR_MAX <= 10);
1199 /* First a simple test. */
1200 char enough[10];
1201 if (wctomb (enough, L'A') != 1)
1202 FAIL ();
1204 #if __USE_FORTIFY_LEVEL >= 1
1205 /* We know the wchar_t encoding is ISO 10646. So pick a
1206 character which has a multibyte representation which does not
1207 fit. */
1208 CHK_FAIL_START
1209 char smallbuf[2];
1210 if (wctomb (smallbuf, L'\x100') != 2)
1211 FAIL ();
1212 CHK_FAIL_END
1213 #endif
1215 mbstate_t s;
1216 memset (&s, '\0', sizeof (s));
1217 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
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 (wcrtomb (smallbuf, L'\x100', &s) != 2)
1227 FAIL ();
1228 CHK_FAIL_END
1229 #endif
1231 wchar_t wenough[10];
1232 memset (&s, '\0', sizeof (s));
1233 const char *cp = "A";
1234 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1235 || wcscmp (wenough, L"A") != 0)
1236 FAIL ();
1238 cp = "BC";
1239 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1240 || wcscmp (wenough, L"BC") != 0)
1241 FAIL ();
1243 #if __USE_FORTIFY_LEVEL >= 1
1244 CHK_FAIL_START
1245 wchar_t wsmallbuf[2];
1246 cp = "ABC";
1247 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1248 CHK_FAIL_END
1249 #endif
1251 cp = "A";
1252 if (mbstowcs (wenough, cp, 10) != 1
1253 || wcscmp (wenough, L"A") != 0)
1254 FAIL ();
1256 cp = "DEF";
1257 if (mbstowcs (wenough, cp, l0 + 10) != 3
1258 || wcscmp (wenough, L"DEF") != 0)
1259 FAIL ();
1261 #if __USE_FORTIFY_LEVEL >= 1
1262 CHK_FAIL_START
1263 wchar_t wsmallbuf[2];
1264 cp = "ABC";
1265 mbstowcs (wsmallbuf, cp, 10);
1266 CHK_FAIL_END
1267 #endif
1269 memset (&s, '\0', sizeof (s));
1270 cp = "ABC";
1271 wcscpy (wenough, L"DEF");
1272 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1273 || wcscmp (wenough, L"AEF") != 0)
1274 FAIL ();
1276 cp = "IJ";
1277 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1278 || wcscmp (wenough, L"IEF") != 0)
1279 FAIL ();
1281 #if __USE_FORTIFY_LEVEL >= 1
1282 CHK_FAIL_START
1283 wchar_t wsmallbuf[2];
1284 cp = "ABC";
1285 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1286 CHK_FAIL_END
1287 #endif
1289 memset (&s, '\0', sizeof (s));
1290 const wchar_t *wcp = L"A";
1291 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1292 || strcmp (enough, "A") != 0)
1293 FAIL ();
1295 wcp = L"BC";
1296 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1297 || strcmp (enough, "BC") != 0)
1298 FAIL ();
1300 #if __USE_FORTIFY_LEVEL >= 1
1301 CHK_FAIL_START
1302 char smallbuf[2];
1303 wcp = L"ABC";
1304 wcsrtombs (smallbuf, &wcp, 10, &s);
1305 CHK_FAIL_END
1306 #endif
1308 memset (enough, 'Z', sizeof (enough));
1309 wcp = L"EF";
1310 if (wcstombs (enough, wcp, 10) != 2
1311 || strcmp (enough, "EF") != 0)
1312 FAIL ();
1314 wcp = L"G";
1315 if (wcstombs (enough, wcp, l0 + 10) != 1
1316 || strcmp (enough, "G") != 0)
1317 FAIL ();
1319 #if __USE_FORTIFY_LEVEL >= 1
1320 CHK_FAIL_START
1321 char smallbuf[2];
1322 wcp = L"ABC";
1323 wcstombs (smallbuf, wcp, 10);
1324 CHK_FAIL_END
1325 #endif
1327 memset (&s, '\0', sizeof (s));
1328 wcp = L"AB";
1329 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1330 || strcmp (enough, "A") != 0)
1331 FAIL ();
1333 wcp = L"BCD";
1334 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1335 || strcmp (enough, "B") != 0)
1336 FAIL ();
1338 #if __USE_FORTIFY_LEVEL >= 1
1339 CHK_FAIL_START
1340 char smallbuf[2];
1341 wcp = L"ABC";
1342 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1343 CHK_FAIL_END
1344 #endif
1346 else
1348 puts ("cannot set locale");
1349 ret = 1;
1352 int fd = posix_openpt (O_RDWR);
1353 if (fd != -1)
1355 char enough[1000];
1356 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1357 FAIL ();
1359 #if __USE_FORTIFY_LEVEL >= 1
1360 CHK_FAIL_START
1361 char smallbuf[2];
1362 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1363 FAIL ();
1364 CHK_FAIL_END
1365 #endif
1366 close (fd);
1369 #if PATH_MAX > 0
1370 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1371 # if __USE_FORTIFY_LEVEL >= 1
1372 CHK_FAIL_START
1373 char smallbuf[1];
1374 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1375 CHK_FAIL_END
1376 # endif
1377 #endif
1379 gid_t grpslarge[5];
1380 int ngr = getgroups (5, grpslarge);
1381 asm volatile ("" : : "r" (ngr));
1382 #if __USE_FORTIFY_LEVEL >= 1
1383 CHK_FAIL_START
1384 char smallbuf[1];
1385 ngr = getgroups (5, (gid_t *) smallbuf);
1386 asm volatile ("" : : "r" (ngr));
1387 CHK_FAIL_END
1388 #endif
1390 fd = open (_PATH_TTY, O_RDONLY);
1391 if (fd != -1)
1393 char enough[1000];
1394 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1395 FAIL ();
1397 #if __USE_FORTIFY_LEVEL >= 1
1398 CHK_FAIL_START
1399 char smallbuf[2];
1400 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1401 FAIL ();
1402 CHK_FAIL_END
1403 #endif
1404 close (fd);
1407 char hostnamelarge[1000];
1408 gethostname (hostnamelarge, sizeof (hostnamelarge));
1409 #if __USE_FORTIFY_LEVEL >= 1
1410 CHK_FAIL_START
1411 char smallbuf[1];
1412 gethostname (smallbuf, sizeof (hostnamelarge));
1413 CHK_FAIL_END
1414 #endif
1416 char loginlarge[1000];
1417 getlogin_r (loginlarge, sizeof (hostnamelarge));
1418 #if __USE_FORTIFY_LEVEL >= 1
1419 CHK_FAIL_START
1420 char smallbuf[1];
1421 getlogin_r (smallbuf, sizeof (loginlarge));
1422 CHK_FAIL_END
1423 #endif
1425 char domainnamelarge[1000];
1426 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1427 asm volatile ("" : : "r" (res));
1428 #if __USE_FORTIFY_LEVEL >= 1
1429 CHK_FAIL_START
1430 char smallbuf[1];
1431 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1432 asm volatile ("" : : "r" (res));
1433 CHK_FAIL_END
1434 #endif
1436 fd_set s;
1437 FD_ZERO (&s);
1438 FD_SET (FD_SETSIZE - 1, &s);
1439 #if __USE_FORTIFY_LEVEL >= 1
1440 CHK_FAIL_START
1441 FD_SET (FD_SETSIZE, &s);
1442 CHK_FAIL_END
1443 #endif
1444 FD_CLR (FD_SETSIZE - 1, &s);
1445 #if __USE_FORTIFY_LEVEL >= 1
1446 CHK_FAIL_START
1447 FD_CLR (FD_SETSIZE, &s);
1448 CHK_FAIL_END
1449 #endif
1450 FD_ISSET (FD_SETSIZE - 1, &s);
1451 #if __USE_FORTIFY_LEVEL >= 1
1452 CHK_FAIL_START
1453 FD_ISSET (FD_SETSIZE, &s);
1454 CHK_FAIL_END
1455 #endif
1457 struct pollfd fds[1];
1458 fds[0].fd = STDOUT_FILENO;
1459 fds[0].events = POLLOUT;
1460 poll (fds, 1, 0);
1461 #if __USE_FORTIFY_LEVEL >= 1
1462 CHK_FAIL_START
1463 poll (fds, 2, 0);
1464 CHK_FAIL_END
1465 #endif
1466 ppoll (fds, 1, NULL, NULL);
1467 #if __USE_FORTIFY_LEVEL >= 1
1468 CHK_FAIL_START
1469 ppoll (fds, 2, NULL, NULL);
1470 CHK_FAIL_END
1471 #endif
1473 return ret;