debug: Adapt fortify tests to libsupport
[glibc.git] / debug / tst-fortify.c
blob20e926751ac25d4179f80bf3fed84e5f19546eb8
1 /* Copyright (C) 2004-2023 Free Software Foundation, Inc.
2 Copyright The GNU Toolchain Authors.
3 This file is part of the GNU C Library.
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 <https://www.gnu.org/licenses/>. */
19 /* This file tests gets. Force it to be declared. */
20 #include <features.h>
21 #undef __GLIBC_USE_DEPRECATED_GETS
22 #define __GLIBC_USE_DEPRECATED_GETS 1
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <locale.h>
28 #include <obstack.h>
29 #include <setjmp.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <wchar.h>
36 #include <sys/poll.h>
37 #include <sys/select.h>
38 #include <sys/socket.h>
39 #include <sys/un.h>
40 #include <paths.h>
42 #include <support/temp_file.h>
43 #include <support/support.h>
45 #ifndef _GNU_SOURCE
46 # define MEMPCPY memcpy
47 # define WMEMPCPY wmemcpy
48 # define MEMPCPY_RET(x) 0
49 # define WMEMPCPY_RET(x) 0
50 #else
51 # define MEMPCPY mempcpy
52 # define WMEMPCPY wmempcpy
53 # define MEMPCPY_RET(x) __builtin_strlen (x)
54 # define WMEMPCPY_RET(x) wcslen (x)
55 #endif
57 #define obstack_chunk_alloc malloc
58 #define obstack_chunk_free free
60 static char *temp_filename;
62 static void
63 do_prepare (int argc, char *argv[])
65 int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
66 if (temp_fd == -1)
68 printf ("cannot create temporary file: %m\n");
69 exit (1);
72 const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
73 if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
75 puts ("could not write test strings into file");
76 unlink (temp_filename);
77 exit (1);
80 #define PREPARE do_prepare
82 static volatile int chk_fail_ok;
83 static volatile int ret;
84 static jmp_buf chk_fail_buf;
86 static void
87 handler (int sig)
89 if (chk_fail_ok)
91 chk_fail_ok = 0;
92 longjmp (chk_fail_buf, 1);
94 else
95 _exit (127);
98 #if __USE_FORTIFY_LEVEL == 3
99 volatile size_t buf_size = 10;
100 #else
101 char buf[10];
102 wchar_t wbuf[10];
103 #define buf_size sizeof (buf)
104 #endif
106 static volatile size_t l0;
107 static volatile char *p;
108 static volatile wchar_t *wp;
109 static const char *str1 = "JIHGFEDCBA";
110 static const char *str2 = "F";
111 static const char *str3 = "%s%n%s%n";
112 static const char *str4 = "Hello, ";
113 static const char *str5 = "World!\n";
114 static const wchar_t *wstr1 = L"JIHGFEDCBA";
115 static const wchar_t *wstr2 = L"F";
116 static const wchar_t *wstr3 = L"%s%n%s%n";
117 static const wchar_t *wstr4 = L"Hello, ";
118 static const wchar_t *wstr5 = L"World!\n";
119 static char buf2[10] = "%s";
120 static int num1 = 67;
121 static int num2 = 987654;
123 #define FAIL() \
124 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
125 #define CHK_FAIL_START \
126 chk_fail_ok = 1; \
127 if (! setjmp (chk_fail_buf)) \
129 #define CHK_FAIL_END \
130 chk_fail_ok = 0; \
131 FAIL (); \
133 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
134 # define CHK_FAIL2_START CHK_FAIL_START
135 # define CHK_FAIL2_END CHK_FAIL_END
136 #else
137 # define CHK_FAIL2_START
138 # define CHK_FAIL2_END
139 #endif
141 static int
142 do_test (void)
144 #if __USE_FORTIFY_LEVEL == 3
145 char *buf = (char *) malloc (buf_size);
146 wchar_t *wbuf = (wchar_t *) malloc (buf_size * sizeof (wchar_t));
147 #endif
148 set_fortify_handler (handler);
150 struct A { char buf1[9]; char buf2[1]; } a;
151 struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
153 printf ("Test checking routines at fortify level %d\n",
154 #ifdef __USE_FORTIFY_LEVEL
155 (int) __USE_FORTIFY_LEVEL
156 #else
158 #endif
161 #if defined __USE_FORTIFY_LEVEL && !defined __fortify_function
162 printf ("Test skipped");
163 if (l0 == 0)
164 return 0;
165 #endif
167 /* These ops can be done without runtime checking of object size. */
168 memcpy (buf, "abcdefghij", 10);
169 memmove (buf + 1, buf, 9);
170 if (memcmp (buf, "aabcdefghi", 10))
171 FAIL ();
173 memcpy (buf, "abcdefghij", 10);
174 bcopy (buf, buf + 1, 9);
175 if (memcmp (buf, "aabcdefghi", 10))
176 FAIL ();
178 if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
179 || memcmp (buf, "aabcdabcde", 10))
180 FAIL ();
182 memset (buf + 8, 'j', 2);
183 if (memcmp (buf, "aabcdabcjj", 10))
184 FAIL ();
186 bzero (buf + 8, 2);
187 if (memcmp (buf, "aabcdabc\0\0", 10))
188 FAIL ();
190 explicit_bzero (buf + 6, 4);
191 if (memcmp (buf, "aabcda\0\0\0\0", 10))
192 FAIL ();
194 strcpy (buf + 4, "EDCBA");
195 if (memcmp (buf, "aabcEDCBA", 10))
196 FAIL ();
198 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
199 FAIL ();
201 strncpy (buf + 6, "X", 4);
202 if (memcmp (buf, "aabcEDX\0\0", 10))
203 FAIL ();
205 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
206 FAIL ();
208 if (snprintf (buf + 7, 3, "%s", "987654") != 6
209 || memcmp (buf, "aabcEDX98", 10))
210 FAIL ();
212 /* These ops need runtime checking, but shouldn't __chk_fail. */
213 memcpy (buf, "abcdefghij", l0 + 10);
214 memmove (buf + 1, buf, l0 + 9);
215 if (memcmp (buf, "aabcdefghi", 10))
216 FAIL ();
218 memcpy (buf, "abcdefghij", l0 + 10);
219 bcopy (buf, buf + 1, l0 + 9);
220 if (memcmp (buf, "aabcdefghi", 10))
221 FAIL ();
223 if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
224 || memcmp (buf, "aabcdabcde", 10))
225 FAIL ();
227 memset (buf + 8, 'j', l0 + 2);
228 if (memcmp (buf, "aabcdabcjj", 10))
229 FAIL ();
231 bzero (buf + 8, l0 + 2);
232 if (memcmp (buf, "aabcdabc\0\0", 10))
233 FAIL ();
235 explicit_bzero (buf + 6, l0 + 4);
236 if (memcmp (buf, "aabcda\0\0\0\0", 10))
237 FAIL ();
239 strcpy (buf + 4, str1 + 5);
240 if (memcmp (buf, "aabcEDCBA", 10))
241 FAIL ();
243 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
244 FAIL ();
246 strncpy (buf + 6, "X", l0 + 4);
247 if (memcmp (buf, "aabcEDX\0\0", 10))
248 FAIL ();
250 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
251 || memcmp (buf, "aabcEcd\0\0", 10))
252 FAIL ();
254 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
255 FAIL ();
257 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
258 FAIL ();
260 buf[l0 + 8] = '\0';
261 strcat (buf, "A");
262 if (memcmp (buf, "aabcEcd9A", 10))
263 FAIL ();
265 buf[l0 + 7] = '\0';
266 strncat (buf, "ZYXWV", l0 + 2);
267 if (memcmp (buf, "aabcEcdZY", 10))
268 FAIL ();
270 /* The following tests are supposed to succeed at all fortify
271 levels, even though they overflow a.buf1 into a.buf2. */
272 memcpy (a.buf1, "abcdefghij", l0 + 10);
273 memmove (a.buf1 + 1, a.buf1, l0 + 9);
274 if (memcmp (a.buf1, "aabcdefghi", 10))
275 FAIL ();
277 memcpy (a.buf1, "abcdefghij", l0 + 10);
278 bcopy (a.buf1, a.buf1 + 1, l0 + 9);
279 if (memcmp (a.buf1, "aabcdefghi", 10))
280 FAIL ();
282 if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
283 != a.buf1 + 5 + MEMPCPY_RET ("abcde")
284 || memcmp (a.buf1, "aabcdabcde", 10))
285 FAIL ();
287 memset (a.buf1 + 8, 'j', l0 + 2);
288 if (memcmp (a.buf1, "aabcdabcjj", 10))
289 FAIL ();
291 bzero (a.buf1 + 8, l0 + 2);
292 if (memcmp (a.buf1, "aabcdabc\0\0", 10))
293 FAIL ();
295 explicit_bzero (a.buf1 + 6, l0 + 4);
296 if (memcmp (a.buf1, "aabcda\0\0\0\0", 10))
297 FAIL ();
299 #if __USE_FORTIFY_LEVEL < 2
300 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
301 and sufficient GCC support, as the string operations overflow
302 from a.buf1 into a.buf2. */
303 strcpy (a.buf1 + 4, str1 + 5);
304 if (memcmp (a.buf1, "aabcEDCBA", 10))
305 FAIL ();
307 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
308 || memcmp (a.buf1, "aabcEDCBF", 10))
309 FAIL ();
311 strncpy (a.buf1 + 6, "X", l0 + 4);
312 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
313 FAIL ();
315 if (sprintf (a.buf1 + 7, "%d", num1) != 2
316 || memcmp (a.buf1, "aabcEDX67", 10))
317 FAIL ();
319 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
320 || memcmp (a.buf1, "aabcEDX98", 10))
321 FAIL ();
323 a.buf1[l0 + 8] = '\0';
324 strcat (a.buf1, "A");
325 if (memcmp (a.buf1, "aabcEDX9A", 10))
326 FAIL ();
328 a.buf1[l0 + 7] = '\0';
329 strncat (a.buf1, "ZYXWV", l0 + 2);
330 if (memcmp (a.buf1, "aabcEDXZY", 10))
331 FAIL ();
333 #endif
335 #if __USE_FORTIFY_LEVEL >= 1
336 /* Now check if all buffer overflows are caught at runtime.
337 N.B. All tests involving a length parameter need to be done
338 twice: once with the length a compile-time constant, once without. */
340 CHK_FAIL_START
341 memcpy (buf + 1, "abcdefghij", 10);
342 CHK_FAIL_END
344 CHK_FAIL_START
345 memcpy (buf + 1, "abcdefghij", l0 + 10);
346 CHK_FAIL_END
348 CHK_FAIL_START
349 memmove (buf + 2, buf + 1, 9);
350 CHK_FAIL_END
352 CHK_FAIL_START
353 memmove (buf + 2, buf + 1, l0 + 9);
354 CHK_FAIL_END
356 CHK_FAIL_START
357 bcopy (buf + 1, buf + 2, 9);
358 CHK_FAIL_END
360 CHK_FAIL_START
361 bcopy (buf + 1, buf + 2, l0 + 9);
362 CHK_FAIL_END
364 #ifdef _GNU_SOURCE
365 CHK_FAIL_START
366 p = (char *) mempcpy (buf + 6, "abcde", 5);
367 CHK_FAIL_END
369 CHK_FAIL_START
370 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
371 CHK_FAIL_END
372 #endif
374 CHK_FAIL_START
375 memset (buf + 9, 'j', 2);
376 CHK_FAIL_END
378 CHK_FAIL_START
379 memset (buf + 9, 'j', l0 + 2);
380 CHK_FAIL_END
382 CHK_FAIL_START
383 bzero (buf + 9, 2);
384 CHK_FAIL_END
386 CHK_FAIL_START
387 bzero (buf + 9, l0 + 2);
388 CHK_FAIL_END
390 CHK_FAIL_START
391 explicit_bzero (buf + 9, 2);
392 CHK_FAIL_END
394 CHK_FAIL_START
395 explicit_bzero (buf + 9, l0 + 2);
396 CHK_FAIL_END
398 CHK_FAIL_START
399 strcpy (buf + 5, str1 + 5);
400 CHK_FAIL_END
402 CHK_FAIL_START
403 p = stpcpy (buf + 9, str2);
404 CHK_FAIL_END
406 CHK_FAIL_START
407 strncpy (buf + 7, "X", 4);
408 CHK_FAIL_END
410 CHK_FAIL_START
411 strncpy (buf + 7, "X", l0 + 4);
412 CHK_FAIL_END
414 CHK_FAIL_START
415 stpncpy (buf + 6, "cd", 5);
416 CHK_FAIL_END
418 CHK_FAIL_START
419 stpncpy (buf + 6, "cd", l0 + 5);
420 CHK_FAIL_END
422 # if !defined __cplusplus || defined __va_arg_pack
423 CHK_FAIL_START
424 sprintf (buf + 8, "%d", num1);
425 CHK_FAIL_END
427 CHK_FAIL_START
428 snprintf (buf + 8, 3, "%d", num2);
429 CHK_FAIL_END
431 CHK_FAIL_START
432 snprintf (buf + 8, l0 + 3, "%d", num2);
433 CHK_FAIL_END
435 CHK_FAIL_START
436 swprintf (wbuf + 8, 3, L"%d", num1);
437 CHK_FAIL_END
439 CHK_FAIL_START
440 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
441 CHK_FAIL_END
442 # endif
444 memcpy (buf, str1 + 2, 9);
445 CHK_FAIL_START
446 strcat (buf, "AB");
447 CHK_FAIL_END
449 memcpy (buf, str1 + 3, 8);
450 CHK_FAIL_START
451 strncat (buf, "ZYXWV", 3);
452 CHK_FAIL_END
454 memcpy (buf, str1 + 3, 8);
455 CHK_FAIL_START
456 strncat (buf, "ZYXWV", l0 + 3);
457 CHK_FAIL_END
459 CHK_FAIL_START
460 memcpy (a.buf1 + 1, "abcdefghij", 10);
461 CHK_FAIL_END
463 CHK_FAIL_START
464 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
465 CHK_FAIL_END
467 CHK_FAIL_START
468 memmove (a.buf1 + 2, a.buf1 + 1, 9);
469 CHK_FAIL_END
471 CHK_FAIL_START
472 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
473 CHK_FAIL_END
475 CHK_FAIL_START
476 bcopy (a.buf1 + 1, a.buf1 + 2, 9);
477 CHK_FAIL_END
479 CHK_FAIL_START
480 bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
481 CHK_FAIL_END
483 #ifdef _GNU_SOURCE
484 CHK_FAIL_START
485 p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
486 CHK_FAIL_END
488 CHK_FAIL_START
489 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
490 CHK_FAIL_END
491 #endif
493 CHK_FAIL_START
494 memset (a.buf1 + 9, 'j', 2);
495 CHK_FAIL_END
497 CHK_FAIL_START
498 memset (a.buf1 + 9, 'j', l0 + 2);
499 CHK_FAIL_END
501 CHK_FAIL_START
502 bzero (a.buf1 + 9, 2);
503 CHK_FAIL_END
505 CHK_FAIL_START
506 bzero (a.buf1 + 9, l0 + 2);
507 CHK_FAIL_END
509 CHK_FAIL_START
510 explicit_bzero (a.buf1 + 9, 2);
511 CHK_FAIL_END
513 CHK_FAIL_START
514 explicit_bzero (a.buf1 + 9, l0 + 2);
515 CHK_FAIL_END
517 # if __USE_FORTIFY_LEVEL >= 2
518 # define O 0
519 # else
520 # define O 1
521 # endif
523 CHK_FAIL_START
524 strcpy (a.buf1 + (O + 4), str1 + 5);
525 CHK_FAIL_END
527 CHK_FAIL_START
528 p = stpcpy (a.buf1 + (O + 8), str2);
529 CHK_FAIL_END
531 CHK_FAIL_START
532 strncpy (a.buf1 + (O + 6), "X", 4);
533 CHK_FAIL_END
535 CHK_FAIL_START
536 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
537 CHK_FAIL_END
539 CHK_FAIL_START
540 strlcpy (a.buf1 + (O + 6), "X", 4);
541 CHK_FAIL_END
543 CHK_FAIL_START
544 strlcpy (a.buf1 + (O + 6), "X", l0 + 4);
545 CHK_FAIL_END
548 char *volatile buf2 = buf;
549 if (strlcpy (buf2, "a", sizeof (buf) + 1) != 1)
550 FAIL ();
553 # if !defined __cplusplus || defined __va_arg_pack
554 CHK_FAIL_START
555 sprintf (a.buf1 + (O + 7), "%d", num1);
556 CHK_FAIL_END
558 CHK_FAIL_START
559 snprintf (a.buf1 + (O + 7), 3, "%d", num2);
560 CHK_FAIL_END
562 CHK_FAIL_START
563 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
564 CHK_FAIL_END
565 # endif
567 memcpy (a.buf1, str1 + (3 - O), 8 + O);
568 CHK_FAIL_START
569 strcat (a.buf1, "AB");
570 CHK_FAIL_END
572 memcpy (a.buf1, str1 + (4 - O), 7 + O);
573 CHK_FAIL_START
574 strncat (a.buf1, "ZYXWV", l0 + 3);
575 CHK_FAIL_END
577 memset (a.buf1, 0, sizeof (a.buf1));
578 CHK_FAIL_START
579 strlcat (a.buf1 + (O + 6), "X", 4);
580 CHK_FAIL_END
582 memset (a.buf1, 0, sizeof (a.buf1));
583 CHK_FAIL_START
584 strlcat (a.buf1 + (O + 6), "X", l0 + 4);
585 CHK_FAIL_END
588 buf[0] = '\0';
589 char *volatile buf2 = buf;
590 if (strlcat (buf2, "a", sizeof (buf) + 1) != 1)
591 FAIL ();
593 #endif
596 /* These ops can be done without runtime checking of object size. */
597 wmemcpy (wbuf, L"abcdefghij", 10);
598 wmemmove (wbuf + 1, wbuf, 9);
599 if (wmemcmp (wbuf, L"aabcdefghi", 10))
600 FAIL ();
602 if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
603 || wmemcmp (wbuf, L"aabcdabcde", 10))
604 FAIL ();
606 wmemset (wbuf + 8, L'j', 2);
607 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
608 FAIL ();
610 wcscpy (wbuf + 4, L"EDCBA");
611 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
612 FAIL ();
614 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
615 FAIL ();
617 wcsncpy (wbuf + 6, L"X", 4);
618 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
619 FAIL ();
621 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
622 || wmemcmp (wbuf, L"aabcEDX98", 10))
623 FAIL ();
625 if (swprintf (wbuf + 7, 3, L"64") != 2
626 || wmemcmp (wbuf, L"aabcEDX64", 10))
627 FAIL ();
629 /* These ops need runtime checking, but shouldn't __chk_fail. */
630 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
631 wmemmove (wbuf + 1, wbuf, l0 + 9);
632 if (wmemcmp (wbuf, L"aabcdefghi", 10))
633 FAIL ();
635 if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
636 != wbuf + 5 + WMEMPCPY_RET (L"abcde")
637 || wmemcmp (wbuf, L"aabcdabcde", 10))
638 FAIL ();
640 wmemset (wbuf + 8, L'j', l0 + 2);
641 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
642 FAIL ();
644 wcscpy (wbuf + 4, wstr1 + 5);
645 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
646 FAIL ();
648 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
649 FAIL ();
651 wcsncpy (wbuf + 6, L"X", l0 + 4);
652 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
653 FAIL ();
655 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
656 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
657 FAIL ();
659 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
660 || wmemcmp (wbuf, L"aabcEcd98", 10))
661 FAIL ();
663 wbuf[l0 + 8] = L'\0';
664 wcscat (wbuf, L"A");
665 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
666 FAIL ();
668 wbuf[l0 + 7] = L'\0';
669 wcsncat (wbuf, L"ZYXWV", l0 + 2);
670 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
671 FAIL ();
673 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
674 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
675 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
676 FAIL ();
678 if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
679 != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
680 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
681 FAIL ();
683 wmemset (wa.buf1 + 8, L'j', l0 + 2);
684 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
685 FAIL ();
687 #if __USE_FORTIFY_LEVEL < 2
688 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
689 and sufficient GCC support, as the string operations overflow
690 from a.buf1 into a.buf2. */
691 wcscpy (wa.buf1 + 4, wstr1 + 5);
692 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
693 FAIL ();
695 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
696 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
697 FAIL ();
699 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
700 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
701 FAIL ();
703 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
704 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
705 FAIL ();
707 wa.buf1[l0 + 8] = L'\0';
708 wcscat (wa.buf1, L"A");
709 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
710 FAIL ();
712 wa.buf1[l0 + 7] = L'\0';
713 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
714 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
715 FAIL ();
717 #endif
719 #if __USE_FORTIFY_LEVEL >= 1
720 /* Now check if all buffer overflows are caught at runtime.
721 N.B. All tests involving a length parameter need to be done
722 twice: once with the length a compile-time constant, once without. */
724 CHK_FAIL_START
725 wmemcpy (wbuf + 1, L"abcdefghij", 10);
726 CHK_FAIL_END
728 CHK_FAIL_START
729 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
730 CHK_FAIL_END
732 CHK_FAIL_START
733 wmemcpy (wbuf + 9, L"abcdefghij", 10);
734 CHK_FAIL_END
736 CHK_FAIL_START
737 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
738 CHK_FAIL_END
740 CHK_FAIL_START
741 wmemmove (wbuf + 2, wbuf + 1, 9);
742 CHK_FAIL_END
744 CHK_FAIL_START
745 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
746 CHK_FAIL_END
748 #ifdef _GNU_SOURCE
749 CHK_FAIL_START
750 wp = wmempcpy (wbuf + 6, L"abcde", 5);
751 CHK_FAIL_END
753 CHK_FAIL_START
754 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
755 CHK_FAIL_END
756 #endif
758 CHK_FAIL_START
759 wmemset (wbuf + 9, L'j', 2);
760 CHK_FAIL_END
762 CHK_FAIL_START
763 wmemset (wbuf + 9, L'j', l0 + 2);
764 CHK_FAIL_END
766 CHK_FAIL_START
767 wcscpy (wbuf + 5, wstr1 + 5);
768 CHK_FAIL_END
770 CHK_FAIL_START
771 wp = wcpcpy (wbuf + 9, wstr2);
772 CHK_FAIL_END
774 CHK_FAIL_START
775 wcsncpy (wbuf + 7, L"X", 4);
776 CHK_FAIL_END
778 CHK_FAIL_START
779 wcsncpy (wbuf + 7, L"X", l0 + 4);
780 CHK_FAIL_END
782 CHK_FAIL_START
783 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
784 CHK_FAIL_END
786 CHK_FAIL_START
787 wcslcpy (wbuf + 7, L"X", 4);
788 CHK_FAIL_END
790 CHK_FAIL_START
791 wcslcpy (wbuf + 7, L"X", l0 + 4);
792 CHK_FAIL_END
794 CHK_FAIL_START
795 wcslcpy (wbuf + 9, L"XABCDEFGH", 8);
796 CHK_FAIL_END
798 CHK_FAIL_START
799 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
800 CHK_FAIL_END
802 CHK_FAIL_START
803 wcpncpy (wbuf + 6, L"cd", 5);
804 CHK_FAIL_END
806 CHK_FAIL_START
807 wcpncpy (wbuf + 6, L"cd", l0 + 5);
808 CHK_FAIL_END
810 wmemcpy (wbuf, wstr1 + 2, 9);
811 CHK_FAIL_START
812 wcscat (wbuf, L"AB");
813 CHK_FAIL_END
815 wmemcpy (wbuf, wstr1 + 3, 8);
816 CHK_FAIL_START
817 wcsncat (wbuf, L"ZYXWV", l0 + 3);
818 CHK_FAIL_END
820 wmemcpy (wbuf, wstr1 + 4, 7);
821 CHK_FAIL_START
822 wcslcat (wbuf, L"ZYXWV", l0 + 11);
823 CHK_FAIL_END
825 CHK_FAIL_START
826 wmemcpy (wa.buf1 + 1, L"abcdefghij", 10);
827 CHK_FAIL_END
829 CHK_FAIL_START
830 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
831 CHK_FAIL_END
833 CHK_FAIL_START
834 wmemmove (wa.buf1 + 2, wa.buf1 + 1, 9);
835 CHK_FAIL_END
837 CHK_FAIL_START
838 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
839 CHK_FAIL_END
841 #ifdef _GNU_SOURCE
842 CHK_FAIL_START
843 wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
844 CHK_FAIL_END
846 CHK_FAIL_START
847 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
848 CHK_FAIL_END
849 #endif
851 CHK_FAIL_START
852 wmemset (wa.buf1 + 9, L'j', 2);
853 CHK_FAIL_END
855 CHK_FAIL_START
856 wmemset (wa.buf1 + 9, L'j', l0 + 2);
857 CHK_FAIL_END
859 #if __USE_FORTIFY_LEVEL >= 2
860 # define O 0
861 #else
862 # define O 1
863 #endif
865 CHK_FAIL_START
866 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
867 CHK_FAIL_END
869 CHK_FAIL_START
870 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
871 CHK_FAIL_END
873 CHK_FAIL_START
874 wcsncpy (wa.buf1 + (O + 6), L"X", 4);
875 CHK_FAIL_END
877 CHK_FAIL_START
878 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
879 CHK_FAIL_END
881 wmemcpy (wa.buf1, wstr1 + (3 - O), 8 + O);
882 CHK_FAIL_START
883 wcscat (wa.buf1, L"AB");
884 CHK_FAIL_END
886 wmemcpy (wa.buf1, wstr1 + (4 - O), 7 + O);
887 CHK_FAIL_START
888 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
889 CHK_FAIL_END
890 #endif
893 /* Now checks for %n protection. */
895 /* Constant literals passed directly are always ok
896 (even with warnings about possible bugs from GCC). */
897 int n1, n2;
898 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
899 || n1 != 1 || n2 != 2)
900 FAIL ();
902 /* In this case the format string is not known at compile time,
903 but resides in read-only memory, so is ok. */
904 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
905 || n1 != 1 || n2 != 2)
906 FAIL ();
908 strcpy (buf2 + 2, "%n%s%n");
909 /* When the format string is writable and contains %n,
910 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
911 CHK_FAIL2_START
912 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
913 FAIL ();
914 CHK_FAIL2_END
916 CHK_FAIL2_START
917 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
918 FAIL ();
919 CHK_FAIL2_END
921 /* But if there is no %n, even writable format string
922 should work. */
923 buf2[6] = '\0';
924 if (sprintf (buf, buf2 + 4, str2) != 1)
925 FAIL ();
927 /* Constant literals passed directly are always ok
928 (even with warnings about possible bugs from GCC). */
929 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
930 || n1 != 7 || n2 != 14)
931 FAIL ();
933 /* In this case the format string is not known at compile time,
934 but resides in read-only memory, so is ok. */
935 if (printf (str3, str4, &n1, str5, &n2) != 14
936 || n1 != 7 || n2 != 14)
937 FAIL ();
939 strcpy (buf2 + 2, "%n%s%n");
940 /* When the format string is writable and contains %n,
941 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
942 CHK_FAIL2_START
943 if (printf (buf2, str4, &n1, str5, &n1) != 14)
944 FAIL ();
945 CHK_FAIL2_END
947 /* But if there is no %n, even writable format string
948 should work. */
949 buf2[6] = '\0';
950 if (printf (buf2 + 4, str5) != 7)
951 FAIL ();
953 FILE *fp = stdout;
955 /* Constant literals passed directly are always ok
956 (even with warnings about possible bugs from GCC). */
957 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
958 || n1 != 7 || n2 != 14)
959 FAIL ();
961 /* In this case the format string is not known at compile time,
962 but resides in read-only memory, so is ok. */
963 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
964 || n1 != 7 || n2 != 14)
965 FAIL ();
967 strcpy (buf2 + 2, "%n%s%n");
968 /* When the format string is writable and contains %n,
969 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
970 CHK_FAIL2_START
971 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
972 FAIL ();
973 CHK_FAIL2_END
975 /* But if there is no %n, even writable format string
976 should work. */
977 buf2[6] = '\0';
978 if (fprintf (fp, buf2 + 4, str5) != 7)
979 FAIL ();
981 #ifdef _GNU_SOURCE
982 char *my_ptr = NULL;
983 strcpy (buf2 + 2, "%n%s%n");
984 /* When the format string is writable and contains %n,
985 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
986 CHK_FAIL2_START
987 if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
988 FAIL ();
989 else
990 free (my_ptr);
991 CHK_FAIL2_END
993 struct obstack obs;
994 obstack_init (&obs);
995 CHK_FAIL2_START
996 if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
997 FAIL ();
998 CHK_FAIL2_END
999 obstack_free (&obs, NULL);
1001 my_ptr = NULL;
1002 if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
1003 FAIL ();
1004 else
1005 free (my_ptr);
1007 obstack_init (&obs);
1008 if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
1009 FAIL ();
1010 obstack_free (&obs, NULL);
1011 #endif
1013 if (freopen (temp_filename, "r", stdin) == NULL)
1015 puts ("could not open temporary file");
1016 exit (1);
1019 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
1020 FAIL ();
1021 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
1022 FAIL ();
1024 #if __USE_FORTIFY_LEVEL >= 1
1025 CHK_FAIL_START
1026 if (gets (buf) != buf)
1027 FAIL ();
1028 CHK_FAIL_END
1029 #endif
1031 rewind (stdin);
1033 if (fgets (buf, buf_size, stdin) != buf
1034 || memcmp (buf, "abcdefgh\n", 10))
1035 FAIL ();
1036 if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
1037 FAIL ();
1039 rewind (stdin);
1041 if (fgets (buf, l0 + buf_size, stdin) != buf
1042 || memcmp (buf, "abcdefgh\n", 10))
1043 FAIL ();
1045 #if __USE_FORTIFY_LEVEL >= 1
1046 CHK_FAIL_START
1047 if (fgets (buf, buf_size + 1, stdin) != buf)
1048 FAIL ();
1049 CHK_FAIL_END
1051 CHK_FAIL_START
1052 if (fgets (buf, l0 + buf_size + 1, stdin) != buf)
1053 FAIL ();
1054 CHK_FAIL_END
1055 #endif
1057 rewind (stdin);
1059 #ifdef _GNU_SOURCE
1060 if (fgets_unlocked (buf, buf_size, stdin) != buf
1061 || memcmp (buf, "abcdefgh\n", 10))
1062 FAIL ();
1063 if (fgets_unlocked (buf, buf_size, stdin) != buf
1064 || memcmp (buf, "ABCDEFGHI", 10))
1065 FAIL ();
1067 rewind (stdin);
1069 if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf
1070 || memcmp (buf, "abcdefgh\n", 10))
1071 FAIL ();
1073 #if __USE_FORTIFY_LEVEL >= 1
1074 CHK_FAIL_START
1075 if (fgets_unlocked (buf, buf_size + 1, stdin) != buf)
1076 FAIL ();
1077 CHK_FAIL_END
1079 CHK_FAIL_START
1080 if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf)
1081 FAIL ();
1082 CHK_FAIL_END
1083 #endif
1085 rewind (stdin);
1086 #endif
1088 if (fread (buf, 1, buf_size, stdin) != buf_size
1089 || memcmp (buf, "abcdefgh\nA", 10))
1090 FAIL ();
1091 if (fread (buf, buf_size, 1, stdin) != 1
1092 || memcmp (buf, "BCDEFGHI\na", 10))
1093 FAIL ();
1095 rewind (stdin);
1097 if (fread (buf, l0 + 1, buf_size, stdin) != buf_size
1098 || memcmp (buf, "abcdefgh\nA", 10))
1099 FAIL ();
1100 if (fread (buf, buf_size, l0 + 1, stdin) != 1
1101 || memcmp (buf, "BCDEFGHI\na", 10))
1102 FAIL ();
1104 #if __USE_FORTIFY_LEVEL >= 1
1105 CHK_FAIL_START
1106 if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1)
1107 FAIL ();
1108 CHK_FAIL_END
1110 CHK_FAIL_START
1111 if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1)
1112 FAIL ();
1113 CHK_FAIL_END
1114 #endif
1116 rewind (stdin);
1118 if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size
1119 || memcmp (buf, "abcdefgh\nA", 10))
1120 FAIL ();
1121 if (fread_unlocked (buf, buf_size, 1, stdin) != 1
1122 || memcmp (buf, "BCDEFGHI\na", 10))
1123 FAIL ();
1125 rewind (stdin);
1127 if (fread_unlocked (buf, 1, 4, stdin) != 4
1128 || memcmp (buf, "abcdFGHI\na", 10))
1129 FAIL ();
1130 if (fread_unlocked (buf, 4, 1, stdin) != 1
1131 || memcmp (buf, "efghFGHI\na", 10))
1132 FAIL ();
1134 rewind (stdin);
1136 if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size
1137 || memcmp (buf, "abcdefgh\nA", 10))
1138 FAIL ();
1139 if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1
1140 || memcmp (buf, "BCDEFGHI\na", 10))
1141 FAIL ();
1143 #if __USE_FORTIFY_LEVEL >= 1
1144 CHK_FAIL_START
1145 if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1)
1146 FAIL ();
1147 CHK_FAIL_END
1149 CHK_FAIL_START
1150 if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1)
1151 FAIL ();
1152 CHK_FAIL_END
1153 #endif
1155 lseek (fileno (stdin), 0, SEEK_SET);
1157 if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
1158 || memcmp (buf, "abcdefgh\n", 9))
1159 FAIL ();
1160 if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
1161 || memcmp (buf, "ABCDEFGHI", 9))
1162 FAIL ();
1164 lseek (fileno (stdin), 0, SEEK_SET);
1166 if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1
1167 || memcmp (buf, "abcdefgh\n", 9))
1168 FAIL ();
1170 #if __USE_FORTIFY_LEVEL >= 1
1171 CHK_FAIL_START
1172 if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1)
1173 FAIL ();
1174 CHK_FAIL_END
1176 CHK_FAIL_START
1177 if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1)
1178 FAIL ();
1179 CHK_FAIL_END
1180 #endif
1182 if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2)
1183 != buf_size - 1
1184 || memcmp (buf, "\nABCDEFGH", 9))
1185 FAIL ();
1186 if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
1187 || memcmp (buf, "abcdefgh\n", 9))
1188 FAIL ();
1189 if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
1190 != buf_size - 1
1191 || memcmp (buf, "h\nABCDEFG", 9))
1192 FAIL ();
1194 #if __USE_FORTIFY_LEVEL >= 1
1195 CHK_FAIL_START
1196 if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
1197 != buf_size + 1)
1198 FAIL ();
1199 CHK_FAIL_END
1201 CHK_FAIL_START
1202 if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
1203 != buf_size + 1)
1204 FAIL ();
1205 CHK_FAIL_END
1206 #endif
1208 if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2)
1209 != buf_size - 1
1210 || memcmp (buf, "\nABCDEFGH", 9))
1211 FAIL ();
1212 if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
1213 || memcmp (buf, "abcdefgh\n", 9))
1214 FAIL ();
1215 if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
1216 != buf_size - 1
1217 || memcmp (buf, "h\nABCDEFG", 9))
1218 FAIL ();
1220 #if __USE_FORTIFY_LEVEL >= 1
1221 CHK_FAIL_START
1222 if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
1223 != buf_size + 1)
1224 FAIL ();
1225 CHK_FAIL_END
1227 CHK_FAIL_START
1228 if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
1229 != buf_size + 1)
1230 FAIL ();
1231 CHK_FAIL_END
1232 #endif
1234 if (freopen (temp_filename, "r", stdin) == NULL)
1236 puts ("could not open temporary file");
1237 exit (1);
1240 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
1242 puts ("could not seek in test file");
1243 exit (1);
1246 #if __USE_FORTIFY_LEVEL >= 1
1247 CHK_FAIL_START
1248 if (gets (buf) != buf)
1249 FAIL ();
1250 CHK_FAIL_END
1251 #endif
1253 /* Check whether missing N$ formats are detected. */
1254 CHK_FAIL2_START
1255 printf ("%3$d\n", 1, 2, 3, 4);
1256 CHK_FAIL2_END
1258 CHK_FAIL2_START
1259 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
1260 CHK_FAIL2_END
1262 CHK_FAIL2_START
1263 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
1264 CHK_FAIL2_END
1266 CHK_FAIL2_START
1267 snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
1268 CHK_FAIL2_END
1270 int sp[2];
1271 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
1272 FAIL ();
1273 else
1275 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
1276 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
1277 != strlen (sendstr))
1278 FAIL ();
1280 char recvbuf[12];
1281 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
1282 != sizeof recvbuf
1283 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1284 FAIL ();
1286 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
1287 != sizeof recvbuf - 7
1288 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1289 FAIL ();
1291 #if __USE_FORTIFY_LEVEL >= 1
1292 CHK_FAIL_START
1293 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
1294 != sizeof recvbuf)
1295 FAIL ();
1296 CHK_FAIL_END
1298 CHK_FAIL_START
1299 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
1300 != sizeof recvbuf - 3)
1301 FAIL ();
1302 CHK_FAIL_END
1303 #endif
1305 socklen_t sl;
1306 struct sockaddr_un sa_un;
1308 sl = sizeof (sa_un);
1309 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
1310 (struct sockaddr *) &sa_un, &sl)
1311 != sizeof recvbuf
1312 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1313 FAIL ();
1315 sl = sizeof (sa_un);
1316 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
1317 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
1318 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1319 FAIL ();
1321 #if __USE_FORTIFY_LEVEL >= 1
1322 CHK_FAIL_START
1323 sl = sizeof (sa_un);
1324 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1325 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1326 FAIL ();
1327 CHK_FAIL_END
1329 CHK_FAIL_START
1330 sl = sizeof (sa_un);
1331 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1332 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1333 FAIL ();
1334 CHK_FAIL_END
1335 #endif
1337 close (sp[0]);
1338 close (sp[1]);
1341 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1342 char *enddir = strchr (fname, '\0');
1343 if (mkdtemp (fname) == NULL)
1345 printf ("mkdtemp failed: %m\n");
1346 return 1;
1348 *enddir = '/';
1349 if (symlink ("bar", fname) != 0)
1350 FAIL ();
1352 char readlinkbuf[4];
1353 if (readlink (fname, readlinkbuf, 4) != 3
1354 || memcmp (readlinkbuf, "bar", 3) != 0)
1355 FAIL ();
1356 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1357 || memcmp (readlinkbuf, "bbar", 4) != 0)
1358 FAIL ();
1360 #if __USE_FORTIFY_LEVEL >= 1
1361 CHK_FAIL_START
1362 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1363 FAIL ();
1364 CHK_FAIL_END
1366 CHK_FAIL_START
1367 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1368 FAIL ();
1369 CHK_FAIL_END
1370 #endif
1372 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1373 if (tmpfd < 0)
1374 FAIL ();
1376 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1377 || memcmp (readlinkbuf, "bar", 3) != 0)
1378 FAIL ();
1379 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1380 l0 + 3) != 3
1381 || memcmp (readlinkbuf, "bbar", 4) != 0)
1382 FAIL ();
1384 #if __USE_FORTIFY_LEVEL >= 1
1385 CHK_FAIL_START
1386 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1387 l0 + 3) != 3)
1388 FAIL ();
1389 CHK_FAIL_END
1391 CHK_FAIL_START
1392 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1393 4) != 3)
1394 FAIL ();
1395 CHK_FAIL_END
1396 #endif
1398 close (tmpfd);
1400 char *cwd1 = getcwd (NULL, 0);
1401 if (cwd1 == NULL)
1402 FAIL ();
1404 char *cwd2 = getcwd (NULL, 250);
1405 if (cwd2 == NULL)
1406 FAIL ();
1408 if (cwd1 && cwd2)
1410 if (strcmp (cwd1, cwd2) != 0)
1411 FAIL ();
1413 *enddir = '\0';
1414 if (chdir (fname))
1415 FAIL ();
1417 char *cwd3 = getcwd (NULL, 0);
1418 if (cwd3 == NULL)
1419 FAIL ();
1420 if (strcmp (fname, cwd3) != 0)
1421 printf ("getcwd after chdir is '%s' != '%s',"
1422 "get{c,}wd tests skipped\n", cwd3, fname);
1423 else
1425 char getcwdbuf[sizeof fname - 3];
1427 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1428 if (cwd4 != getcwdbuf
1429 || strcmp (getcwdbuf, fname) != 0)
1430 FAIL ();
1432 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1433 if (cwd4 != getcwdbuf + 1
1434 || getcwdbuf[0] != fname[0]
1435 || strcmp (getcwdbuf + 1, fname) != 0)
1436 FAIL ();
1438 #if __USE_FORTIFY_LEVEL >= 1
1439 CHK_FAIL_START
1440 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1441 != getcwdbuf + 2)
1442 FAIL ();
1443 CHK_FAIL_END
1445 CHK_FAIL_START
1446 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1447 != getcwdbuf + 2)
1448 FAIL ();
1449 CHK_FAIL_END
1450 #endif
1452 if (getwd (getcwdbuf) != getcwdbuf
1453 || strcmp (getcwdbuf, fname) != 0)
1454 FAIL ();
1456 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1457 || strcmp (getcwdbuf + 1, fname) != 0)
1458 FAIL ();
1460 #if __USE_FORTIFY_LEVEL >= 1
1461 CHK_FAIL_START
1462 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1463 FAIL ();
1464 CHK_FAIL_END
1465 #endif
1468 if (chdir (cwd1) != 0)
1469 FAIL ();
1470 free (cwd3);
1473 free (cwd1);
1474 free (cwd2);
1475 *enddir = '/';
1476 if (unlink (fname) != 0)
1477 FAIL ();
1479 *enddir = '\0';
1480 if (rmdir (fname) != 0)
1481 FAIL ();
1484 #if PATH_MAX > 0
1485 char largebuf[PATH_MAX];
1486 char *realres = realpath (".", largebuf);
1487 if (realres != largebuf)
1488 FAIL ();
1490 # if __USE_FORTIFY_LEVEL >= 1
1491 CHK_FAIL_START
1492 char realbuf[1];
1493 realres = realpath (".", realbuf);
1494 if (realres != realbuf)
1495 FAIL ();
1496 CHK_FAIL_END
1497 # endif
1498 #endif
1500 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1502 assert (MB_CUR_MAX <= 10);
1504 /* First a simple test. */
1505 char enough[10];
1506 if (wctomb (enough, L'A') != 1)
1507 FAIL ();
1509 #if __USE_FORTIFY_LEVEL >= 1
1510 /* We know the wchar_t encoding is ISO 10646. So pick a
1511 character which has a multibyte representation which does not
1512 fit. */
1513 CHK_FAIL_START
1514 char smallbuf[2];
1515 if (wctomb (smallbuf, L'\x100') != 2)
1516 FAIL ();
1517 CHK_FAIL_END
1518 #endif
1520 mbstate_t s;
1521 memset (&s, '\0', sizeof (s));
1522 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1523 FAIL ();
1525 #if __USE_FORTIFY_LEVEL >= 1
1526 /* We know the wchar_t encoding is ISO 10646. So pick a
1527 character which has a multibyte representation which does not
1528 fit. */
1529 CHK_FAIL_START
1530 char smallbuf[1];
1531 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1532 FAIL ();
1533 CHK_FAIL_END
1535 /* Same input with a large enough buffer and we're good. */
1536 char bigenoughbuf[2];
1537 if (wcrtomb (bigenoughbuf, L'\x100', &s) != 2)
1538 FAIL ();
1539 #endif
1541 wchar_t wenough[10];
1542 memset (&s, '\0', sizeof (s));
1543 const char *cp = "A";
1544 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1545 || wcscmp (wenough, L"A") != 0)
1546 FAIL ();
1548 cp = "BC";
1549 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1550 || wcscmp (wenough, L"BC") != 0)
1551 FAIL ();
1553 #if __USE_FORTIFY_LEVEL >= 1
1554 CHK_FAIL_START
1555 wchar_t wsmallbuf[2];
1556 cp = "ABC";
1557 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1558 CHK_FAIL_END
1559 #endif
1561 /* Bug 29030 regression check */
1562 cp = "HelloWorld";
1563 if (mbsrtowcs (NULL, &cp, (size_t)-1, &s) != 10)
1564 FAIL ();
1566 cp = "A";
1567 if (mbstowcs (wenough, cp, 10) != 1
1568 || wcscmp (wenough, L"A") != 0)
1569 FAIL ();
1571 cp = "DEF";
1572 if (mbstowcs (wenough, cp, l0 + 10) != 3
1573 || wcscmp (wenough, L"DEF") != 0)
1574 FAIL ();
1576 #if __USE_FORTIFY_LEVEL >= 1
1577 CHK_FAIL_START
1578 wchar_t wsmallbuf[2];
1579 cp = "ABC";
1580 mbstowcs (wsmallbuf, cp, 10);
1581 CHK_FAIL_END
1582 #endif
1584 memset (&s, '\0', sizeof (s));
1585 cp = "ABC";
1586 wcscpy (wenough, L"DEF");
1587 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1588 || wcscmp (wenough, L"AEF") != 0)
1589 FAIL ();
1591 cp = "IJ";
1592 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1593 || wcscmp (wenough, L"IEF") != 0)
1594 FAIL ();
1596 #if __USE_FORTIFY_LEVEL >= 1
1597 CHK_FAIL_START
1598 wchar_t wsmallbuf[2];
1599 cp = "ABC";
1600 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1601 CHK_FAIL_END
1602 #endif
1604 memset (&s, '\0', sizeof (s));
1605 const wchar_t *wcp = L"A";
1606 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1607 || strcmp (enough, "A") != 0)
1608 FAIL ();
1610 wcp = L"BC";
1611 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1612 || strcmp (enough, "BC") != 0)
1613 FAIL ();
1615 #if __USE_FORTIFY_LEVEL >= 1
1616 CHK_FAIL_START
1617 char smallbuf[2];
1618 wcp = L"ABC";
1619 wcsrtombs (smallbuf, &wcp, 10, &s);
1620 CHK_FAIL_END
1621 #endif
1623 memset (enough, 'Z', sizeof (enough));
1624 wcp = L"EF";
1625 if (wcstombs (enough, wcp, 10) != 2
1626 || strcmp (enough, "EF") != 0)
1627 FAIL ();
1629 wcp = L"G";
1630 if (wcstombs (enough, wcp, l0 + 10) != 1
1631 || strcmp (enough, "G") != 0)
1632 FAIL ();
1634 #if __USE_FORTIFY_LEVEL >= 1
1635 CHK_FAIL_START
1636 char smallbuf[2];
1637 wcp = L"ABC";
1638 wcstombs (smallbuf, wcp, 10);
1639 CHK_FAIL_END
1640 #endif
1642 memset (&s, '\0', sizeof (s));
1643 wcp = L"AB";
1644 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1645 || strcmp (enough, "A") != 0)
1646 FAIL ();
1648 wcp = L"BCD";
1649 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1650 || strcmp (enough, "B") != 0)
1651 FAIL ();
1653 #if __USE_FORTIFY_LEVEL >= 1
1654 CHK_FAIL_START
1655 char smallbuf[2];
1656 wcp = L"ABC";
1657 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1658 CHK_FAIL_END
1659 #endif
1661 else
1663 puts ("cannot set locale");
1664 ret = 1;
1667 int fd;
1669 #ifdef _GNU_SOURCE
1670 fd = posix_openpt (O_RDWR);
1671 if (fd != -1)
1673 char enough[1000];
1674 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1675 FAIL ();
1677 #if __USE_FORTIFY_LEVEL >= 1
1678 CHK_FAIL_START
1679 char smallbuf[2];
1680 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1681 FAIL ();
1682 CHK_FAIL_END
1683 #endif
1684 close (fd);
1686 #endif
1688 #if PATH_MAX > 0
1689 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1690 # if __USE_FORTIFY_LEVEL >= 1
1691 CHK_FAIL_START
1692 char smallbuf[1];
1693 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1694 CHK_FAIL_END
1695 # endif
1696 #endif
1698 gid_t grpslarge[5];
1699 int ngr = getgroups (5, grpslarge);
1700 asm volatile ("" : : "r" (ngr));
1701 #if __USE_FORTIFY_LEVEL >= 1
1702 CHK_FAIL_START
1703 char smallbuf[1];
1704 ngr = getgroups (5, (gid_t *) smallbuf);
1705 asm volatile ("" : : "r" (ngr));
1706 CHK_FAIL_END
1707 #endif
1709 fd = open (_PATH_TTY, O_RDONLY);
1710 if (fd != -1)
1712 char enough[1000];
1713 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1714 FAIL ();
1716 #if __USE_FORTIFY_LEVEL >= 1
1717 CHK_FAIL_START
1718 char smallbuf[2];
1719 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1720 FAIL ();
1721 CHK_FAIL_END
1722 #endif
1723 close (fd);
1726 char hostnamelarge[1000];
1727 gethostname (hostnamelarge, sizeof (hostnamelarge));
1728 #if __USE_FORTIFY_LEVEL >= 1
1729 CHK_FAIL_START
1730 char smallbuf[1];
1731 gethostname (smallbuf, sizeof (hostnamelarge));
1732 CHK_FAIL_END
1733 #endif
1735 char loginlarge[1000];
1736 getlogin_r (loginlarge, sizeof (hostnamelarge));
1737 #if __USE_FORTIFY_LEVEL >= 1
1738 CHK_FAIL_START
1739 char smallbuf[1];
1740 getlogin_r (smallbuf, sizeof (loginlarge));
1741 CHK_FAIL_END
1742 #endif
1744 char domainnamelarge[1000];
1745 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1746 asm volatile ("" : : "r" (res));
1747 #if __USE_FORTIFY_LEVEL >= 1
1748 CHK_FAIL_START
1749 char smallbuf[1];
1750 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1751 asm volatile ("" : : "r" (res));
1752 CHK_FAIL_END
1753 #endif
1755 fd_set s;
1756 FD_ZERO (&s);
1758 FD_SET (FD_SETSIZE - 1, &s);
1759 #if __USE_FORTIFY_LEVEL >= 1
1760 CHK_FAIL_START
1761 FD_SET (FD_SETSIZE, &s);
1762 CHK_FAIL_END
1764 CHK_FAIL_START
1765 FD_SET (l0 + FD_SETSIZE, &s);
1766 CHK_FAIL_END
1767 #endif
1769 FD_CLR (FD_SETSIZE - 1, &s);
1770 #if __USE_FORTIFY_LEVEL >= 1
1771 CHK_FAIL_START
1772 FD_CLR (FD_SETSIZE, &s);
1773 CHK_FAIL_END
1775 CHK_FAIL_START
1776 FD_SET (l0 + FD_SETSIZE, &s);
1777 CHK_FAIL_END
1778 #endif
1780 FD_ISSET (FD_SETSIZE - 1, &s);
1781 #if __USE_FORTIFY_LEVEL >= 1
1782 CHK_FAIL_START
1783 FD_ISSET (FD_SETSIZE, &s);
1784 CHK_FAIL_END
1786 CHK_FAIL_START
1787 FD_ISSET (l0 + FD_SETSIZE, &s);
1788 CHK_FAIL_END
1789 #endif
1791 struct pollfd fds[1];
1792 fds[0].fd = STDOUT_FILENO;
1793 fds[0].events = POLLOUT;
1794 poll (fds, 1, 0);
1795 #if __USE_FORTIFY_LEVEL >= 1
1796 CHK_FAIL_START
1797 poll (fds, 2, 0);
1798 CHK_FAIL_END
1800 CHK_FAIL_START
1801 poll (fds, l0 + 2, 0);
1802 CHK_FAIL_END
1803 #endif
1804 #ifdef _GNU_SOURCE
1805 ppoll (fds, 1, NULL, NULL);
1806 # if __USE_FORTIFY_LEVEL >= 1
1807 CHK_FAIL_START
1808 ppoll (fds, 2, NULL, NULL);
1809 CHK_FAIL_END
1811 CHK_FAIL_START
1812 ppoll (fds, l0 + 2, NULL, NULL);
1813 CHK_FAIL_END
1814 # endif
1815 #endif
1817 return ret;
1820 #include <support/test-driver.c>