S390: Optimize wmemset.
[glibc.git] / debug / tst-chk1.c
blobbded5831c0aa349e83b1deb1c1a21a121cabbbd3
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.
268 N.B. All tests involving a length parameter need to be done
269 twice: once with the length a compile-time constant, once without. */
271 CHK_FAIL_START
272 memcpy (buf + 1, "abcdefghij", 10);
273 CHK_FAIL_END
275 CHK_FAIL_START
276 memcpy (buf + 1, "abcdefghij", l0 + 10);
277 CHK_FAIL_END
279 CHK_FAIL_START
280 memmove (buf + 2, buf + 1, 9);
281 CHK_FAIL_END
283 CHK_FAIL_START
284 memmove (buf + 2, buf + 1, l0 + 9);
285 CHK_FAIL_END
287 CHK_FAIL_START
288 p = (char *) mempcpy (buf + 6, "abcde", 5);
289 CHK_FAIL_END
291 CHK_FAIL_START
292 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
293 CHK_FAIL_END
295 CHK_FAIL_START
296 memset (buf + 9, 'j', 2);
297 CHK_FAIL_END
299 CHK_FAIL_START
300 memset (buf + 9, 'j', l0 + 2);
301 CHK_FAIL_END
303 CHK_FAIL_START
304 strcpy (buf + 5, str1 + 5);
305 CHK_FAIL_END
307 CHK_FAIL_START
308 p = stpcpy (buf + 9, str2);
309 CHK_FAIL_END
311 CHK_FAIL_START
312 strncpy (buf + 7, "X", 4);
313 CHK_FAIL_END
315 CHK_FAIL_START
316 strncpy (buf + 7, "X", l0 + 4);
317 CHK_FAIL_END
319 CHK_FAIL_START
320 stpncpy (buf + 6, "cd", 5);
321 CHK_FAIL_END
323 CHK_FAIL_START
324 stpncpy (buf + 6, "cd", l0 + 5);
325 CHK_FAIL_END
327 # if !defined __cplusplus || defined __va_arg_pack
328 CHK_FAIL_START
329 sprintf (buf + 8, "%d", num1);
330 CHK_FAIL_END
332 CHK_FAIL_START
333 snprintf (buf + 8, 3, "%d", num2);
334 CHK_FAIL_END
336 CHK_FAIL_START
337 snprintf (buf + 8, l0 + 3, "%d", num2);
338 CHK_FAIL_END
340 CHK_FAIL_START
341 swprintf (wbuf + 8, 3, L"%d", num1);
342 CHK_FAIL_END
344 CHK_FAIL_START
345 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
346 CHK_FAIL_END
347 # endif
349 memcpy (buf, str1 + 2, 9);
350 CHK_FAIL_START
351 strcat (buf, "AB");
352 CHK_FAIL_END
354 memcpy (buf, str1 + 3, 8);
355 CHK_FAIL_START
356 strncat (buf, "ZYXWV", 3);
357 CHK_FAIL_END
359 memcpy (buf, str1 + 3, 8);
360 CHK_FAIL_START
361 strncat (buf, "ZYXWV", l0 + 3);
362 CHK_FAIL_END
364 CHK_FAIL_START
365 memcpy (a.buf1 + 1, "abcdefghij", 10);
366 CHK_FAIL_END
368 CHK_FAIL_START
369 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
370 CHK_FAIL_END
372 CHK_FAIL_START
373 memmove (a.buf1 + 2, a.buf1 + 1, 9);
374 CHK_FAIL_END
376 CHK_FAIL_START
377 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
378 CHK_FAIL_END
380 CHK_FAIL_START
381 p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
382 CHK_FAIL_END
384 CHK_FAIL_START
385 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
386 CHK_FAIL_END
388 CHK_FAIL_START
389 memset (a.buf1 + 9, 'j', 2);
390 CHK_FAIL_END
392 CHK_FAIL_START
393 memset (a.buf1 + 9, 'j', l0 + 2);
394 CHK_FAIL_END
396 # if __USE_FORTIFY_LEVEL >= 2
397 # define O 0
398 # else
399 # define O 1
400 # endif
402 CHK_FAIL_START
403 strcpy (a.buf1 + (O + 4), str1 + 5);
404 CHK_FAIL_END
406 CHK_FAIL_START
407 p = stpcpy (a.buf1 + (O + 8), str2);
408 CHK_FAIL_END
410 CHK_FAIL_START
411 strncpy (a.buf1 + (O + 6), "X", 4);
412 CHK_FAIL_END
414 CHK_FAIL_START
415 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
416 CHK_FAIL_END
418 # if !defined __cplusplus || defined __va_arg_pack
419 CHK_FAIL_START
420 sprintf (a.buf1 + (O + 7), "%d", num1);
421 CHK_FAIL_END
423 CHK_FAIL_START
424 snprintf (a.buf1 + (O + 7), 3, "%d", num2);
425 CHK_FAIL_END
427 CHK_FAIL_START
428 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
429 CHK_FAIL_END
430 # endif
432 memcpy (a.buf1, str1 + (3 - O), 8 + O);
433 CHK_FAIL_START
434 strcat (a.buf1, "AB");
435 CHK_FAIL_END
437 memcpy (a.buf1, str1 + (4 - O), 7 + O);
438 CHK_FAIL_START
439 strncat (a.buf1, "ZYXWV", l0 + 3);
440 CHK_FAIL_END
441 #endif
444 /* These ops can be done without runtime checking of object size. */
445 wmemcpy (wbuf, L"abcdefghij", 10);
446 wmemmove (wbuf + 1, wbuf, 9);
447 if (wmemcmp (wbuf, L"aabcdefghi", 10))
448 FAIL ();
450 if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
451 || wmemcmp (wbuf, L"aabcdabcde", 10))
452 FAIL ();
454 wmemset (wbuf + 8, L'j', 2);
455 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
456 FAIL ();
458 wcscpy (wbuf + 4, L"EDCBA");
459 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
460 FAIL ();
462 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
463 FAIL ();
465 wcsncpy (wbuf + 6, L"X", 4);
466 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
467 FAIL ();
469 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
470 || wmemcmp (wbuf, L"aabcEDX98", 10))
471 FAIL ();
473 if (swprintf (wbuf + 7, 3, L"64") != 2
474 || wmemcmp (wbuf, L"aabcEDX64", 10))
475 FAIL ();
477 /* These ops need runtime checking, but shouldn't __chk_fail. */
478 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
479 wmemmove (wbuf + 1, wbuf, l0 + 9);
480 if (wmemcmp (wbuf, L"aabcdefghi", 10))
481 FAIL ();
483 if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
484 || wmemcmp (wbuf, L"aabcdabcde", 10))
485 FAIL ();
487 wmemset (wbuf + 8, L'j', l0 + 2);
488 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
489 FAIL ();
491 wcscpy (wbuf + 4, wstr1 + 5);
492 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
493 FAIL ();
495 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
496 FAIL ();
498 wcsncpy (wbuf + 6, L"X", l0 + 4);
499 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
500 FAIL ();
502 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
503 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
504 FAIL ();
506 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
507 || wmemcmp (wbuf, L"aabcEcd98", 10))
508 FAIL ();
510 wbuf[l0 + 8] = L'\0';
511 wcscat (wbuf, L"A");
512 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
513 FAIL ();
515 wbuf[l0 + 7] = L'\0';
516 wcsncat (wbuf, L"ZYXWV", l0 + 2);
517 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
518 FAIL ();
520 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
521 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
522 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
523 FAIL ();
525 if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
526 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
527 FAIL ();
529 wmemset (wa.buf1 + 8, L'j', l0 + 2);
530 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
531 FAIL ();
533 #if __USE_FORTIFY_LEVEL < 2
534 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
535 and sufficient GCC support, as the string operations overflow
536 from a.buf1 into a.buf2. */
537 wcscpy (wa.buf1 + 4, wstr1 + 5);
538 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
539 FAIL ();
541 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
542 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
543 FAIL ();
545 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
546 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
547 FAIL ();
549 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
550 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
551 FAIL ();
553 wa.buf1[l0 + 8] = L'\0';
554 wcscat (wa.buf1, L"A");
555 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
556 FAIL ();
558 wa.buf1[l0 + 7] = L'\0';
559 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
560 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
561 FAIL ();
563 #endif
565 #if __USE_FORTIFY_LEVEL >= 1
566 /* Now check if all buffer overflows are caught at runtime.
567 N.B. All tests involving a length parameter need to be done
568 twice: once with the length a compile-time constant, once without. */
570 CHK_FAIL_START
571 wmemcpy (wbuf + 1, L"abcdefghij", 10);
572 CHK_FAIL_END
574 CHK_FAIL_START
575 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
576 CHK_FAIL_END
578 CHK_FAIL_START
579 wmemcpy (wbuf + 9, L"abcdefghij", 10);
580 CHK_FAIL_END
582 CHK_FAIL_START
583 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
584 CHK_FAIL_END
586 CHK_FAIL_START
587 wmemmove (wbuf + 2, wbuf + 1, 9);
588 CHK_FAIL_END
590 CHK_FAIL_START
591 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
592 CHK_FAIL_END
594 CHK_FAIL_START
595 wp = wmempcpy (wbuf + 6, L"abcde", 5);
596 CHK_FAIL_END
598 CHK_FAIL_START
599 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
600 CHK_FAIL_END
602 CHK_FAIL_START
603 wmemset (wbuf + 9, L'j', 2);
604 CHK_FAIL_END
606 CHK_FAIL_START
607 wmemset (wbuf + 9, L'j', l0 + 2);
608 CHK_FAIL_END
610 CHK_FAIL_START
611 wcscpy (wbuf + 5, wstr1 + 5);
612 CHK_FAIL_END
614 CHK_FAIL_START
615 wp = wcpcpy (wbuf + 9, wstr2);
616 CHK_FAIL_END
618 CHK_FAIL_START
619 wcsncpy (wbuf + 7, L"X", 4);
620 CHK_FAIL_END
622 CHK_FAIL_START
623 wcsncpy (wbuf + 7, L"X", l0 + 4);
624 CHK_FAIL_END
626 CHK_FAIL_START
627 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
628 CHK_FAIL_END
630 CHK_FAIL_START
631 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
632 CHK_FAIL_END
634 CHK_FAIL_START
635 wcpncpy (wbuf + 6, L"cd", 5);
636 CHK_FAIL_END
638 CHK_FAIL_START
639 wcpncpy (wbuf + 6, L"cd", l0 + 5);
640 CHK_FAIL_END
642 wmemcpy (wbuf, wstr1 + 2, 9);
643 CHK_FAIL_START
644 wcscat (wbuf, L"AB");
645 CHK_FAIL_END
647 wmemcpy (wbuf, wstr1 + 3, 8);
648 CHK_FAIL_START
649 wcsncat (wbuf, L"ZYXWV", l0 + 3);
650 CHK_FAIL_END
652 CHK_FAIL_START
653 wmemcpy (wa.buf1 + 1, L"abcdefghij", 10);
654 CHK_FAIL_END
656 CHK_FAIL_START
657 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
658 CHK_FAIL_END
660 CHK_FAIL_START
661 wmemmove (wa.buf1 + 2, wa.buf1 + 1, 9);
662 CHK_FAIL_END
664 CHK_FAIL_START
665 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
666 CHK_FAIL_END
668 CHK_FAIL_START
669 wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
670 CHK_FAIL_END
672 CHK_FAIL_START
673 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
674 CHK_FAIL_END
676 CHK_FAIL_START
677 wmemset (wa.buf1 + 9, L'j', 2);
678 CHK_FAIL_END
680 CHK_FAIL_START
681 wmemset (wa.buf1 + 9, L'j', l0 + 2);
682 CHK_FAIL_END
684 #if __USE_FORTIFY_LEVEL >= 2
685 # define O 0
686 #else
687 # define O 1
688 #endif
690 CHK_FAIL_START
691 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
692 CHK_FAIL_END
694 CHK_FAIL_START
695 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
696 CHK_FAIL_END
698 CHK_FAIL_START
699 wcsncpy (wa.buf1 + (O + 6), L"X", 4);
700 CHK_FAIL_END
702 CHK_FAIL_START
703 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
704 CHK_FAIL_END
706 wmemcpy (wa.buf1, wstr1 + (3 - O), 8 + O);
707 CHK_FAIL_START
708 wcscat (wa.buf1, L"AB");
709 CHK_FAIL_END
711 wmemcpy (wa.buf1, wstr1 + (4 - O), 7 + O);
712 CHK_FAIL_START
713 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
714 CHK_FAIL_END
715 #endif
718 /* Now checks for %n protection. */
720 /* Constant literals passed directly are always ok
721 (even with warnings about possible bugs from GCC). */
722 int n1, n2;
723 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
724 || n1 != 1 || n2 != 2)
725 FAIL ();
727 /* In this case the format string is not known at compile time,
728 but resides in read-only memory, so is ok. */
729 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
730 || n1 != 1 || n2 != 2)
731 FAIL ();
733 strcpy (buf2 + 2, "%n%s%n");
734 /* When the format string is writable and contains %n,
735 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
736 CHK_FAIL2_START
737 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
738 FAIL ();
739 CHK_FAIL2_END
741 CHK_FAIL2_START
742 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
743 FAIL ();
744 CHK_FAIL2_END
746 /* But if there is no %n, even writable format string
747 should work. */
748 buf2[6] = '\0';
749 if (sprintf (buf, buf2 + 4, str2) != 1)
750 FAIL ();
752 /* Constant literals passed directly are always ok
753 (even with warnings about possible bugs from GCC). */
754 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
755 || n1 != 7 || n2 != 14)
756 FAIL ();
758 /* In this case the format string is not known at compile time,
759 but resides in read-only memory, so is ok. */
760 if (printf (str3, str4, &n1, str5, &n2) != 14
761 || n1 != 7 || n2 != 14)
762 FAIL ();
764 strcpy (buf2 + 2, "%n%s%n");
765 /* When the format string is writable and contains %n,
766 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
767 CHK_FAIL2_START
768 if (printf (buf2, str4, &n1, str5, &n1) != 14)
769 FAIL ();
770 CHK_FAIL2_END
772 /* But if there is no %n, even writable format string
773 should work. */
774 buf2[6] = '\0';
775 if (printf (buf2 + 4, str5) != 7)
776 FAIL ();
778 FILE *fp = stdout;
780 /* Constant literals passed directly are always ok
781 (even with warnings about possible bugs from GCC). */
782 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
783 || n1 != 7 || n2 != 14)
784 FAIL ();
786 /* In this case the format string is not known at compile time,
787 but resides in read-only memory, so is ok. */
788 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
789 || n1 != 7 || n2 != 14)
790 FAIL ();
792 strcpy (buf2 + 2, "%n%s%n");
793 /* When the format string is writable and contains %n,
794 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
795 CHK_FAIL2_START
796 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
797 FAIL ();
798 CHK_FAIL2_END
800 /* But if there is no %n, even writable format string
801 should work. */
802 buf2[6] = '\0';
803 if (fprintf (fp, buf2 + 4, str5) != 7)
804 FAIL ();
806 char *my_ptr = NULL;
807 strcpy (buf2 + 2, "%n%s%n");
808 /* When the format string is writable and contains %n,
809 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
810 CHK_FAIL2_START
811 if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
812 FAIL ();
813 else
814 free (my_ptr);
815 CHK_FAIL2_END
817 struct obstack obs;
818 obstack_init (&obs);
819 CHK_FAIL2_START
820 if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
821 FAIL ();
822 CHK_FAIL2_END
823 obstack_free (&obs, NULL);
825 my_ptr = NULL;
826 if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
827 FAIL ();
828 else
829 free (my_ptr);
831 obstack_init (&obs);
832 if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
833 FAIL ();
834 obstack_free (&obs, NULL);
836 if (freopen (temp_filename, "r", stdin) == NULL)
838 puts ("could not open temporary file");
839 exit (1);
842 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
843 FAIL ();
844 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
845 FAIL ();
847 #if __USE_FORTIFY_LEVEL >= 1
848 CHK_FAIL_START
849 if (gets (buf) != buf)
850 FAIL ();
851 CHK_FAIL_END
852 #endif
854 rewind (stdin);
856 if (fgets (buf, sizeof (buf), stdin) != buf
857 || memcmp (buf, "abcdefgh\n", 10))
858 FAIL ();
859 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
860 FAIL ();
862 rewind (stdin);
864 if (fgets (buf, l0 + sizeof (buf), stdin) != buf
865 || memcmp (buf, "abcdefgh\n", 10))
866 FAIL ();
868 #if __USE_FORTIFY_LEVEL >= 1
869 CHK_FAIL_START
870 if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
871 FAIL ();
872 CHK_FAIL_END
874 CHK_FAIL_START
875 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
876 FAIL ();
877 CHK_FAIL_END
878 #endif
880 rewind (stdin);
882 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
883 || memcmp (buf, "abcdefgh\n", 10))
884 FAIL ();
885 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
886 || memcmp (buf, "ABCDEFGHI", 10))
887 FAIL ();
889 rewind (stdin);
891 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
892 || memcmp (buf, "abcdefgh\n", 10))
893 FAIL ();
895 #if __USE_FORTIFY_LEVEL >= 1
896 CHK_FAIL_START
897 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
898 FAIL ();
899 CHK_FAIL_END
901 CHK_FAIL_START
902 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
903 FAIL ();
904 CHK_FAIL_END
905 #endif
907 rewind (stdin);
909 if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
910 || memcmp (buf, "abcdefgh\nA", 10))
911 FAIL ();
912 if (fread (buf, sizeof (buf), 1, stdin) != 1
913 || memcmp (buf, "BCDEFGHI\na", 10))
914 FAIL ();
916 rewind (stdin);
918 if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
919 || memcmp (buf, "abcdefgh\nA", 10))
920 FAIL ();
921 if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
922 || memcmp (buf, "BCDEFGHI\na", 10))
923 FAIL ();
925 #if __USE_FORTIFY_LEVEL >= 1
926 CHK_FAIL_START
927 if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
928 FAIL ();
929 CHK_FAIL_END
931 CHK_FAIL_START
932 if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
933 FAIL ();
934 CHK_FAIL_END
935 #endif
937 rewind (stdin);
939 if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
940 || memcmp (buf, "abcdefgh\nA", 10))
941 FAIL ();
942 if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
943 || memcmp (buf, "BCDEFGHI\na", 10))
944 FAIL ();
946 rewind (stdin);
948 if (fread_unlocked (buf, 1, 4, stdin) != 4
949 || memcmp (buf, "abcdFGHI\na", 10))
950 FAIL ();
951 if (fread_unlocked (buf, 4, 1, stdin) != 1
952 || memcmp (buf, "efghFGHI\na", 10))
953 FAIL ();
955 rewind (stdin);
957 if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
958 || memcmp (buf, "abcdefgh\nA", 10))
959 FAIL ();
960 if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
961 || memcmp (buf, "BCDEFGHI\na", 10))
962 FAIL ();
964 #if __USE_FORTIFY_LEVEL >= 1
965 CHK_FAIL_START
966 if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
967 FAIL ();
968 CHK_FAIL_END
970 CHK_FAIL_START
971 if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
972 FAIL ();
973 CHK_FAIL_END
974 #endif
976 lseek (fileno (stdin), 0, SEEK_SET);
978 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
979 || memcmp (buf, "abcdefgh\n", 9))
980 FAIL ();
981 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
982 || memcmp (buf, "ABCDEFGHI", 9))
983 FAIL ();
985 lseek (fileno (stdin), 0, SEEK_SET);
987 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
988 || memcmp (buf, "abcdefgh\n", 9))
989 FAIL ();
991 #if __USE_FORTIFY_LEVEL >= 1
992 CHK_FAIL_START
993 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
994 FAIL ();
995 CHK_FAIL_END
997 CHK_FAIL_START
998 if (read (fileno (stdin), buf, l0 + sizeof (buf) + 1) != sizeof (buf) + 1)
999 FAIL ();
1000 CHK_FAIL_END
1001 #endif
1003 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
1004 != sizeof (buf) - 1
1005 || memcmp (buf, "\nABCDEFGH", 9))
1006 FAIL ();
1007 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
1008 || memcmp (buf, "abcdefgh\n", 9))
1009 FAIL ();
1010 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
1011 != sizeof (buf) - 1
1012 || memcmp (buf, "h\nABCDEFG", 9))
1013 FAIL ();
1015 #if __USE_FORTIFY_LEVEL >= 1
1016 CHK_FAIL_START
1017 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
1018 != sizeof (buf) + 1)
1019 FAIL ();
1020 CHK_FAIL_END
1022 CHK_FAIL_START
1023 if (pread (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
1024 != sizeof (buf) + 1)
1025 FAIL ();
1026 CHK_FAIL_END
1027 #endif
1029 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
1030 != sizeof (buf) - 1
1031 || memcmp (buf, "\nABCDEFGH", 9))
1032 FAIL ();
1033 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
1034 || memcmp (buf, "abcdefgh\n", 9))
1035 FAIL ();
1036 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
1037 != sizeof (buf) - 1
1038 || memcmp (buf, "h\nABCDEFG", 9))
1039 FAIL ();
1041 #if __USE_FORTIFY_LEVEL >= 1
1042 CHK_FAIL_START
1043 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
1044 != sizeof (buf) + 1)
1045 FAIL ();
1046 CHK_FAIL_END
1048 CHK_FAIL_START
1049 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
1050 != sizeof (buf) + 1)
1051 FAIL ();
1052 CHK_FAIL_END
1053 #endif
1055 if (freopen (temp_filename, "r", stdin) == NULL)
1057 puts ("could not open temporary file");
1058 exit (1);
1061 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
1063 puts ("could not seek in test file");
1064 exit (1);
1067 #if __USE_FORTIFY_LEVEL >= 1
1068 CHK_FAIL_START
1069 if (gets (buf) != buf)
1070 FAIL ();
1071 CHK_FAIL_END
1072 #endif
1074 /* Check whether missing N$ formats are detected. */
1075 CHK_FAIL2_START
1076 printf ("%3$d\n", 1, 2, 3, 4);
1077 CHK_FAIL2_END
1079 CHK_FAIL2_START
1080 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
1081 CHK_FAIL2_END
1083 CHK_FAIL2_START
1084 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
1085 CHK_FAIL2_END
1087 CHK_FAIL2_START
1088 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
1089 CHK_FAIL2_END
1091 int sp[2];
1092 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
1093 FAIL ();
1094 else
1096 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
1097 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
1098 != strlen (sendstr))
1099 FAIL ();
1101 char recvbuf[12];
1102 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
1103 != sizeof recvbuf
1104 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1105 FAIL ();
1107 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
1108 != sizeof recvbuf - 7
1109 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1110 FAIL ();
1112 #if __USE_FORTIFY_LEVEL >= 1
1113 CHK_FAIL_START
1114 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
1115 != sizeof recvbuf)
1116 FAIL ();
1117 CHK_FAIL_END
1119 CHK_FAIL_START
1120 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
1121 != sizeof recvbuf - 3)
1122 FAIL ();
1123 CHK_FAIL_END
1124 #endif
1126 socklen_t sl;
1127 struct sockaddr_un sa_un;
1129 sl = sizeof (sa_un);
1130 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
1131 (struct sockaddr *) &sa_un, &sl)
1132 != sizeof recvbuf
1133 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1134 FAIL ();
1136 sl = sizeof (sa_un);
1137 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
1138 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
1139 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1140 FAIL ();
1142 #if __USE_FORTIFY_LEVEL >= 1
1143 CHK_FAIL_START
1144 sl = sizeof (sa_un);
1145 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1146 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1147 FAIL ();
1148 CHK_FAIL_END
1150 CHK_FAIL_START
1151 sl = sizeof (sa_un);
1152 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1153 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1154 FAIL ();
1155 CHK_FAIL_END
1156 #endif
1158 close (sp[0]);
1159 close (sp[1]);
1162 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1163 char *enddir = strchr (fname, '\0');
1164 if (mkdtemp (fname) == NULL)
1166 printf ("mkdtemp failed: %m\n");
1167 return 1;
1169 *enddir = '/';
1170 if (symlink ("bar", fname) != 0)
1171 FAIL ();
1173 char readlinkbuf[4];
1174 if (readlink (fname, readlinkbuf, 4) != 3
1175 || memcmp (readlinkbuf, "bar", 3) != 0)
1176 FAIL ();
1177 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1178 || memcmp (readlinkbuf, "bbar", 4) != 0)
1179 FAIL ();
1181 #if __USE_FORTIFY_LEVEL >= 1
1182 CHK_FAIL_START
1183 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1184 FAIL ();
1185 CHK_FAIL_END
1187 CHK_FAIL_START
1188 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1189 FAIL ();
1190 CHK_FAIL_END
1191 #endif
1193 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1194 if (tmpfd < 0)
1195 FAIL ();
1197 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1198 || memcmp (readlinkbuf, "bar", 3) != 0)
1199 FAIL ();
1200 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1201 l0 + 3) != 3
1202 || memcmp (readlinkbuf, "bbar", 4) != 0)
1203 FAIL ();
1205 #if __USE_FORTIFY_LEVEL >= 1
1206 CHK_FAIL_START
1207 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1208 l0 + 3) != 3)
1209 FAIL ();
1210 CHK_FAIL_END
1212 CHK_FAIL_START
1213 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1214 4) != 3)
1215 FAIL ();
1216 CHK_FAIL_END
1217 #endif
1219 close (tmpfd);
1221 char *cwd1 = getcwd (NULL, 0);
1222 if (cwd1 == NULL)
1223 FAIL ();
1225 char *cwd2 = getcwd (NULL, 250);
1226 if (cwd2 == NULL)
1227 FAIL ();
1229 if (cwd1 && cwd2)
1231 if (strcmp (cwd1, cwd2) != 0)
1232 FAIL ();
1234 *enddir = '\0';
1235 if (chdir (fname))
1236 FAIL ();
1238 char *cwd3 = getcwd (NULL, 0);
1239 if (cwd3 == NULL)
1240 FAIL ();
1241 if (strcmp (fname, cwd3) != 0)
1242 printf ("getcwd after chdir is '%s' != '%s',"
1243 "get{c,}wd tests skipped\n", cwd3, fname);
1244 else
1246 char getcwdbuf[sizeof fname - 3];
1248 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1249 if (cwd4 != getcwdbuf
1250 || strcmp (getcwdbuf, fname) != 0)
1251 FAIL ();
1253 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1254 if (cwd4 != getcwdbuf + 1
1255 || getcwdbuf[0] != fname[0]
1256 || strcmp (getcwdbuf + 1, fname) != 0)
1257 FAIL ();
1259 #if __USE_FORTIFY_LEVEL >= 1
1260 CHK_FAIL_START
1261 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1262 != getcwdbuf + 2)
1263 FAIL ();
1264 CHK_FAIL_END
1266 CHK_FAIL_START
1267 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1268 != getcwdbuf + 2)
1269 FAIL ();
1270 CHK_FAIL_END
1271 #endif
1273 if (getwd (getcwdbuf) != getcwdbuf
1274 || strcmp (getcwdbuf, fname) != 0)
1275 FAIL ();
1277 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1278 || strcmp (getcwdbuf + 1, fname) != 0)
1279 FAIL ();
1281 #if __USE_FORTIFY_LEVEL >= 1
1282 CHK_FAIL_START
1283 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1284 FAIL ();
1285 CHK_FAIL_END
1286 #endif
1289 if (chdir (cwd1) != 0)
1290 FAIL ();
1291 free (cwd3);
1294 free (cwd1);
1295 free (cwd2);
1296 *enddir = '/';
1297 if (unlink (fname) != 0)
1298 FAIL ();
1300 *enddir = '\0';
1301 if (rmdir (fname) != 0)
1302 FAIL ();
1305 #if PATH_MAX > 0
1306 char largebuf[PATH_MAX];
1307 char *realres = realpath (".", largebuf);
1308 if (realres != largebuf)
1309 FAIL ();
1311 # if __USE_FORTIFY_LEVEL >= 1
1312 CHK_FAIL_START
1313 char realbuf[1];
1314 realres = realpath (".", realbuf);
1315 if (realres != realbuf)
1316 FAIL ();
1317 CHK_FAIL_END
1318 # endif
1319 #endif
1321 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1323 assert (MB_CUR_MAX <= 10);
1325 /* First a simple test. */
1326 char enough[10];
1327 if (wctomb (enough, L'A') != 1)
1328 FAIL ();
1330 #if __USE_FORTIFY_LEVEL >= 1
1331 /* We know the wchar_t encoding is ISO 10646. So pick a
1332 character which has a multibyte representation which does not
1333 fit. */
1334 CHK_FAIL_START
1335 char smallbuf[2];
1336 if (wctomb (smallbuf, L'\x100') != 2)
1337 FAIL ();
1338 CHK_FAIL_END
1339 #endif
1341 mbstate_t s;
1342 memset (&s, '\0', sizeof (s));
1343 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1344 FAIL ();
1346 #if __USE_FORTIFY_LEVEL >= 1
1347 /* We know the wchar_t encoding is ISO 10646. So pick a
1348 character which has a multibyte representation which does not
1349 fit. */
1350 CHK_FAIL_START
1351 char smallbuf[2];
1352 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1353 FAIL ();
1354 CHK_FAIL_END
1355 #endif
1357 wchar_t wenough[10];
1358 memset (&s, '\0', sizeof (s));
1359 const char *cp = "A";
1360 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1361 || wcscmp (wenough, L"A") != 0)
1362 FAIL ();
1364 cp = "BC";
1365 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1366 || wcscmp (wenough, L"BC") != 0)
1367 FAIL ();
1369 #if __USE_FORTIFY_LEVEL >= 1
1370 CHK_FAIL_START
1371 wchar_t wsmallbuf[2];
1372 cp = "ABC";
1373 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1374 CHK_FAIL_END
1375 #endif
1377 cp = "A";
1378 if (mbstowcs (wenough, cp, 10) != 1
1379 || wcscmp (wenough, L"A") != 0)
1380 FAIL ();
1382 cp = "DEF";
1383 if (mbstowcs (wenough, cp, l0 + 10) != 3
1384 || wcscmp (wenough, L"DEF") != 0)
1385 FAIL ();
1387 #if __USE_FORTIFY_LEVEL >= 1
1388 CHK_FAIL_START
1389 wchar_t wsmallbuf[2];
1390 cp = "ABC";
1391 mbstowcs (wsmallbuf, cp, 10);
1392 CHK_FAIL_END
1393 #endif
1395 memset (&s, '\0', sizeof (s));
1396 cp = "ABC";
1397 wcscpy (wenough, L"DEF");
1398 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1399 || wcscmp (wenough, L"AEF") != 0)
1400 FAIL ();
1402 cp = "IJ";
1403 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1404 || wcscmp (wenough, L"IEF") != 0)
1405 FAIL ();
1407 #if __USE_FORTIFY_LEVEL >= 1
1408 CHK_FAIL_START
1409 wchar_t wsmallbuf[2];
1410 cp = "ABC";
1411 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1412 CHK_FAIL_END
1413 #endif
1415 memset (&s, '\0', sizeof (s));
1416 const wchar_t *wcp = L"A";
1417 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1418 || strcmp (enough, "A") != 0)
1419 FAIL ();
1421 wcp = L"BC";
1422 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1423 || strcmp (enough, "BC") != 0)
1424 FAIL ();
1426 #if __USE_FORTIFY_LEVEL >= 1
1427 CHK_FAIL_START
1428 char smallbuf[2];
1429 wcp = L"ABC";
1430 wcsrtombs (smallbuf, &wcp, 10, &s);
1431 CHK_FAIL_END
1432 #endif
1434 memset (enough, 'Z', sizeof (enough));
1435 wcp = L"EF";
1436 if (wcstombs (enough, wcp, 10) != 2
1437 || strcmp (enough, "EF") != 0)
1438 FAIL ();
1440 wcp = L"G";
1441 if (wcstombs (enough, wcp, l0 + 10) != 1
1442 || strcmp (enough, "G") != 0)
1443 FAIL ();
1445 #if __USE_FORTIFY_LEVEL >= 1
1446 CHK_FAIL_START
1447 char smallbuf[2];
1448 wcp = L"ABC";
1449 wcstombs (smallbuf, wcp, 10);
1450 CHK_FAIL_END
1451 #endif
1453 memset (&s, '\0', sizeof (s));
1454 wcp = L"AB";
1455 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1456 || strcmp (enough, "A") != 0)
1457 FAIL ();
1459 wcp = L"BCD";
1460 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1461 || strcmp (enough, "B") != 0)
1462 FAIL ();
1464 #if __USE_FORTIFY_LEVEL >= 1
1465 CHK_FAIL_START
1466 char smallbuf[2];
1467 wcp = L"ABC";
1468 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1469 CHK_FAIL_END
1470 #endif
1472 else
1474 puts ("cannot set locale");
1475 ret = 1;
1478 int fd = posix_openpt (O_RDWR);
1479 if (fd != -1)
1481 char enough[1000];
1482 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1483 FAIL ();
1485 #if __USE_FORTIFY_LEVEL >= 1
1486 CHK_FAIL_START
1487 char smallbuf[2];
1488 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1489 FAIL ();
1490 CHK_FAIL_END
1491 #endif
1492 close (fd);
1495 #if PATH_MAX > 0
1496 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1497 # if __USE_FORTIFY_LEVEL >= 1
1498 CHK_FAIL_START
1499 char smallbuf[1];
1500 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1501 CHK_FAIL_END
1502 # endif
1503 #endif
1505 gid_t grpslarge[5];
1506 int ngr = getgroups (5, grpslarge);
1507 asm volatile ("" : : "r" (ngr));
1508 #if __USE_FORTIFY_LEVEL >= 1
1509 CHK_FAIL_START
1510 char smallbuf[1];
1511 ngr = getgroups (5, (gid_t *) smallbuf);
1512 asm volatile ("" : : "r" (ngr));
1513 CHK_FAIL_END
1514 #endif
1516 fd = open (_PATH_TTY, O_RDONLY);
1517 if (fd != -1)
1519 char enough[1000];
1520 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1521 FAIL ();
1523 #if __USE_FORTIFY_LEVEL >= 1
1524 CHK_FAIL_START
1525 char smallbuf[2];
1526 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1527 FAIL ();
1528 CHK_FAIL_END
1529 #endif
1530 close (fd);
1533 char hostnamelarge[1000];
1534 gethostname (hostnamelarge, sizeof (hostnamelarge));
1535 #if __USE_FORTIFY_LEVEL >= 1
1536 CHK_FAIL_START
1537 char smallbuf[1];
1538 gethostname (smallbuf, sizeof (hostnamelarge));
1539 CHK_FAIL_END
1540 #endif
1542 char loginlarge[1000];
1543 getlogin_r (loginlarge, sizeof (hostnamelarge));
1544 #if __USE_FORTIFY_LEVEL >= 1
1545 CHK_FAIL_START
1546 char smallbuf[1];
1547 getlogin_r (smallbuf, sizeof (loginlarge));
1548 CHK_FAIL_END
1549 #endif
1551 char domainnamelarge[1000];
1552 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1553 asm volatile ("" : : "r" (res));
1554 #if __USE_FORTIFY_LEVEL >= 1
1555 CHK_FAIL_START
1556 char smallbuf[1];
1557 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1558 asm volatile ("" : : "r" (res));
1559 CHK_FAIL_END
1560 #endif
1562 fd_set s;
1563 FD_ZERO (&s);
1565 FD_SET (FD_SETSIZE - 1, &s);
1566 #if __USE_FORTIFY_LEVEL >= 1
1567 CHK_FAIL_START
1568 FD_SET (FD_SETSIZE, &s);
1569 CHK_FAIL_END
1571 CHK_FAIL_START
1572 FD_SET (l0 + FD_SETSIZE, &s);
1573 CHK_FAIL_END
1574 #endif
1576 FD_CLR (FD_SETSIZE - 1, &s);
1577 #if __USE_FORTIFY_LEVEL >= 1
1578 CHK_FAIL_START
1579 FD_CLR (FD_SETSIZE, &s);
1580 CHK_FAIL_END
1582 CHK_FAIL_START
1583 FD_SET (l0 + FD_SETSIZE, &s);
1584 CHK_FAIL_END
1585 #endif
1587 FD_ISSET (FD_SETSIZE - 1, &s);
1588 #if __USE_FORTIFY_LEVEL >= 1
1589 CHK_FAIL_START
1590 FD_ISSET (FD_SETSIZE, &s);
1591 CHK_FAIL_END
1593 CHK_FAIL_START
1594 FD_ISSET (l0 + FD_SETSIZE, &s);
1595 CHK_FAIL_END
1596 #endif
1598 struct pollfd fds[1];
1599 fds[0].fd = STDOUT_FILENO;
1600 fds[0].events = POLLOUT;
1601 poll (fds, 1, 0);
1602 #if __USE_FORTIFY_LEVEL >= 1
1603 CHK_FAIL_START
1604 poll (fds, 2, 0);
1605 CHK_FAIL_END
1607 CHK_FAIL_START
1608 poll (fds, l0 + 2, 0);
1609 CHK_FAIL_END
1610 #endif
1611 ppoll (fds, 1, NULL, NULL);
1612 #if __USE_FORTIFY_LEVEL >= 1
1613 CHK_FAIL_START
1614 ppoll (fds, 2, NULL, NULL);
1615 CHK_FAIL_END
1617 CHK_FAIL_START
1618 ppoll (fds, l0 + 2, NULL, NULL);
1619 CHK_FAIL_END
1620 #endif
1622 return ret;