ChangeLog entry for previous changeset
[glibc.git] / debug / tst-chk1.c
blob164d0ae63ec5d4fcf48a491539e4b4119bcce9fb
1 /* Copyright (C) 2004-2017 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 explicit_bzero (buf + 6, 4);
164 if (memcmp (buf, "aabcda\0\0\0\0", 10))
165 FAIL ();
167 strcpy (buf + 4, "EDCBA");
168 if (memcmp (buf, "aabcEDCBA", 10))
169 FAIL ();
171 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
172 FAIL ();
174 strncpy (buf + 6, "X", 4);
175 if (memcmp (buf, "aabcEDX\0\0", 10))
176 FAIL ();
178 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
179 FAIL ();
181 if (snprintf (buf + 7, 3, "%s", "987654") != 6
182 || memcmp (buf, "aabcEDX98", 10))
183 FAIL ();
185 /* These ops need runtime checking, but shouldn't __chk_fail. */
186 memcpy (buf, "abcdefghij", l0 + 10);
187 memmove (buf + 1, buf, l0 + 9);
188 if (memcmp (buf, "aabcdefghi", 10))
189 FAIL ();
191 memcpy (buf, "abcdefghij", l0 + 10);
192 bcopy (buf, buf + 1, l0 + 9);
193 if (memcmp (buf, "aabcdefghi", 10))
194 FAIL ();
196 if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
197 || memcmp (buf, "aabcdabcde", 10))
198 FAIL ();
200 memset (buf + 8, 'j', l0 + 2);
201 if (memcmp (buf, "aabcdabcjj", 10))
202 FAIL ();
204 bzero (buf + 8, l0 + 2);
205 if (memcmp (buf, "aabcdabc\0\0", 10))
206 FAIL ();
208 explicit_bzero (buf + 6, l0 + 4);
209 if (memcmp (buf, "aabcda\0\0\0\0", 10))
210 FAIL ();
212 strcpy (buf + 4, str1 + 5);
213 if (memcmp (buf, "aabcEDCBA", 10))
214 FAIL ();
216 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
217 FAIL ();
219 strncpy (buf + 6, "X", l0 + 4);
220 if (memcmp (buf, "aabcEDX\0\0", 10))
221 FAIL ();
223 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
224 || memcmp (buf, "aabcEcd\0\0", 10))
225 FAIL ();
227 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
228 FAIL ();
230 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
231 FAIL ();
233 buf[l0 + 8] = '\0';
234 strcat (buf, "A");
235 if (memcmp (buf, "aabcEcd9A", 10))
236 FAIL ();
238 buf[l0 + 7] = '\0';
239 strncat (buf, "ZYXWV", l0 + 2);
240 if (memcmp (buf, "aabcEcdZY", 10))
241 FAIL ();
243 /* The following tests are supposed to succeed at all fortify
244 levels, even though they overflow a.buf1 into a.buf2. */
245 memcpy (a.buf1, "abcdefghij", l0 + 10);
246 memmove (a.buf1 + 1, a.buf1, l0 + 9);
247 if (memcmp (a.buf1, "aabcdefghi", 10))
248 FAIL ();
250 memcpy (a.buf1, "abcdefghij", l0 + 10);
251 bcopy (a.buf1, a.buf1 + 1, l0 + 9);
252 if (memcmp (a.buf1, "aabcdefghi", 10))
253 FAIL ();
255 if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
256 || memcmp (a.buf1, "aabcdabcde", 10))
257 FAIL ();
259 memset (a.buf1 + 8, 'j', l0 + 2);
260 if (memcmp (a.buf1, "aabcdabcjj", 10))
261 FAIL ();
263 bzero (a.buf1 + 8, l0 + 2);
264 if (memcmp (a.buf1, "aabcdabc\0\0", 10))
265 FAIL ();
267 explicit_bzero (a.buf1 + 6, l0 + 4);
268 if (memcmp (a.buf1, "aabcda\0\0\0\0", 10))
269 FAIL ();
271 #if __USE_FORTIFY_LEVEL < 2
272 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
273 and sufficient GCC support, as the string operations overflow
274 from a.buf1 into a.buf2. */
275 strcpy (a.buf1 + 4, str1 + 5);
276 if (memcmp (a.buf1, "aabcEDCBA", 10))
277 FAIL ();
279 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
280 || memcmp (a.buf1, "aabcEDCBF", 10))
281 FAIL ();
283 strncpy (a.buf1 + 6, "X", l0 + 4);
284 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
285 FAIL ();
287 if (sprintf (a.buf1 + 7, "%d", num1) != 2
288 || memcmp (a.buf1, "aabcEDX67", 10))
289 FAIL ();
291 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
292 || memcmp (a.buf1, "aabcEDX98", 10))
293 FAIL ();
295 a.buf1[l0 + 8] = '\0';
296 strcat (a.buf1, "A");
297 if (memcmp (a.buf1, "aabcEDX9A", 10))
298 FAIL ();
300 a.buf1[l0 + 7] = '\0';
301 strncat (a.buf1, "ZYXWV", l0 + 2);
302 if (memcmp (a.buf1, "aabcEDXZY", 10))
303 FAIL ();
305 #endif
307 #if __USE_FORTIFY_LEVEL >= 1
308 /* Now check if all buffer overflows are caught at runtime.
309 N.B. All tests involving a length parameter need to be done
310 twice: once with the length a compile-time constant, once without. */
312 CHK_FAIL_START
313 memcpy (buf + 1, "abcdefghij", 10);
314 CHK_FAIL_END
316 CHK_FAIL_START
317 memcpy (buf + 1, "abcdefghij", l0 + 10);
318 CHK_FAIL_END
320 CHK_FAIL_START
321 memmove (buf + 2, buf + 1, 9);
322 CHK_FAIL_END
324 CHK_FAIL_START
325 memmove (buf + 2, buf + 1, l0 + 9);
326 CHK_FAIL_END
328 CHK_FAIL_START
329 bcopy (buf + 1, buf + 2, 9);
330 CHK_FAIL_END
332 CHK_FAIL_START
333 bcopy (buf + 1, buf + 2, l0 + 9);
334 CHK_FAIL_END
336 CHK_FAIL_START
337 p = (char *) mempcpy (buf + 6, "abcde", 5);
338 CHK_FAIL_END
340 CHK_FAIL_START
341 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
342 CHK_FAIL_END
344 CHK_FAIL_START
345 memset (buf + 9, 'j', 2);
346 CHK_FAIL_END
348 CHK_FAIL_START
349 memset (buf + 9, 'j', l0 + 2);
350 CHK_FAIL_END
352 CHK_FAIL_START
353 bzero (buf + 9, 2);
354 CHK_FAIL_END
356 CHK_FAIL_START
357 bzero (buf + 9, l0 + 2);
358 CHK_FAIL_END
360 CHK_FAIL_START
361 explicit_bzero (buf + 9, 2);
362 CHK_FAIL_END
364 CHK_FAIL_START
365 explicit_bzero (buf + 9, l0 + 2);
366 CHK_FAIL_END
368 CHK_FAIL_START
369 strcpy (buf + 5, str1 + 5);
370 CHK_FAIL_END
372 CHK_FAIL_START
373 p = stpcpy (buf + 9, str2);
374 CHK_FAIL_END
376 CHK_FAIL_START
377 strncpy (buf + 7, "X", 4);
378 CHK_FAIL_END
380 CHK_FAIL_START
381 strncpy (buf + 7, "X", l0 + 4);
382 CHK_FAIL_END
384 CHK_FAIL_START
385 stpncpy (buf + 6, "cd", 5);
386 CHK_FAIL_END
388 CHK_FAIL_START
389 stpncpy (buf + 6, "cd", l0 + 5);
390 CHK_FAIL_END
392 # if !defined __cplusplus || defined __va_arg_pack
393 CHK_FAIL_START
394 sprintf (buf + 8, "%d", num1);
395 CHK_FAIL_END
397 CHK_FAIL_START
398 snprintf (buf + 8, 3, "%d", num2);
399 CHK_FAIL_END
401 CHK_FAIL_START
402 snprintf (buf + 8, l0 + 3, "%d", num2);
403 CHK_FAIL_END
405 CHK_FAIL_START
406 swprintf (wbuf + 8, 3, L"%d", num1);
407 CHK_FAIL_END
409 CHK_FAIL_START
410 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
411 CHK_FAIL_END
412 # endif
414 memcpy (buf, str1 + 2, 9);
415 CHK_FAIL_START
416 strcat (buf, "AB");
417 CHK_FAIL_END
419 memcpy (buf, str1 + 3, 8);
420 CHK_FAIL_START
421 strncat (buf, "ZYXWV", 3);
422 CHK_FAIL_END
424 memcpy (buf, str1 + 3, 8);
425 CHK_FAIL_START
426 strncat (buf, "ZYXWV", l0 + 3);
427 CHK_FAIL_END
429 CHK_FAIL_START
430 memcpy (a.buf1 + 1, "abcdefghij", 10);
431 CHK_FAIL_END
433 CHK_FAIL_START
434 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
435 CHK_FAIL_END
437 CHK_FAIL_START
438 memmove (a.buf1 + 2, a.buf1 + 1, 9);
439 CHK_FAIL_END
441 CHK_FAIL_START
442 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
443 CHK_FAIL_END
445 CHK_FAIL_START
446 bcopy (a.buf1 + 1, a.buf1 + 2, 9);
447 CHK_FAIL_END
449 CHK_FAIL_START
450 bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
451 CHK_FAIL_END
453 CHK_FAIL_START
454 p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
455 CHK_FAIL_END
457 CHK_FAIL_START
458 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
459 CHK_FAIL_END
461 CHK_FAIL_START
462 memset (a.buf1 + 9, 'j', 2);
463 CHK_FAIL_END
465 CHK_FAIL_START
466 memset (a.buf1 + 9, 'j', l0 + 2);
467 CHK_FAIL_END
469 CHK_FAIL_START
470 bzero (a.buf1 + 9, 2);
471 CHK_FAIL_END
473 CHK_FAIL_START
474 bzero (a.buf1 + 9, l0 + 2);
475 CHK_FAIL_END
477 CHK_FAIL_START
478 explicit_bzero (a.buf1 + 9, 2);
479 CHK_FAIL_END
481 CHK_FAIL_START
482 explicit_bzero (a.buf1 + 9, l0 + 2);
483 CHK_FAIL_END
485 # if __USE_FORTIFY_LEVEL >= 2
486 # define O 0
487 # else
488 # define O 1
489 # endif
491 CHK_FAIL_START
492 strcpy (a.buf1 + (O + 4), str1 + 5);
493 CHK_FAIL_END
495 CHK_FAIL_START
496 p = stpcpy (a.buf1 + (O + 8), str2);
497 CHK_FAIL_END
499 CHK_FAIL_START
500 strncpy (a.buf1 + (O + 6), "X", 4);
501 CHK_FAIL_END
503 CHK_FAIL_START
504 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
505 CHK_FAIL_END
507 # if !defined __cplusplus || defined __va_arg_pack
508 CHK_FAIL_START
509 sprintf (a.buf1 + (O + 7), "%d", num1);
510 CHK_FAIL_END
512 CHK_FAIL_START
513 snprintf (a.buf1 + (O + 7), 3, "%d", num2);
514 CHK_FAIL_END
516 CHK_FAIL_START
517 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
518 CHK_FAIL_END
519 # endif
521 memcpy (a.buf1, str1 + (3 - O), 8 + O);
522 CHK_FAIL_START
523 strcat (a.buf1, "AB");
524 CHK_FAIL_END
526 memcpy (a.buf1, str1 + (4 - O), 7 + O);
527 CHK_FAIL_START
528 strncat (a.buf1, "ZYXWV", l0 + 3);
529 CHK_FAIL_END
530 #endif
533 /* These ops can be done without runtime checking of object size. */
534 wmemcpy (wbuf, L"abcdefghij", 10);
535 wmemmove (wbuf + 1, wbuf, 9);
536 if (wmemcmp (wbuf, L"aabcdefghi", 10))
537 FAIL ();
539 if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
540 || wmemcmp (wbuf, L"aabcdabcde", 10))
541 FAIL ();
543 wmemset (wbuf + 8, L'j', 2);
544 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
545 FAIL ();
547 wcscpy (wbuf + 4, L"EDCBA");
548 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
549 FAIL ();
551 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
552 FAIL ();
554 wcsncpy (wbuf + 6, L"X", 4);
555 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
556 FAIL ();
558 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
559 || wmemcmp (wbuf, L"aabcEDX98", 10))
560 FAIL ();
562 if (swprintf (wbuf + 7, 3, L"64") != 2
563 || wmemcmp (wbuf, L"aabcEDX64", 10))
564 FAIL ();
566 /* These ops need runtime checking, but shouldn't __chk_fail. */
567 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
568 wmemmove (wbuf + 1, wbuf, l0 + 9);
569 if (wmemcmp (wbuf, L"aabcdefghi", 10))
570 FAIL ();
572 if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
573 || wmemcmp (wbuf, L"aabcdabcde", 10))
574 FAIL ();
576 wmemset (wbuf + 8, L'j', l0 + 2);
577 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
578 FAIL ();
580 wcscpy (wbuf + 4, wstr1 + 5);
581 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
582 FAIL ();
584 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
585 FAIL ();
587 wcsncpy (wbuf + 6, L"X", l0 + 4);
588 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
589 FAIL ();
591 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
592 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
593 FAIL ();
595 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
596 || wmemcmp (wbuf, L"aabcEcd98", 10))
597 FAIL ();
599 wbuf[l0 + 8] = L'\0';
600 wcscat (wbuf, L"A");
601 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
602 FAIL ();
604 wbuf[l0 + 7] = L'\0';
605 wcsncat (wbuf, L"ZYXWV", l0 + 2);
606 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
607 FAIL ();
609 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
610 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
611 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
612 FAIL ();
614 if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
615 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
616 FAIL ();
618 wmemset (wa.buf1 + 8, L'j', l0 + 2);
619 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
620 FAIL ();
622 #if __USE_FORTIFY_LEVEL < 2
623 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
624 and sufficient GCC support, as the string operations overflow
625 from a.buf1 into a.buf2. */
626 wcscpy (wa.buf1 + 4, wstr1 + 5);
627 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
628 FAIL ();
630 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
631 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
632 FAIL ();
634 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
635 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
636 FAIL ();
638 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
639 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
640 FAIL ();
642 wa.buf1[l0 + 8] = L'\0';
643 wcscat (wa.buf1, L"A");
644 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
645 FAIL ();
647 wa.buf1[l0 + 7] = L'\0';
648 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
649 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
650 FAIL ();
652 #endif
654 #if __USE_FORTIFY_LEVEL >= 1
655 /* Now check if all buffer overflows are caught at runtime.
656 N.B. All tests involving a length parameter need to be done
657 twice: once with the length a compile-time constant, once without. */
659 CHK_FAIL_START
660 wmemcpy (wbuf + 1, L"abcdefghij", 10);
661 CHK_FAIL_END
663 CHK_FAIL_START
664 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
665 CHK_FAIL_END
667 CHK_FAIL_START
668 wmemcpy (wbuf + 9, L"abcdefghij", 10);
669 CHK_FAIL_END
671 CHK_FAIL_START
672 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
673 CHK_FAIL_END
675 CHK_FAIL_START
676 wmemmove (wbuf + 2, wbuf + 1, 9);
677 CHK_FAIL_END
679 CHK_FAIL_START
680 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
681 CHK_FAIL_END
683 CHK_FAIL_START
684 wp = wmempcpy (wbuf + 6, L"abcde", 5);
685 CHK_FAIL_END
687 CHK_FAIL_START
688 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
689 CHK_FAIL_END
691 CHK_FAIL_START
692 wmemset (wbuf + 9, L'j', 2);
693 CHK_FAIL_END
695 CHK_FAIL_START
696 wmemset (wbuf + 9, L'j', l0 + 2);
697 CHK_FAIL_END
699 CHK_FAIL_START
700 wcscpy (wbuf + 5, wstr1 + 5);
701 CHK_FAIL_END
703 CHK_FAIL_START
704 wp = wcpcpy (wbuf + 9, wstr2);
705 CHK_FAIL_END
707 CHK_FAIL_START
708 wcsncpy (wbuf + 7, L"X", 4);
709 CHK_FAIL_END
711 CHK_FAIL_START
712 wcsncpy (wbuf + 7, L"X", l0 + 4);
713 CHK_FAIL_END
715 CHK_FAIL_START
716 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
717 CHK_FAIL_END
719 CHK_FAIL_START
720 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
721 CHK_FAIL_END
723 CHK_FAIL_START
724 wcpncpy (wbuf + 6, L"cd", 5);
725 CHK_FAIL_END
727 CHK_FAIL_START
728 wcpncpy (wbuf + 6, L"cd", l0 + 5);
729 CHK_FAIL_END
731 wmemcpy (wbuf, wstr1 + 2, 9);
732 CHK_FAIL_START
733 wcscat (wbuf, L"AB");
734 CHK_FAIL_END
736 wmemcpy (wbuf, wstr1 + 3, 8);
737 CHK_FAIL_START
738 wcsncat (wbuf, L"ZYXWV", l0 + 3);
739 CHK_FAIL_END
741 CHK_FAIL_START
742 wmemcpy (wa.buf1 + 1, L"abcdefghij", 10);
743 CHK_FAIL_END
745 CHK_FAIL_START
746 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
747 CHK_FAIL_END
749 CHK_FAIL_START
750 wmemmove (wa.buf1 + 2, wa.buf1 + 1, 9);
751 CHK_FAIL_END
753 CHK_FAIL_START
754 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
755 CHK_FAIL_END
757 CHK_FAIL_START
758 wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
759 CHK_FAIL_END
761 CHK_FAIL_START
762 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
763 CHK_FAIL_END
765 CHK_FAIL_START
766 wmemset (wa.buf1 + 9, L'j', 2);
767 CHK_FAIL_END
769 CHK_FAIL_START
770 wmemset (wa.buf1 + 9, L'j', l0 + 2);
771 CHK_FAIL_END
773 #if __USE_FORTIFY_LEVEL >= 2
774 # define O 0
775 #else
776 # define O 1
777 #endif
779 CHK_FAIL_START
780 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
781 CHK_FAIL_END
783 CHK_FAIL_START
784 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
785 CHK_FAIL_END
787 CHK_FAIL_START
788 wcsncpy (wa.buf1 + (O + 6), L"X", 4);
789 CHK_FAIL_END
791 CHK_FAIL_START
792 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
793 CHK_FAIL_END
795 wmemcpy (wa.buf1, wstr1 + (3 - O), 8 + O);
796 CHK_FAIL_START
797 wcscat (wa.buf1, L"AB");
798 CHK_FAIL_END
800 wmemcpy (wa.buf1, wstr1 + (4 - O), 7 + O);
801 CHK_FAIL_START
802 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
803 CHK_FAIL_END
804 #endif
807 /* Now checks for %n protection. */
809 /* Constant literals passed directly are always ok
810 (even with warnings about possible bugs from GCC). */
811 int n1, n2;
812 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
813 || n1 != 1 || n2 != 2)
814 FAIL ();
816 /* In this case the format string is not known at compile time,
817 but resides in read-only memory, so is ok. */
818 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
819 || n1 != 1 || n2 != 2)
820 FAIL ();
822 strcpy (buf2 + 2, "%n%s%n");
823 /* When the format string is writable and contains %n,
824 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
825 CHK_FAIL2_START
826 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
827 FAIL ();
828 CHK_FAIL2_END
830 CHK_FAIL2_START
831 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
832 FAIL ();
833 CHK_FAIL2_END
835 /* But if there is no %n, even writable format string
836 should work. */
837 buf2[6] = '\0';
838 if (sprintf (buf, buf2 + 4, str2) != 1)
839 FAIL ();
841 /* Constant literals passed directly are always ok
842 (even with warnings about possible bugs from GCC). */
843 if (printf ("%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 (printf (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 (printf (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 (printf (buf2 + 4, str5) != 7)
865 FAIL ();
867 FILE *fp = stdout;
869 /* Constant literals passed directly are always ok
870 (even with warnings about possible bugs from GCC). */
871 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
872 || n1 != 7 || n2 != 14)
873 FAIL ();
875 /* In this case the format string is not known at compile time,
876 but resides in read-only memory, so is ok. */
877 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
878 || n1 != 7 || n2 != 14)
879 FAIL ();
881 strcpy (buf2 + 2, "%n%s%n");
882 /* When the format string is writable and contains %n,
883 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
884 CHK_FAIL2_START
885 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
886 FAIL ();
887 CHK_FAIL2_END
889 /* But if there is no %n, even writable format string
890 should work. */
891 buf2[6] = '\0';
892 if (fprintf (fp, buf2 + 4, str5) != 7)
893 FAIL ();
895 char *my_ptr = NULL;
896 strcpy (buf2 + 2, "%n%s%n");
897 /* When the format string is writable and contains %n,
898 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
899 CHK_FAIL2_START
900 if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
901 FAIL ();
902 else
903 free (my_ptr);
904 CHK_FAIL2_END
906 struct obstack obs;
907 obstack_init (&obs);
908 CHK_FAIL2_START
909 if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
910 FAIL ();
911 CHK_FAIL2_END
912 obstack_free (&obs, NULL);
914 my_ptr = NULL;
915 if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
916 FAIL ();
917 else
918 free (my_ptr);
920 obstack_init (&obs);
921 if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
922 FAIL ();
923 obstack_free (&obs, NULL);
925 if (freopen (temp_filename, "r", stdin) == NULL)
927 puts ("could not open temporary file");
928 exit (1);
931 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
932 FAIL ();
933 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
934 FAIL ();
936 #if __USE_FORTIFY_LEVEL >= 1
937 CHK_FAIL_START
938 if (gets (buf) != buf)
939 FAIL ();
940 CHK_FAIL_END
941 #endif
943 rewind (stdin);
945 if (fgets (buf, sizeof (buf), stdin) != buf
946 || memcmp (buf, "abcdefgh\n", 10))
947 FAIL ();
948 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
949 FAIL ();
951 rewind (stdin);
953 if (fgets (buf, l0 + sizeof (buf), stdin) != buf
954 || memcmp (buf, "abcdefgh\n", 10))
955 FAIL ();
957 #if __USE_FORTIFY_LEVEL >= 1
958 CHK_FAIL_START
959 if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
960 FAIL ();
961 CHK_FAIL_END
963 CHK_FAIL_START
964 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
965 FAIL ();
966 CHK_FAIL_END
967 #endif
969 rewind (stdin);
971 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
972 || memcmp (buf, "abcdefgh\n", 10))
973 FAIL ();
974 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
975 || memcmp (buf, "ABCDEFGHI", 10))
976 FAIL ();
978 rewind (stdin);
980 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
981 || memcmp (buf, "abcdefgh\n", 10))
982 FAIL ();
984 #if __USE_FORTIFY_LEVEL >= 1
985 CHK_FAIL_START
986 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
987 FAIL ();
988 CHK_FAIL_END
990 CHK_FAIL_START
991 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
992 FAIL ();
993 CHK_FAIL_END
994 #endif
996 rewind (stdin);
998 if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
999 || memcmp (buf, "abcdefgh\nA", 10))
1000 FAIL ();
1001 if (fread (buf, sizeof (buf), 1, stdin) != 1
1002 || memcmp (buf, "BCDEFGHI\na", 10))
1003 FAIL ();
1005 rewind (stdin);
1007 if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
1008 || memcmp (buf, "abcdefgh\nA", 10))
1009 FAIL ();
1010 if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
1011 || memcmp (buf, "BCDEFGHI\na", 10))
1012 FAIL ();
1014 #if __USE_FORTIFY_LEVEL >= 1
1015 CHK_FAIL_START
1016 if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
1017 FAIL ();
1018 CHK_FAIL_END
1020 CHK_FAIL_START
1021 if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
1022 FAIL ();
1023 CHK_FAIL_END
1024 #endif
1026 rewind (stdin);
1028 if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
1029 || memcmp (buf, "abcdefgh\nA", 10))
1030 FAIL ();
1031 if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
1032 || memcmp (buf, "BCDEFGHI\na", 10))
1033 FAIL ();
1035 rewind (stdin);
1037 if (fread_unlocked (buf, 1, 4, stdin) != 4
1038 || memcmp (buf, "abcdFGHI\na", 10))
1039 FAIL ();
1040 if (fread_unlocked (buf, 4, 1, stdin) != 1
1041 || memcmp (buf, "efghFGHI\na", 10))
1042 FAIL ();
1044 rewind (stdin);
1046 if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
1047 || memcmp (buf, "abcdefgh\nA", 10))
1048 FAIL ();
1049 if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
1050 || memcmp (buf, "BCDEFGHI\na", 10))
1051 FAIL ();
1053 #if __USE_FORTIFY_LEVEL >= 1
1054 CHK_FAIL_START
1055 if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
1056 FAIL ();
1057 CHK_FAIL_END
1059 CHK_FAIL_START
1060 if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
1061 FAIL ();
1062 CHK_FAIL_END
1063 #endif
1065 lseek (fileno (stdin), 0, SEEK_SET);
1067 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
1068 || memcmp (buf, "abcdefgh\n", 9))
1069 FAIL ();
1070 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
1071 || memcmp (buf, "ABCDEFGHI", 9))
1072 FAIL ();
1074 lseek (fileno (stdin), 0, SEEK_SET);
1076 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
1077 || memcmp (buf, "abcdefgh\n", 9))
1078 FAIL ();
1080 #if __USE_FORTIFY_LEVEL >= 1
1081 CHK_FAIL_START
1082 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
1083 FAIL ();
1084 CHK_FAIL_END
1086 CHK_FAIL_START
1087 if (read (fileno (stdin), buf, l0 + sizeof (buf) + 1) != sizeof (buf) + 1)
1088 FAIL ();
1089 CHK_FAIL_END
1090 #endif
1092 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
1093 != sizeof (buf) - 1
1094 || memcmp (buf, "\nABCDEFGH", 9))
1095 FAIL ();
1096 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
1097 || memcmp (buf, "abcdefgh\n", 9))
1098 FAIL ();
1099 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
1100 != sizeof (buf) - 1
1101 || memcmp (buf, "h\nABCDEFG", 9))
1102 FAIL ();
1104 #if __USE_FORTIFY_LEVEL >= 1
1105 CHK_FAIL_START
1106 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
1107 != sizeof (buf) + 1)
1108 FAIL ();
1109 CHK_FAIL_END
1111 CHK_FAIL_START
1112 if (pread (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
1113 != sizeof (buf) + 1)
1114 FAIL ();
1115 CHK_FAIL_END
1116 #endif
1118 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
1119 != sizeof (buf) - 1
1120 || memcmp (buf, "\nABCDEFGH", 9))
1121 FAIL ();
1122 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
1123 || memcmp (buf, "abcdefgh\n", 9))
1124 FAIL ();
1125 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
1126 != sizeof (buf) - 1
1127 || memcmp (buf, "h\nABCDEFG", 9))
1128 FAIL ();
1130 #if __USE_FORTIFY_LEVEL >= 1
1131 CHK_FAIL_START
1132 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
1133 != sizeof (buf) + 1)
1134 FAIL ();
1135 CHK_FAIL_END
1137 CHK_FAIL_START
1138 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
1139 != sizeof (buf) + 1)
1140 FAIL ();
1141 CHK_FAIL_END
1142 #endif
1144 if (freopen (temp_filename, "r", stdin) == NULL)
1146 puts ("could not open temporary file");
1147 exit (1);
1150 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
1152 puts ("could not seek in test file");
1153 exit (1);
1156 #if __USE_FORTIFY_LEVEL >= 1
1157 CHK_FAIL_START
1158 if (gets (buf) != buf)
1159 FAIL ();
1160 CHK_FAIL_END
1161 #endif
1163 /* Check whether missing N$ formats are detected. */
1164 CHK_FAIL2_START
1165 printf ("%3$d\n", 1, 2, 3, 4);
1166 CHK_FAIL2_END
1168 CHK_FAIL2_START
1169 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
1170 CHK_FAIL2_END
1172 CHK_FAIL2_START
1173 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
1174 CHK_FAIL2_END
1176 CHK_FAIL2_START
1177 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
1178 CHK_FAIL2_END
1180 int sp[2];
1181 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
1182 FAIL ();
1183 else
1185 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
1186 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
1187 != strlen (sendstr))
1188 FAIL ();
1190 char recvbuf[12];
1191 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
1192 != sizeof recvbuf
1193 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1194 FAIL ();
1196 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
1197 != sizeof recvbuf - 7
1198 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1199 FAIL ();
1201 #if __USE_FORTIFY_LEVEL >= 1
1202 CHK_FAIL_START
1203 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
1204 != sizeof recvbuf)
1205 FAIL ();
1206 CHK_FAIL_END
1208 CHK_FAIL_START
1209 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
1210 != sizeof recvbuf - 3)
1211 FAIL ();
1212 CHK_FAIL_END
1213 #endif
1215 socklen_t sl;
1216 struct sockaddr_un sa_un;
1218 sl = sizeof (sa_un);
1219 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
1220 (struct sockaddr *) &sa_un, &sl)
1221 != sizeof recvbuf
1222 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1223 FAIL ();
1225 sl = sizeof (sa_un);
1226 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
1227 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
1228 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1229 FAIL ();
1231 #if __USE_FORTIFY_LEVEL >= 1
1232 CHK_FAIL_START
1233 sl = sizeof (sa_un);
1234 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1235 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1236 FAIL ();
1237 CHK_FAIL_END
1239 CHK_FAIL_START
1240 sl = sizeof (sa_un);
1241 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1242 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1243 FAIL ();
1244 CHK_FAIL_END
1245 #endif
1247 close (sp[0]);
1248 close (sp[1]);
1251 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1252 char *enddir = strchr (fname, '\0');
1253 if (mkdtemp (fname) == NULL)
1255 printf ("mkdtemp failed: %m\n");
1256 return 1;
1258 *enddir = '/';
1259 if (symlink ("bar", fname) != 0)
1260 FAIL ();
1262 char readlinkbuf[4];
1263 if (readlink (fname, readlinkbuf, 4) != 3
1264 || memcmp (readlinkbuf, "bar", 3) != 0)
1265 FAIL ();
1266 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1267 || memcmp (readlinkbuf, "bbar", 4) != 0)
1268 FAIL ();
1270 #if __USE_FORTIFY_LEVEL >= 1
1271 CHK_FAIL_START
1272 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1273 FAIL ();
1274 CHK_FAIL_END
1276 CHK_FAIL_START
1277 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1278 FAIL ();
1279 CHK_FAIL_END
1280 #endif
1282 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1283 if (tmpfd < 0)
1284 FAIL ();
1286 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1287 || memcmp (readlinkbuf, "bar", 3) != 0)
1288 FAIL ();
1289 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1290 l0 + 3) != 3
1291 || memcmp (readlinkbuf, "bbar", 4) != 0)
1292 FAIL ();
1294 #if __USE_FORTIFY_LEVEL >= 1
1295 CHK_FAIL_START
1296 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1297 l0 + 3) != 3)
1298 FAIL ();
1299 CHK_FAIL_END
1301 CHK_FAIL_START
1302 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1303 4) != 3)
1304 FAIL ();
1305 CHK_FAIL_END
1306 #endif
1308 close (tmpfd);
1310 char *cwd1 = getcwd (NULL, 0);
1311 if (cwd1 == NULL)
1312 FAIL ();
1314 char *cwd2 = getcwd (NULL, 250);
1315 if (cwd2 == NULL)
1316 FAIL ();
1318 if (cwd1 && cwd2)
1320 if (strcmp (cwd1, cwd2) != 0)
1321 FAIL ();
1323 *enddir = '\0';
1324 if (chdir (fname))
1325 FAIL ();
1327 char *cwd3 = getcwd (NULL, 0);
1328 if (cwd3 == NULL)
1329 FAIL ();
1330 if (strcmp (fname, cwd3) != 0)
1331 printf ("getcwd after chdir is '%s' != '%s',"
1332 "get{c,}wd tests skipped\n", cwd3, fname);
1333 else
1335 char getcwdbuf[sizeof fname - 3];
1337 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1338 if (cwd4 != getcwdbuf
1339 || strcmp (getcwdbuf, fname) != 0)
1340 FAIL ();
1342 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1343 if (cwd4 != getcwdbuf + 1
1344 || getcwdbuf[0] != fname[0]
1345 || strcmp (getcwdbuf + 1, fname) != 0)
1346 FAIL ();
1348 #if __USE_FORTIFY_LEVEL >= 1
1349 CHK_FAIL_START
1350 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1351 != getcwdbuf + 2)
1352 FAIL ();
1353 CHK_FAIL_END
1355 CHK_FAIL_START
1356 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1357 != getcwdbuf + 2)
1358 FAIL ();
1359 CHK_FAIL_END
1360 #endif
1362 if (getwd (getcwdbuf) != getcwdbuf
1363 || strcmp (getcwdbuf, fname) != 0)
1364 FAIL ();
1366 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1367 || strcmp (getcwdbuf + 1, fname) != 0)
1368 FAIL ();
1370 #if __USE_FORTIFY_LEVEL >= 1
1371 CHK_FAIL_START
1372 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1373 FAIL ();
1374 CHK_FAIL_END
1375 #endif
1378 if (chdir (cwd1) != 0)
1379 FAIL ();
1380 free (cwd3);
1383 free (cwd1);
1384 free (cwd2);
1385 *enddir = '/';
1386 if (unlink (fname) != 0)
1387 FAIL ();
1389 *enddir = '\0';
1390 if (rmdir (fname) != 0)
1391 FAIL ();
1394 #if PATH_MAX > 0
1395 char largebuf[PATH_MAX];
1396 char *realres = realpath (".", largebuf);
1397 if (realres != largebuf)
1398 FAIL ();
1400 # if __USE_FORTIFY_LEVEL >= 1
1401 CHK_FAIL_START
1402 char realbuf[1];
1403 realres = realpath (".", realbuf);
1404 if (realres != realbuf)
1405 FAIL ();
1406 CHK_FAIL_END
1407 # endif
1408 #endif
1410 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1412 assert (MB_CUR_MAX <= 10);
1414 /* First a simple test. */
1415 char enough[10];
1416 if (wctomb (enough, L'A') != 1)
1417 FAIL ();
1419 #if __USE_FORTIFY_LEVEL >= 1
1420 /* We know the wchar_t encoding is ISO 10646. So pick a
1421 character which has a multibyte representation which does not
1422 fit. */
1423 CHK_FAIL_START
1424 char smallbuf[2];
1425 if (wctomb (smallbuf, L'\x100') != 2)
1426 FAIL ();
1427 CHK_FAIL_END
1428 #endif
1430 mbstate_t s;
1431 memset (&s, '\0', sizeof (s));
1432 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1433 FAIL ();
1435 #if __USE_FORTIFY_LEVEL >= 1
1436 /* We know the wchar_t encoding is ISO 10646. So pick a
1437 character which has a multibyte representation which does not
1438 fit. */
1439 CHK_FAIL_START
1440 char smallbuf[2];
1441 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1442 FAIL ();
1443 CHK_FAIL_END
1444 #endif
1446 wchar_t wenough[10];
1447 memset (&s, '\0', sizeof (s));
1448 const char *cp = "A";
1449 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1450 || wcscmp (wenough, L"A") != 0)
1451 FAIL ();
1453 cp = "BC";
1454 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1455 || wcscmp (wenough, L"BC") != 0)
1456 FAIL ();
1458 #if __USE_FORTIFY_LEVEL >= 1
1459 CHK_FAIL_START
1460 wchar_t wsmallbuf[2];
1461 cp = "ABC";
1462 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1463 CHK_FAIL_END
1464 #endif
1466 cp = "A";
1467 if (mbstowcs (wenough, cp, 10) != 1
1468 || wcscmp (wenough, L"A") != 0)
1469 FAIL ();
1471 cp = "DEF";
1472 if (mbstowcs (wenough, cp, l0 + 10) != 3
1473 || wcscmp (wenough, L"DEF") != 0)
1474 FAIL ();
1476 #if __USE_FORTIFY_LEVEL >= 1
1477 CHK_FAIL_START
1478 wchar_t wsmallbuf[2];
1479 cp = "ABC";
1480 mbstowcs (wsmallbuf, cp, 10);
1481 CHK_FAIL_END
1482 #endif
1484 memset (&s, '\0', sizeof (s));
1485 cp = "ABC";
1486 wcscpy (wenough, L"DEF");
1487 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1488 || wcscmp (wenough, L"AEF") != 0)
1489 FAIL ();
1491 cp = "IJ";
1492 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1493 || wcscmp (wenough, L"IEF") != 0)
1494 FAIL ();
1496 #if __USE_FORTIFY_LEVEL >= 1
1497 CHK_FAIL_START
1498 wchar_t wsmallbuf[2];
1499 cp = "ABC";
1500 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1501 CHK_FAIL_END
1502 #endif
1504 memset (&s, '\0', sizeof (s));
1505 const wchar_t *wcp = L"A";
1506 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1507 || strcmp (enough, "A") != 0)
1508 FAIL ();
1510 wcp = L"BC";
1511 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1512 || strcmp (enough, "BC") != 0)
1513 FAIL ();
1515 #if __USE_FORTIFY_LEVEL >= 1
1516 CHK_FAIL_START
1517 char smallbuf[2];
1518 wcp = L"ABC";
1519 wcsrtombs (smallbuf, &wcp, 10, &s);
1520 CHK_FAIL_END
1521 #endif
1523 memset (enough, 'Z', sizeof (enough));
1524 wcp = L"EF";
1525 if (wcstombs (enough, wcp, 10) != 2
1526 || strcmp (enough, "EF") != 0)
1527 FAIL ();
1529 wcp = L"G";
1530 if (wcstombs (enough, wcp, l0 + 10) != 1
1531 || strcmp (enough, "G") != 0)
1532 FAIL ();
1534 #if __USE_FORTIFY_LEVEL >= 1
1535 CHK_FAIL_START
1536 char smallbuf[2];
1537 wcp = L"ABC";
1538 wcstombs (smallbuf, wcp, 10);
1539 CHK_FAIL_END
1540 #endif
1542 memset (&s, '\0', sizeof (s));
1543 wcp = L"AB";
1544 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1545 || strcmp (enough, "A") != 0)
1546 FAIL ();
1548 wcp = L"BCD";
1549 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1550 || strcmp (enough, "B") != 0)
1551 FAIL ();
1553 #if __USE_FORTIFY_LEVEL >= 1
1554 CHK_FAIL_START
1555 char smallbuf[2];
1556 wcp = L"ABC";
1557 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1558 CHK_FAIL_END
1559 #endif
1561 else
1563 puts ("cannot set locale");
1564 ret = 1;
1567 int fd = posix_openpt (O_RDWR);
1568 if (fd != -1)
1570 char enough[1000];
1571 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1572 FAIL ();
1574 #if __USE_FORTIFY_LEVEL >= 1
1575 CHK_FAIL_START
1576 char smallbuf[2];
1577 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1578 FAIL ();
1579 CHK_FAIL_END
1580 #endif
1581 close (fd);
1584 #if PATH_MAX > 0
1585 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1586 # if __USE_FORTIFY_LEVEL >= 1
1587 CHK_FAIL_START
1588 char smallbuf[1];
1589 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1590 CHK_FAIL_END
1591 # endif
1592 #endif
1594 gid_t grpslarge[5];
1595 int ngr = getgroups (5, grpslarge);
1596 asm volatile ("" : : "r" (ngr));
1597 #if __USE_FORTIFY_LEVEL >= 1
1598 CHK_FAIL_START
1599 char smallbuf[1];
1600 ngr = getgroups (5, (gid_t *) smallbuf);
1601 asm volatile ("" : : "r" (ngr));
1602 CHK_FAIL_END
1603 #endif
1605 fd = open (_PATH_TTY, O_RDONLY);
1606 if (fd != -1)
1608 char enough[1000];
1609 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1610 FAIL ();
1612 #if __USE_FORTIFY_LEVEL >= 1
1613 CHK_FAIL_START
1614 char smallbuf[2];
1615 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1616 FAIL ();
1617 CHK_FAIL_END
1618 #endif
1619 close (fd);
1622 char hostnamelarge[1000];
1623 gethostname (hostnamelarge, sizeof (hostnamelarge));
1624 #if __USE_FORTIFY_LEVEL >= 1
1625 CHK_FAIL_START
1626 char smallbuf[1];
1627 gethostname (smallbuf, sizeof (hostnamelarge));
1628 CHK_FAIL_END
1629 #endif
1631 char loginlarge[1000];
1632 getlogin_r (loginlarge, sizeof (hostnamelarge));
1633 #if __USE_FORTIFY_LEVEL >= 1
1634 CHK_FAIL_START
1635 char smallbuf[1];
1636 getlogin_r (smallbuf, sizeof (loginlarge));
1637 CHK_FAIL_END
1638 #endif
1640 char domainnamelarge[1000];
1641 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1642 asm volatile ("" : : "r" (res));
1643 #if __USE_FORTIFY_LEVEL >= 1
1644 CHK_FAIL_START
1645 char smallbuf[1];
1646 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1647 asm volatile ("" : : "r" (res));
1648 CHK_FAIL_END
1649 #endif
1651 fd_set s;
1652 FD_ZERO (&s);
1654 FD_SET (FD_SETSIZE - 1, &s);
1655 #if __USE_FORTIFY_LEVEL >= 1
1656 CHK_FAIL_START
1657 FD_SET (FD_SETSIZE, &s);
1658 CHK_FAIL_END
1660 CHK_FAIL_START
1661 FD_SET (l0 + FD_SETSIZE, &s);
1662 CHK_FAIL_END
1663 #endif
1665 FD_CLR (FD_SETSIZE - 1, &s);
1666 #if __USE_FORTIFY_LEVEL >= 1
1667 CHK_FAIL_START
1668 FD_CLR (FD_SETSIZE, &s);
1669 CHK_FAIL_END
1671 CHK_FAIL_START
1672 FD_SET (l0 + FD_SETSIZE, &s);
1673 CHK_FAIL_END
1674 #endif
1676 FD_ISSET (FD_SETSIZE - 1, &s);
1677 #if __USE_FORTIFY_LEVEL >= 1
1678 CHK_FAIL_START
1679 FD_ISSET (FD_SETSIZE, &s);
1680 CHK_FAIL_END
1682 CHK_FAIL_START
1683 FD_ISSET (l0 + FD_SETSIZE, &s);
1684 CHK_FAIL_END
1685 #endif
1687 struct pollfd fds[1];
1688 fds[0].fd = STDOUT_FILENO;
1689 fds[0].events = POLLOUT;
1690 poll (fds, 1, 0);
1691 #if __USE_FORTIFY_LEVEL >= 1
1692 CHK_FAIL_START
1693 poll (fds, 2, 0);
1694 CHK_FAIL_END
1696 CHK_FAIL_START
1697 poll (fds, l0 + 2, 0);
1698 CHK_FAIL_END
1699 #endif
1700 ppoll (fds, 1, NULL, NULL);
1701 #if __USE_FORTIFY_LEVEL >= 1
1702 CHK_FAIL_START
1703 ppoll (fds, 2, NULL, NULL);
1704 CHK_FAIL_END
1706 CHK_FAIL_START
1707 ppoll (fds, l0 + 2, NULL, NULL);
1708 CHK_FAIL_END
1709 #endif
1711 return ret;