powerpc: Installed-header hygiene
[glibc.git] / debug / tst-chk1.c
blob478c2fbb819eaaa48ef528658eac12652a29f033
1 /* Copyright (C) 2004-2016 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 memcpy (buf, "abcdefghij", 10);
147 bcopy (buf, buf + 1, 9);
148 if (memcmp (buf, "aabcdefghi", 10))
149 FAIL ();
151 if (mempcpy (buf + 5, "abcde", 5) != buf + 10
152 || memcmp (buf, "aabcdabcde", 10))
153 FAIL ();
155 memset (buf + 8, 'j', 2);
156 if (memcmp (buf, "aabcdabcjj", 10))
157 FAIL ();
159 bzero (buf + 8, 2);
160 if (memcmp (buf, "aabcdabc\0\0", 10))
161 FAIL ();
163 strcpy (buf + 4, "EDCBA");
164 if (memcmp (buf, "aabcEDCBA", 10))
165 FAIL ();
167 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
168 FAIL ();
170 strncpy (buf + 6, "X", 4);
171 if (memcmp (buf, "aabcEDX\0\0", 10))
172 FAIL ();
174 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
175 FAIL ();
177 if (snprintf (buf + 7, 3, "%s", "987654") != 6
178 || memcmp (buf, "aabcEDX98", 10))
179 FAIL ();
181 /* These ops need runtime checking, but shouldn't __chk_fail. */
182 memcpy (buf, "abcdefghij", l0 + 10);
183 memmove (buf + 1, buf, l0 + 9);
184 if (memcmp (buf, "aabcdefghi", 10))
185 FAIL ();
187 memcpy (buf, "abcdefghij", l0 + 10);
188 bcopy (buf, buf + 1, l0 + 9);
189 if (memcmp (buf, "aabcdefghi", 10))
190 FAIL ();
192 if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
193 || memcmp (buf, "aabcdabcde", 10))
194 FAIL ();
196 memset (buf + 8, 'j', l0 + 2);
197 if (memcmp (buf, "aabcdabcjj", 10))
198 FAIL ();
200 bzero (buf + 8, l0 + 2);
201 if (memcmp (buf, "aabcdabc\0\0", 10))
202 FAIL ();
204 strcpy (buf + 4, str1 + 5);
205 if (memcmp (buf, "aabcEDCBA", 10))
206 FAIL ();
208 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
209 FAIL ();
211 strncpy (buf + 6, "X", l0 + 4);
212 if (memcmp (buf, "aabcEDX\0\0", 10))
213 FAIL ();
215 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
216 || memcmp (buf, "aabcEcd\0\0", 10))
217 FAIL ();
219 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
220 FAIL ();
222 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
223 FAIL ();
225 buf[l0 + 8] = '\0';
226 strcat (buf, "A");
227 if (memcmp (buf, "aabcEcd9A", 10))
228 FAIL ();
230 buf[l0 + 7] = '\0';
231 strncat (buf, "ZYXWV", l0 + 2);
232 if (memcmp (buf, "aabcEcdZY", 10))
233 FAIL ();
235 /* The following tests are supposed to succeed at all fortify
236 levels, even though they overflow a.buf1 into a.buf2. */
237 memcpy (a.buf1, "abcdefghij", l0 + 10);
238 memmove (a.buf1 + 1, a.buf1, l0 + 9);
239 if (memcmp (a.buf1, "aabcdefghi", 10))
240 FAIL ();
242 memcpy (a.buf1, "abcdefghij", l0 + 10);
243 bcopy (a.buf1, a.buf1 + 1, l0 + 9);
244 if (memcmp (a.buf1, "aabcdefghi", 10))
245 FAIL ();
247 if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
248 || memcmp (a.buf1, "aabcdabcde", 10))
249 FAIL ();
251 memset (a.buf1 + 8, 'j', l0 + 2);
252 if (memcmp (a.buf1, "aabcdabcjj", 10))
253 FAIL ();
255 bzero (a.buf1 + 8, l0 + 2);
256 if (memcmp (a.buf1, "aabcdabc\0\0", 10))
257 FAIL ();
259 #if __USE_FORTIFY_LEVEL < 2
260 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
261 and sufficient GCC support, as the string operations overflow
262 from a.buf1 into a.buf2. */
263 strcpy (a.buf1 + 4, str1 + 5);
264 if (memcmp (a.buf1, "aabcEDCBA", 10))
265 FAIL ();
267 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
268 || memcmp (a.buf1, "aabcEDCBF", 10))
269 FAIL ();
271 strncpy (a.buf1 + 6, "X", l0 + 4);
272 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
273 FAIL ();
275 if (sprintf (a.buf1 + 7, "%d", num1) != 2
276 || memcmp (a.buf1, "aabcEDX67", 10))
277 FAIL ();
279 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
280 || memcmp (a.buf1, "aabcEDX98", 10))
281 FAIL ();
283 a.buf1[l0 + 8] = '\0';
284 strcat (a.buf1, "A");
285 if (memcmp (a.buf1, "aabcEDX9A", 10))
286 FAIL ();
288 a.buf1[l0 + 7] = '\0';
289 strncat (a.buf1, "ZYXWV", l0 + 2);
290 if (memcmp (a.buf1, "aabcEDXZY", 10))
291 FAIL ();
293 #endif
295 #if __USE_FORTIFY_LEVEL >= 1
296 /* Now check if all buffer overflows are caught at runtime.
297 N.B. All tests involving a length parameter need to be done
298 twice: once with the length a compile-time constant, once without. */
300 CHK_FAIL_START
301 memcpy (buf + 1, "abcdefghij", 10);
302 CHK_FAIL_END
304 CHK_FAIL_START
305 memcpy (buf + 1, "abcdefghij", l0 + 10);
306 CHK_FAIL_END
308 CHK_FAIL_START
309 memmove (buf + 2, buf + 1, 9);
310 CHK_FAIL_END
312 CHK_FAIL_START
313 memmove (buf + 2, buf + 1, l0 + 9);
314 CHK_FAIL_END
316 CHK_FAIL_START
317 bcopy (buf + 1, buf + 2, 9);
318 CHK_FAIL_END
320 CHK_FAIL_START
321 bcopy (buf + 1, buf + 2, l0 + 9);
322 CHK_FAIL_END
324 CHK_FAIL_START
325 p = (char *) mempcpy (buf + 6, "abcde", 5);
326 CHK_FAIL_END
328 CHK_FAIL_START
329 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
330 CHK_FAIL_END
332 CHK_FAIL_START
333 memset (buf + 9, 'j', 2);
334 CHK_FAIL_END
336 CHK_FAIL_START
337 memset (buf + 9, 'j', l0 + 2);
338 CHK_FAIL_END
340 CHK_FAIL_START
341 bzero (buf + 9, 2);
342 CHK_FAIL_END
344 CHK_FAIL_START
345 bzero (buf + 9, l0 + 2);
346 CHK_FAIL_END
348 CHK_FAIL_START
349 strcpy (buf + 5, str1 + 5);
350 CHK_FAIL_END
352 CHK_FAIL_START
353 p = stpcpy (buf + 9, str2);
354 CHK_FAIL_END
356 CHK_FAIL_START
357 strncpy (buf + 7, "X", 4);
358 CHK_FAIL_END
360 CHK_FAIL_START
361 strncpy (buf + 7, "X", l0 + 4);
362 CHK_FAIL_END
364 CHK_FAIL_START
365 stpncpy (buf + 6, "cd", 5);
366 CHK_FAIL_END
368 CHK_FAIL_START
369 stpncpy (buf + 6, "cd", l0 + 5);
370 CHK_FAIL_END
372 # if !defined __cplusplus || defined __va_arg_pack
373 CHK_FAIL_START
374 sprintf (buf + 8, "%d", num1);
375 CHK_FAIL_END
377 CHK_FAIL_START
378 snprintf (buf + 8, 3, "%d", num2);
379 CHK_FAIL_END
381 CHK_FAIL_START
382 snprintf (buf + 8, l0 + 3, "%d", num2);
383 CHK_FAIL_END
385 CHK_FAIL_START
386 swprintf (wbuf + 8, 3, L"%d", num1);
387 CHK_FAIL_END
389 CHK_FAIL_START
390 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
391 CHK_FAIL_END
392 # endif
394 memcpy (buf, str1 + 2, 9);
395 CHK_FAIL_START
396 strcat (buf, "AB");
397 CHK_FAIL_END
399 memcpy (buf, str1 + 3, 8);
400 CHK_FAIL_START
401 strncat (buf, "ZYXWV", 3);
402 CHK_FAIL_END
404 memcpy (buf, str1 + 3, 8);
405 CHK_FAIL_START
406 strncat (buf, "ZYXWV", l0 + 3);
407 CHK_FAIL_END
409 CHK_FAIL_START
410 memcpy (a.buf1 + 1, "abcdefghij", 10);
411 CHK_FAIL_END
413 CHK_FAIL_START
414 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
415 CHK_FAIL_END
417 CHK_FAIL_START
418 memmove (a.buf1 + 2, a.buf1 + 1, 9);
419 CHK_FAIL_END
421 CHK_FAIL_START
422 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
423 CHK_FAIL_END
425 CHK_FAIL_START
426 bcopy (a.buf1 + 1, a.buf1 + 2, 9);
427 CHK_FAIL_END
429 CHK_FAIL_START
430 bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
431 CHK_FAIL_END
433 CHK_FAIL_START
434 p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
435 CHK_FAIL_END
437 CHK_FAIL_START
438 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
439 CHK_FAIL_END
441 CHK_FAIL_START
442 memset (a.buf1 + 9, 'j', 2);
443 CHK_FAIL_END
445 CHK_FAIL_START
446 memset (a.buf1 + 9, 'j', l0 + 2);
447 CHK_FAIL_END
449 CHK_FAIL_START
450 bzero (a.buf1 + 9, 2);
451 CHK_FAIL_END
453 CHK_FAIL_START
454 bzero (a.buf1 + 9, l0 + 2);
455 CHK_FAIL_END
457 # if __USE_FORTIFY_LEVEL >= 2
458 # define O 0
459 # else
460 # define O 1
461 # endif
463 CHK_FAIL_START
464 strcpy (a.buf1 + (O + 4), str1 + 5);
465 CHK_FAIL_END
467 CHK_FAIL_START
468 p = stpcpy (a.buf1 + (O + 8), str2);
469 CHK_FAIL_END
471 CHK_FAIL_START
472 strncpy (a.buf1 + (O + 6), "X", 4);
473 CHK_FAIL_END
475 CHK_FAIL_START
476 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
477 CHK_FAIL_END
479 # if !defined __cplusplus || defined __va_arg_pack
480 CHK_FAIL_START
481 sprintf (a.buf1 + (O + 7), "%d", num1);
482 CHK_FAIL_END
484 CHK_FAIL_START
485 snprintf (a.buf1 + (O + 7), 3, "%d", num2);
486 CHK_FAIL_END
488 CHK_FAIL_START
489 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
490 CHK_FAIL_END
491 # endif
493 memcpy (a.buf1, str1 + (3 - O), 8 + O);
494 CHK_FAIL_START
495 strcat (a.buf1, "AB");
496 CHK_FAIL_END
498 memcpy (a.buf1, str1 + (4 - O), 7 + O);
499 CHK_FAIL_START
500 strncat (a.buf1, "ZYXWV", l0 + 3);
501 CHK_FAIL_END
502 #endif
505 /* These ops can be done without runtime checking of object size. */
506 wmemcpy (wbuf, L"abcdefghij", 10);
507 wmemmove (wbuf + 1, wbuf, 9);
508 if (wmemcmp (wbuf, L"aabcdefghi", 10))
509 FAIL ();
511 if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
512 || wmemcmp (wbuf, L"aabcdabcde", 10))
513 FAIL ();
515 wmemset (wbuf + 8, L'j', 2);
516 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
517 FAIL ();
519 wcscpy (wbuf + 4, L"EDCBA");
520 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
521 FAIL ();
523 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
524 FAIL ();
526 wcsncpy (wbuf + 6, L"X", 4);
527 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
528 FAIL ();
530 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
531 || wmemcmp (wbuf, L"aabcEDX98", 10))
532 FAIL ();
534 if (swprintf (wbuf + 7, 3, L"64") != 2
535 || wmemcmp (wbuf, L"aabcEDX64", 10))
536 FAIL ();
538 /* These ops need runtime checking, but shouldn't __chk_fail. */
539 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
540 wmemmove (wbuf + 1, wbuf, l0 + 9);
541 if (wmemcmp (wbuf, L"aabcdefghi", 10))
542 FAIL ();
544 if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
545 || wmemcmp (wbuf, L"aabcdabcde", 10))
546 FAIL ();
548 wmemset (wbuf + 8, L'j', l0 + 2);
549 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
550 FAIL ();
552 wcscpy (wbuf + 4, wstr1 + 5);
553 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
554 FAIL ();
556 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
557 FAIL ();
559 wcsncpy (wbuf + 6, L"X", l0 + 4);
560 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
561 FAIL ();
563 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
564 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
565 FAIL ();
567 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
568 || wmemcmp (wbuf, L"aabcEcd98", 10))
569 FAIL ();
571 wbuf[l0 + 8] = L'\0';
572 wcscat (wbuf, L"A");
573 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
574 FAIL ();
576 wbuf[l0 + 7] = L'\0';
577 wcsncat (wbuf, L"ZYXWV", l0 + 2);
578 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
579 FAIL ();
581 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
582 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
583 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
584 FAIL ();
586 if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
587 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
588 FAIL ();
590 wmemset (wa.buf1 + 8, L'j', l0 + 2);
591 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
592 FAIL ();
594 #if __USE_FORTIFY_LEVEL < 2
595 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
596 and sufficient GCC support, as the string operations overflow
597 from a.buf1 into a.buf2. */
598 wcscpy (wa.buf1 + 4, wstr1 + 5);
599 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
600 FAIL ();
602 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
603 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
604 FAIL ();
606 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
607 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
608 FAIL ();
610 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
611 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
612 FAIL ();
614 wa.buf1[l0 + 8] = L'\0';
615 wcscat (wa.buf1, L"A");
616 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
617 FAIL ();
619 wa.buf1[l0 + 7] = L'\0';
620 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
621 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
622 FAIL ();
624 #endif
626 #if __USE_FORTIFY_LEVEL >= 1
627 /* Now check if all buffer overflows are caught at runtime.
628 N.B. All tests involving a length parameter need to be done
629 twice: once with the length a compile-time constant, once without. */
631 CHK_FAIL_START
632 wmemcpy (wbuf + 1, L"abcdefghij", 10);
633 CHK_FAIL_END
635 CHK_FAIL_START
636 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
637 CHK_FAIL_END
639 CHK_FAIL_START
640 wmemcpy (wbuf + 9, L"abcdefghij", 10);
641 CHK_FAIL_END
643 CHK_FAIL_START
644 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
645 CHK_FAIL_END
647 CHK_FAIL_START
648 wmemmove (wbuf + 2, wbuf + 1, 9);
649 CHK_FAIL_END
651 CHK_FAIL_START
652 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
653 CHK_FAIL_END
655 CHK_FAIL_START
656 wp = wmempcpy (wbuf + 6, L"abcde", 5);
657 CHK_FAIL_END
659 CHK_FAIL_START
660 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
661 CHK_FAIL_END
663 CHK_FAIL_START
664 wmemset (wbuf + 9, L'j', 2);
665 CHK_FAIL_END
667 CHK_FAIL_START
668 wmemset (wbuf + 9, L'j', l0 + 2);
669 CHK_FAIL_END
671 CHK_FAIL_START
672 wcscpy (wbuf + 5, wstr1 + 5);
673 CHK_FAIL_END
675 CHK_FAIL_START
676 wp = wcpcpy (wbuf + 9, wstr2);
677 CHK_FAIL_END
679 CHK_FAIL_START
680 wcsncpy (wbuf + 7, L"X", 4);
681 CHK_FAIL_END
683 CHK_FAIL_START
684 wcsncpy (wbuf + 7, L"X", l0 + 4);
685 CHK_FAIL_END
687 CHK_FAIL_START
688 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
689 CHK_FAIL_END
691 CHK_FAIL_START
692 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
693 CHK_FAIL_END
695 CHK_FAIL_START
696 wcpncpy (wbuf + 6, L"cd", 5);
697 CHK_FAIL_END
699 CHK_FAIL_START
700 wcpncpy (wbuf + 6, L"cd", l0 + 5);
701 CHK_FAIL_END
703 wmemcpy (wbuf, wstr1 + 2, 9);
704 CHK_FAIL_START
705 wcscat (wbuf, L"AB");
706 CHK_FAIL_END
708 wmemcpy (wbuf, wstr1 + 3, 8);
709 CHK_FAIL_START
710 wcsncat (wbuf, L"ZYXWV", l0 + 3);
711 CHK_FAIL_END
713 CHK_FAIL_START
714 wmemcpy (wa.buf1 + 1, L"abcdefghij", 10);
715 CHK_FAIL_END
717 CHK_FAIL_START
718 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
719 CHK_FAIL_END
721 CHK_FAIL_START
722 wmemmove (wa.buf1 + 2, wa.buf1 + 1, 9);
723 CHK_FAIL_END
725 CHK_FAIL_START
726 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
727 CHK_FAIL_END
729 CHK_FAIL_START
730 wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
731 CHK_FAIL_END
733 CHK_FAIL_START
734 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
735 CHK_FAIL_END
737 CHK_FAIL_START
738 wmemset (wa.buf1 + 9, L'j', 2);
739 CHK_FAIL_END
741 CHK_FAIL_START
742 wmemset (wa.buf1 + 9, L'j', l0 + 2);
743 CHK_FAIL_END
745 #if __USE_FORTIFY_LEVEL >= 2
746 # define O 0
747 #else
748 # define O 1
749 #endif
751 CHK_FAIL_START
752 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
753 CHK_FAIL_END
755 CHK_FAIL_START
756 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
757 CHK_FAIL_END
759 CHK_FAIL_START
760 wcsncpy (wa.buf1 + (O + 6), L"X", 4);
761 CHK_FAIL_END
763 CHK_FAIL_START
764 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
765 CHK_FAIL_END
767 wmemcpy (wa.buf1, wstr1 + (3 - O), 8 + O);
768 CHK_FAIL_START
769 wcscat (wa.buf1, L"AB");
770 CHK_FAIL_END
772 wmemcpy (wa.buf1, wstr1 + (4 - O), 7 + O);
773 CHK_FAIL_START
774 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
775 CHK_FAIL_END
776 #endif
779 /* Now checks for %n protection. */
781 /* Constant literals passed directly are always ok
782 (even with warnings about possible bugs from GCC). */
783 int n1, n2;
784 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
785 || n1 != 1 || n2 != 2)
786 FAIL ();
788 /* In this case the format string is not known at compile time,
789 but resides in read-only memory, so is ok. */
790 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
791 || n1 != 1 || n2 != 2)
792 FAIL ();
794 strcpy (buf2 + 2, "%n%s%n");
795 /* When the format string is writable and contains %n,
796 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
797 CHK_FAIL2_START
798 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
799 FAIL ();
800 CHK_FAIL2_END
802 CHK_FAIL2_START
803 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
804 FAIL ();
805 CHK_FAIL2_END
807 /* But if there is no %n, even writable format string
808 should work. */
809 buf2[6] = '\0';
810 if (sprintf (buf, buf2 + 4, str2) != 1)
811 FAIL ();
813 /* Constant literals passed directly are always ok
814 (even with warnings about possible bugs from GCC). */
815 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
816 || n1 != 7 || n2 != 14)
817 FAIL ();
819 /* In this case the format string is not known at compile time,
820 but resides in read-only memory, so is ok. */
821 if (printf (str3, str4, &n1, str5, &n2) != 14
822 || n1 != 7 || n2 != 14)
823 FAIL ();
825 strcpy (buf2 + 2, "%n%s%n");
826 /* When the format string is writable and contains %n,
827 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
828 CHK_FAIL2_START
829 if (printf (buf2, str4, &n1, str5, &n1) != 14)
830 FAIL ();
831 CHK_FAIL2_END
833 /* But if there is no %n, even writable format string
834 should work. */
835 buf2[6] = '\0';
836 if (printf (buf2 + 4, str5) != 7)
837 FAIL ();
839 FILE *fp = stdout;
841 /* Constant literals passed directly are always ok
842 (even with warnings about possible bugs from GCC). */
843 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
844 || n1 != 7 || n2 != 14)
845 FAIL ();
847 /* In this case the format string is not known at compile time,
848 but resides in read-only memory, so is ok. */
849 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
850 || n1 != 7 || n2 != 14)
851 FAIL ();
853 strcpy (buf2 + 2, "%n%s%n");
854 /* When the format string is writable and contains %n,
855 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
856 CHK_FAIL2_START
857 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
858 FAIL ();
859 CHK_FAIL2_END
861 /* But if there is no %n, even writable format string
862 should work. */
863 buf2[6] = '\0';
864 if (fprintf (fp, buf2 + 4, str5) != 7)
865 FAIL ();
867 char *my_ptr = NULL;
868 strcpy (buf2 + 2, "%n%s%n");
869 /* When the format string is writable and contains %n,
870 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
871 CHK_FAIL2_START
872 if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
873 FAIL ();
874 else
875 free (my_ptr);
876 CHK_FAIL2_END
878 struct obstack obs;
879 obstack_init (&obs);
880 CHK_FAIL2_START
881 if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
882 FAIL ();
883 CHK_FAIL2_END
884 obstack_free (&obs, NULL);
886 my_ptr = NULL;
887 if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
888 FAIL ();
889 else
890 free (my_ptr);
892 obstack_init (&obs);
893 if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
894 FAIL ();
895 obstack_free (&obs, NULL);
897 if (freopen (temp_filename, "r", stdin) == NULL)
899 puts ("could not open temporary file");
900 exit (1);
903 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
904 FAIL ();
905 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
906 FAIL ();
908 #if __USE_FORTIFY_LEVEL >= 1
909 CHK_FAIL_START
910 if (gets (buf) != buf)
911 FAIL ();
912 CHK_FAIL_END
913 #endif
915 rewind (stdin);
917 if (fgets (buf, sizeof (buf), stdin) != buf
918 || memcmp (buf, "abcdefgh\n", 10))
919 FAIL ();
920 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
921 FAIL ();
923 rewind (stdin);
925 if (fgets (buf, l0 + sizeof (buf), stdin) != buf
926 || memcmp (buf, "abcdefgh\n", 10))
927 FAIL ();
929 #if __USE_FORTIFY_LEVEL >= 1
930 CHK_FAIL_START
931 if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
932 FAIL ();
933 CHK_FAIL_END
935 CHK_FAIL_START
936 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
937 FAIL ();
938 CHK_FAIL_END
939 #endif
941 rewind (stdin);
943 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
944 || memcmp (buf, "abcdefgh\n", 10))
945 FAIL ();
946 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
947 || memcmp (buf, "ABCDEFGHI", 10))
948 FAIL ();
950 rewind (stdin);
952 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
953 || memcmp (buf, "abcdefgh\n", 10))
954 FAIL ();
956 #if __USE_FORTIFY_LEVEL >= 1
957 CHK_FAIL_START
958 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
959 FAIL ();
960 CHK_FAIL_END
962 CHK_FAIL_START
963 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
964 FAIL ();
965 CHK_FAIL_END
966 #endif
968 rewind (stdin);
970 if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
971 || memcmp (buf, "abcdefgh\nA", 10))
972 FAIL ();
973 if (fread (buf, sizeof (buf), 1, stdin) != 1
974 || memcmp (buf, "BCDEFGHI\na", 10))
975 FAIL ();
977 rewind (stdin);
979 if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
980 || memcmp (buf, "abcdefgh\nA", 10))
981 FAIL ();
982 if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
983 || memcmp (buf, "BCDEFGHI\na", 10))
984 FAIL ();
986 #if __USE_FORTIFY_LEVEL >= 1
987 CHK_FAIL_START
988 if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
989 FAIL ();
990 CHK_FAIL_END
992 CHK_FAIL_START
993 if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
994 FAIL ();
995 CHK_FAIL_END
996 #endif
998 rewind (stdin);
1000 if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
1001 || memcmp (buf, "abcdefgh\nA", 10))
1002 FAIL ();
1003 if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
1004 || memcmp (buf, "BCDEFGHI\na", 10))
1005 FAIL ();
1007 rewind (stdin);
1009 if (fread_unlocked (buf, 1, 4, stdin) != 4
1010 || memcmp (buf, "abcdFGHI\na", 10))
1011 FAIL ();
1012 if (fread_unlocked (buf, 4, 1, stdin) != 1
1013 || memcmp (buf, "efghFGHI\na", 10))
1014 FAIL ();
1016 rewind (stdin);
1018 if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
1019 || memcmp (buf, "abcdefgh\nA", 10))
1020 FAIL ();
1021 if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
1022 || memcmp (buf, "BCDEFGHI\na", 10))
1023 FAIL ();
1025 #if __USE_FORTIFY_LEVEL >= 1
1026 CHK_FAIL_START
1027 if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
1028 FAIL ();
1029 CHK_FAIL_END
1031 CHK_FAIL_START
1032 if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
1033 FAIL ();
1034 CHK_FAIL_END
1035 #endif
1037 lseek (fileno (stdin), 0, SEEK_SET);
1039 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
1040 || memcmp (buf, "abcdefgh\n", 9))
1041 FAIL ();
1042 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
1043 || memcmp (buf, "ABCDEFGHI", 9))
1044 FAIL ();
1046 lseek (fileno (stdin), 0, SEEK_SET);
1048 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
1049 || memcmp (buf, "abcdefgh\n", 9))
1050 FAIL ();
1052 #if __USE_FORTIFY_LEVEL >= 1
1053 CHK_FAIL_START
1054 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
1055 FAIL ();
1056 CHK_FAIL_END
1058 CHK_FAIL_START
1059 if (read (fileno (stdin), buf, l0 + sizeof (buf) + 1) != sizeof (buf) + 1)
1060 FAIL ();
1061 CHK_FAIL_END
1062 #endif
1064 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
1065 != sizeof (buf) - 1
1066 || memcmp (buf, "\nABCDEFGH", 9))
1067 FAIL ();
1068 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
1069 || memcmp (buf, "abcdefgh\n", 9))
1070 FAIL ();
1071 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
1072 != sizeof (buf) - 1
1073 || memcmp (buf, "h\nABCDEFG", 9))
1074 FAIL ();
1076 #if __USE_FORTIFY_LEVEL >= 1
1077 CHK_FAIL_START
1078 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
1079 != sizeof (buf) + 1)
1080 FAIL ();
1081 CHK_FAIL_END
1083 CHK_FAIL_START
1084 if (pread (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
1085 != sizeof (buf) + 1)
1086 FAIL ();
1087 CHK_FAIL_END
1088 #endif
1090 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
1091 != sizeof (buf) - 1
1092 || memcmp (buf, "\nABCDEFGH", 9))
1093 FAIL ();
1094 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
1095 || memcmp (buf, "abcdefgh\n", 9))
1096 FAIL ();
1097 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
1098 != sizeof (buf) - 1
1099 || memcmp (buf, "h\nABCDEFG", 9))
1100 FAIL ();
1102 #if __USE_FORTIFY_LEVEL >= 1
1103 CHK_FAIL_START
1104 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
1105 != sizeof (buf) + 1)
1106 FAIL ();
1107 CHK_FAIL_END
1109 CHK_FAIL_START
1110 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
1111 != sizeof (buf) + 1)
1112 FAIL ();
1113 CHK_FAIL_END
1114 #endif
1116 if (freopen (temp_filename, "r", stdin) == NULL)
1118 puts ("could not open temporary file");
1119 exit (1);
1122 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
1124 puts ("could not seek in test file");
1125 exit (1);
1128 #if __USE_FORTIFY_LEVEL >= 1
1129 CHK_FAIL_START
1130 if (gets (buf) != buf)
1131 FAIL ();
1132 CHK_FAIL_END
1133 #endif
1135 /* Check whether missing N$ formats are detected. */
1136 CHK_FAIL2_START
1137 printf ("%3$d\n", 1, 2, 3, 4);
1138 CHK_FAIL2_END
1140 CHK_FAIL2_START
1141 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
1142 CHK_FAIL2_END
1144 CHK_FAIL2_START
1145 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
1146 CHK_FAIL2_END
1148 CHK_FAIL2_START
1149 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
1150 CHK_FAIL2_END
1152 int sp[2];
1153 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
1154 FAIL ();
1155 else
1157 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
1158 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
1159 != strlen (sendstr))
1160 FAIL ();
1162 char recvbuf[12];
1163 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
1164 != sizeof recvbuf
1165 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1166 FAIL ();
1168 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
1169 != sizeof recvbuf - 7
1170 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1171 FAIL ();
1173 #if __USE_FORTIFY_LEVEL >= 1
1174 CHK_FAIL_START
1175 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
1176 != sizeof recvbuf)
1177 FAIL ();
1178 CHK_FAIL_END
1180 CHK_FAIL_START
1181 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
1182 != sizeof recvbuf - 3)
1183 FAIL ();
1184 CHK_FAIL_END
1185 #endif
1187 socklen_t sl;
1188 struct sockaddr_un sa_un;
1190 sl = sizeof (sa_un);
1191 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
1192 (struct sockaddr *) &sa_un, &sl)
1193 != sizeof recvbuf
1194 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1195 FAIL ();
1197 sl = sizeof (sa_un);
1198 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
1199 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
1200 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1201 FAIL ();
1203 #if __USE_FORTIFY_LEVEL >= 1
1204 CHK_FAIL_START
1205 sl = sizeof (sa_un);
1206 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1207 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1208 FAIL ();
1209 CHK_FAIL_END
1211 CHK_FAIL_START
1212 sl = sizeof (sa_un);
1213 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1214 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1215 FAIL ();
1216 CHK_FAIL_END
1217 #endif
1219 close (sp[0]);
1220 close (sp[1]);
1223 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1224 char *enddir = strchr (fname, '\0');
1225 if (mkdtemp (fname) == NULL)
1227 printf ("mkdtemp failed: %m\n");
1228 return 1;
1230 *enddir = '/';
1231 if (symlink ("bar", fname) != 0)
1232 FAIL ();
1234 char readlinkbuf[4];
1235 if (readlink (fname, readlinkbuf, 4) != 3
1236 || memcmp (readlinkbuf, "bar", 3) != 0)
1237 FAIL ();
1238 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1239 || memcmp (readlinkbuf, "bbar", 4) != 0)
1240 FAIL ();
1242 #if __USE_FORTIFY_LEVEL >= 1
1243 CHK_FAIL_START
1244 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1245 FAIL ();
1246 CHK_FAIL_END
1248 CHK_FAIL_START
1249 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1250 FAIL ();
1251 CHK_FAIL_END
1252 #endif
1254 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1255 if (tmpfd < 0)
1256 FAIL ();
1258 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1259 || memcmp (readlinkbuf, "bar", 3) != 0)
1260 FAIL ();
1261 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1262 l0 + 3) != 3
1263 || memcmp (readlinkbuf, "bbar", 4) != 0)
1264 FAIL ();
1266 #if __USE_FORTIFY_LEVEL >= 1
1267 CHK_FAIL_START
1268 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1269 l0 + 3) != 3)
1270 FAIL ();
1271 CHK_FAIL_END
1273 CHK_FAIL_START
1274 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1275 4) != 3)
1276 FAIL ();
1277 CHK_FAIL_END
1278 #endif
1280 close (tmpfd);
1282 char *cwd1 = getcwd (NULL, 0);
1283 if (cwd1 == NULL)
1284 FAIL ();
1286 char *cwd2 = getcwd (NULL, 250);
1287 if (cwd2 == NULL)
1288 FAIL ();
1290 if (cwd1 && cwd2)
1292 if (strcmp (cwd1, cwd2) != 0)
1293 FAIL ();
1295 *enddir = '\0';
1296 if (chdir (fname))
1297 FAIL ();
1299 char *cwd3 = getcwd (NULL, 0);
1300 if (cwd3 == NULL)
1301 FAIL ();
1302 if (strcmp (fname, cwd3) != 0)
1303 printf ("getcwd after chdir is '%s' != '%s',"
1304 "get{c,}wd tests skipped\n", cwd3, fname);
1305 else
1307 char getcwdbuf[sizeof fname - 3];
1309 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1310 if (cwd4 != getcwdbuf
1311 || strcmp (getcwdbuf, fname) != 0)
1312 FAIL ();
1314 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1315 if (cwd4 != getcwdbuf + 1
1316 || getcwdbuf[0] != fname[0]
1317 || strcmp (getcwdbuf + 1, fname) != 0)
1318 FAIL ();
1320 #if __USE_FORTIFY_LEVEL >= 1
1321 CHK_FAIL_START
1322 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1323 != getcwdbuf + 2)
1324 FAIL ();
1325 CHK_FAIL_END
1327 CHK_FAIL_START
1328 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1329 != getcwdbuf + 2)
1330 FAIL ();
1331 CHK_FAIL_END
1332 #endif
1334 if (getwd (getcwdbuf) != getcwdbuf
1335 || strcmp (getcwdbuf, fname) != 0)
1336 FAIL ();
1338 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1339 || strcmp (getcwdbuf + 1, fname) != 0)
1340 FAIL ();
1342 #if __USE_FORTIFY_LEVEL >= 1
1343 CHK_FAIL_START
1344 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1345 FAIL ();
1346 CHK_FAIL_END
1347 #endif
1350 if (chdir (cwd1) != 0)
1351 FAIL ();
1352 free (cwd3);
1355 free (cwd1);
1356 free (cwd2);
1357 *enddir = '/';
1358 if (unlink (fname) != 0)
1359 FAIL ();
1361 *enddir = '\0';
1362 if (rmdir (fname) != 0)
1363 FAIL ();
1366 #if PATH_MAX > 0
1367 char largebuf[PATH_MAX];
1368 char *realres = realpath (".", largebuf);
1369 if (realres != largebuf)
1370 FAIL ();
1372 # if __USE_FORTIFY_LEVEL >= 1
1373 CHK_FAIL_START
1374 char realbuf[1];
1375 realres = realpath (".", realbuf);
1376 if (realres != realbuf)
1377 FAIL ();
1378 CHK_FAIL_END
1379 # endif
1380 #endif
1382 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1384 assert (MB_CUR_MAX <= 10);
1386 /* First a simple test. */
1387 char enough[10];
1388 if (wctomb (enough, L'A') != 1)
1389 FAIL ();
1391 #if __USE_FORTIFY_LEVEL >= 1
1392 /* We know the wchar_t encoding is ISO 10646. So pick a
1393 character which has a multibyte representation which does not
1394 fit. */
1395 CHK_FAIL_START
1396 char smallbuf[2];
1397 if (wctomb (smallbuf, L'\x100') != 2)
1398 FAIL ();
1399 CHK_FAIL_END
1400 #endif
1402 mbstate_t s;
1403 memset (&s, '\0', sizeof (s));
1404 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1405 FAIL ();
1407 #if __USE_FORTIFY_LEVEL >= 1
1408 /* We know the wchar_t encoding is ISO 10646. So pick a
1409 character which has a multibyte representation which does not
1410 fit. */
1411 CHK_FAIL_START
1412 char smallbuf[2];
1413 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1414 FAIL ();
1415 CHK_FAIL_END
1416 #endif
1418 wchar_t wenough[10];
1419 memset (&s, '\0', sizeof (s));
1420 const char *cp = "A";
1421 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1422 || wcscmp (wenough, L"A") != 0)
1423 FAIL ();
1425 cp = "BC";
1426 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1427 || wcscmp (wenough, L"BC") != 0)
1428 FAIL ();
1430 #if __USE_FORTIFY_LEVEL >= 1
1431 CHK_FAIL_START
1432 wchar_t wsmallbuf[2];
1433 cp = "ABC";
1434 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1435 CHK_FAIL_END
1436 #endif
1438 cp = "A";
1439 if (mbstowcs (wenough, cp, 10) != 1
1440 || wcscmp (wenough, L"A") != 0)
1441 FAIL ();
1443 cp = "DEF";
1444 if (mbstowcs (wenough, cp, l0 + 10) != 3
1445 || wcscmp (wenough, L"DEF") != 0)
1446 FAIL ();
1448 #if __USE_FORTIFY_LEVEL >= 1
1449 CHK_FAIL_START
1450 wchar_t wsmallbuf[2];
1451 cp = "ABC";
1452 mbstowcs (wsmallbuf, cp, 10);
1453 CHK_FAIL_END
1454 #endif
1456 memset (&s, '\0', sizeof (s));
1457 cp = "ABC";
1458 wcscpy (wenough, L"DEF");
1459 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1460 || wcscmp (wenough, L"AEF") != 0)
1461 FAIL ();
1463 cp = "IJ";
1464 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1465 || wcscmp (wenough, L"IEF") != 0)
1466 FAIL ();
1468 #if __USE_FORTIFY_LEVEL >= 1
1469 CHK_FAIL_START
1470 wchar_t wsmallbuf[2];
1471 cp = "ABC";
1472 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1473 CHK_FAIL_END
1474 #endif
1476 memset (&s, '\0', sizeof (s));
1477 const wchar_t *wcp = L"A";
1478 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1479 || strcmp (enough, "A") != 0)
1480 FAIL ();
1482 wcp = L"BC";
1483 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1484 || strcmp (enough, "BC") != 0)
1485 FAIL ();
1487 #if __USE_FORTIFY_LEVEL >= 1
1488 CHK_FAIL_START
1489 char smallbuf[2];
1490 wcp = L"ABC";
1491 wcsrtombs (smallbuf, &wcp, 10, &s);
1492 CHK_FAIL_END
1493 #endif
1495 memset (enough, 'Z', sizeof (enough));
1496 wcp = L"EF";
1497 if (wcstombs (enough, wcp, 10) != 2
1498 || strcmp (enough, "EF") != 0)
1499 FAIL ();
1501 wcp = L"G";
1502 if (wcstombs (enough, wcp, l0 + 10) != 1
1503 || strcmp (enough, "G") != 0)
1504 FAIL ();
1506 #if __USE_FORTIFY_LEVEL >= 1
1507 CHK_FAIL_START
1508 char smallbuf[2];
1509 wcp = L"ABC";
1510 wcstombs (smallbuf, wcp, 10);
1511 CHK_FAIL_END
1512 #endif
1514 memset (&s, '\0', sizeof (s));
1515 wcp = L"AB";
1516 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1517 || strcmp (enough, "A") != 0)
1518 FAIL ();
1520 wcp = L"BCD";
1521 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1522 || strcmp (enough, "B") != 0)
1523 FAIL ();
1525 #if __USE_FORTIFY_LEVEL >= 1
1526 CHK_FAIL_START
1527 char smallbuf[2];
1528 wcp = L"ABC";
1529 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1530 CHK_FAIL_END
1531 #endif
1533 else
1535 puts ("cannot set locale");
1536 ret = 1;
1539 int fd = posix_openpt (O_RDWR);
1540 if (fd != -1)
1542 char enough[1000];
1543 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1544 FAIL ();
1546 #if __USE_FORTIFY_LEVEL >= 1
1547 CHK_FAIL_START
1548 char smallbuf[2];
1549 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1550 FAIL ();
1551 CHK_FAIL_END
1552 #endif
1553 close (fd);
1556 #if PATH_MAX > 0
1557 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1558 # if __USE_FORTIFY_LEVEL >= 1
1559 CHK_FAIL_START
1560 char smallbuf[1];
1561 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1562 CHK_FAIL_END
1563 # endif
1564 #endif
1566 gid_t grpslarge[5];
1567 int ngr = getgroups (5, grpslarge);
1568 asm volatile ("" : : "r" (ngr));
1569 #if __USE_FORTIFY_LEVEL >= 1
1570 CHK_FAIL_START
1571 char smallbuf[1];
1572 ngr = getgroups (5, (gid_t *) smallbuf);
1573 asm volatile ("" : : "r" (ngr));
1574 CHK_FAIL_END
1575 #endif
1577 fd = open (_PATH_TTY, O_RDONLY);
1578 if (fd != -1)
1580 char enough[1000];
1581 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1582 FAIL ();
1584 #if __USE_FORTIFY_LEVEL >= 1
1585 CHK_FAIL_START
1586 char smallbuf[2];
1587 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1588 FAIL ();
1589 CHK_FAIL_END
1590 #endif
1591 close (fd);
1594 char hostnamelarge[1000];
1595 gethostname (hostnamelarge, sizeof (hostnamelarge));
1596 #if __USE_FORTIFY_LEVEL >= 1
1597 CHK_FAIL_START
1598 char smallbuf[1];
1599 gethostname (smallbuf, sizeof (hostnamelarge));
1600 CHK_FAIL_END
1601 #endif
1603 char loginlarge[1000];
1604 getlogin_r (loginlarge, sizeof (hostnamelarge));
1605 #if __USE_FORTIFY_LEVEL >= 1
1606 CHK_FAIL_START
1607 char smallbuf[1];
1608 getlogin_r (smallbuf, sizeof (loginlarge));
1609 CHK_FAIL_END
1610 #endif
1612 char domainnamelarge[1000];
1613 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1614 asm volatile ("" : : "r" (res));
1615 #if __USE_FORTIFY_LEVEL >= 1
1616 CHK_FAIL_START
1617 char smallbuf[1];
1618 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1619 asm volatile ("" : : "r" (res));
1620 CHK_FAIL_END
1621 #endif
1623 fd_set s;
1624 FD_ZERO (&s);
1626 FD_SET (FD_SETSIZE - 1, &s);
1627 #if __USE_FORTIFY_LEVEL >= 1
1628 CHK_FAIL_START
1629 FD_SET (FD_SETSIZE, &s);
1630 CHK_FAIL_END
1632 CHK_FAIL_START
1633 FD_SET (l0 + FD_SETSIZE, &s);
1634 CHK_FAIL_END
1635 #endif
1637 FD_CLR (FD_SETSIZE - 1, &s);
1638 #if __USE_FORTIFY_LEVEL >= 1
1639 CHK_FAIL_START
1640 FD_CLR (FD_SETSIZE, &s);
1641 CHK_FAIL_END
1643 CHK_FAIL_START
1644 FD_SET (l0 + FD_SETSIZE, &s);
1645 CHK_FAIL_END
1646 #endif
1648 FD_ISSET (FD_SETSIZE - 1, &s);
1649 #if __USE_FORTIFY_LEVEL >= 1
1650 CHK_FAIL_START
1651 FD_ISSET (FD_SETSIZE, &s);
1652 CHK_FAIL_END
1654 CHK_FAIL_START
1655 FD_ISSET (l0 + FD_SETSIZE, &s);
1656 CHK_FAIL_END
1657 #endif
1659 struct pollfd fds[1];
1660 fds[0].fd = STDOUT_FILENO;
1661 fds[0].events = POLLOUT;
1662 poll (fds, 1, 0);
1663 #if __USE_FORTIFY_LEVEL >= 1
1664 CHK_FAIL_START
1665 poll (fds, 2, 0);
1666 CHK_FAIL_END
1668 CHK_FAIL_START
1669 poll (fds, l0 + 2, 0);
1670 CHK_FAIL_END
1671 #endif
1672 ppoll (fds, 1, NULL, NULL);
1673 #if __USE_FORTIFY_LEVEL >= 1
1674 CHK_FAIL_START
1675 ppoll (fds, 2, NULL, NULL);
1676 CHK_FAIL_END
1678 CHK_FAIL_START
1679 ppoll (fds, l0 + 2, NULL, NULL);
1680 CHK_FAIL_END
1681 #endif
1683 return ret;