* io/Makefile (CFLAGS-fstatat.c): Set.
[glibc.git] / debug / tst-chk1.c
blobc450744af560cea4cd10737c472486e92979efa2
1 /* Copyright (C) 2004, 2005, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 /* Hack: make sure GCC doesn't know __chk_fail () will not return. */
21 #define __noreturn__
23 #include <assert.h>
24 #include <fcntl.h>
25 #include <locale.h>
26 #include <paths.h>
27 #include <setjmp.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <wchar.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
37 char *temp_filename;
38 static void do_prepare (void);
39 static int do_test (void);
40 #define PREPARE(argc, argv) do_prepare ()
41 #define TEST_FUNCTION do_test ()
42 #include "../test-skeleton.c"
44 static void
45 do_prepare (void)
47 int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
48 if (temp_fd == -1)
50 printf ("cannot create temporary file: %m\n");
51 exit (1);
54 const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
55 if (write (temp_fd, strs, strlen (strs)) != strlen (strs))
57 puts ("could not write test strings into file");
58 unlink (temp_filename);
59 exit (1);
63 volatile int chk_fail_ok;
64 volatile int ret;
65 jmp_buf chk_fail_buf;
67 static void
68 handler (int sig)
70 if (chk_fail_ok)
72 chk_fail_ok = 0;
73 longjmp (chk_fail_buf, 1);
75 else
76 _exit (127);
79 char buf[10];
80 wchar_t wbuf[10];
81 volatile size_t l0;
82 volatile char *p;
83 volatile wchar_t *wp;
84 const char *str1 = "JIHGFEDCBA";
85 const char *str2 = "F";
86 const char *str3 = "%s%n%s%n";
87 const char *str4 = "Hello, ";
88 const char *str5 = "World!\n";
89 const wchar_t *wstr1 = L"JIHGFEDCBA";
90 const wchar_t *wstr2 = L"F";
91 const wchar_t *wstr3 = L"%s%n%s%n";
92 const wchar_t *wstr4 = L"Hello, ";
93 const wchar_t *wstr5 = L"World!\n";
94 char buf2[10] = "%s";
95 int num1 = 67;
96 int num2 = 987654;
98 #define FAIL() \
99 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
100 #define CHK_FAIL_START \
101 chk_fail_ok = 1; \
102 if (! setjmp (chk_fail_buf)) \
104 #define CHK_FAIL_END \
105 chk_fail_ok = 0; \
106 FAIL (); \
108 #if __USE_FORTIFY_LEVEL >= 2
109 #define CHK_FAIL2_START CHK_FAIL_START
110 #define CHK_FAIL2_END CHK_FAIL_END
111 #else
112 #define CHK_FAIL2_START
113 #define CHK_FAIL2_END
114 #endif
116 static int
117 do_test (void)
119 struct sigaction sa;
120 sa.sa_handler = handler;
121 sa.sa_flags = 0;
122 sigemptyset (&sa.sa_mask);
124 sigaction (SIGABRT, &sa, NULL);
126 /* Avoid all the buffer overflow messages on stderr. */
127 int fd = open (_PATH_DEVNULL, O_WRONLY);
128 if (fd == -1)
129 close (STDERR_FILENO);
130 else
132 dup2 (fd, STDERR_FILENO);
133 close (fd);
135 setenv ("LIBC_FATAL_STDERR_", "1", 1);
137 struct A { char buf1[9]; char buf2[1]; } a;
138 struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
140 printf ("Test checking routines at fortify level %d\n",
141 #ifdef __USE_FORTIFY_LEVEL
142 (int) __USE_FORTIFY_LEVEL
143 #else
145 #endif
148 /* These ops can be done without runtime checking of object size. */
149 memcpy (buf, "abcdefghij", 10);
150 memmove (buf + 1, buf, 9);
151 if (memcmp (buf, "aabcdefghi", 10))
152 FAIL ();
154 if (mempcpy (buf + 5, "abcde", 5) != buf + 10
155 || memcmp (buf, "aabcdabcde", 10))
156 FAIL ();
158 memset (buf + 8, 'j', 2);
159 if (memcmp (buf, "aabcdabcjj", 10))
160 FAIL ();
162 strcpy (buf + 4, "EDCBA");
163 if (memcmp (buf, "aabcEDCBA", 10))
164 FAIL ();
166 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
167 FAIL ();
169 strncpy (buf + 6, "X", 4);
170 if (memcmp (buf, "aabcEDX\0\0", 10))
171 FAIL ();
173 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
174 FAIL ();
176 if (snprintf (buf + 7, 3, "%s", "987654") != 6
177 || memcmp (buf, "aabcEDX98", 10))
178 FAIL ();
180 /* These ops need runtime checking, but shouldn't __chk_fail. */
181 memcpy (buf, "abcdefghij", l0 + 10);
182 memmove (buf + 1, buf, l0 + 9);
183 if (memcmp (buf, "aabcdefghi", 10))
184 FAIL ();
186 if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
187 || memcmp (buf, "aabcdabcde", 10))
188 FAIL ();
190 memset (buf + 8, 'j', l0 + 2);
191 if (memcmp (buf, "aabcdabcjj", 10))
192 FAIL ();
194 strcpy (buf + 4, str1 + 5);
195 if (memcmp (buf, "aabcEDCBA", 10))
196 FAIL ();
198 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
199 FAIL ();
201 strncpy (buf + 6, "X", l0 + 4);
202 if (memcmp (buf, "aabcEDX\0\0", 10))
203 FAIL ();
205 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
206 || memcmp (buf, "aabcEcd\0\0", 10))
207 FAIL ();
209 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
210 FAIL ();
212 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
213 FAIL ();
215 buf[l0 + 8] = '\0';
216 strcat (buf, "A");
217 if (memcmp (buf, "aabcEcd9A", 10))
218 FAIL ();
220 buf[l0 + 7] = '\0';
221 strncat (buf, "ZYXWV", l0 + 2);
222 if (memcmp (buf, "aabcEcdZY", 10))
223 FAIL ();
225 memcpy (a.buf1, "abcdefghij", l0 + 10);
226 memmove (a.buf1 + 1, a.buf1, l0 + 9);
227 if (memcmp (a.buf1, "aabcdefghi", 10))
228 FAIL ();
230 if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
231 || memcmp (a.buf1, "aabcdabcde", 10))
232 FAIL ();
234 memset (a.buf1 + 8, 'j', l0 + 2);
235 if (memcmp (a.buf1, "aabcdabcjj", 10))
236 FAIL ();
238 #if __USE_FORTIFY_LEVEL < 2 || !__GNUC_PREREQ (4, 0)
239 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
240 and sufficient GCC support, as the string operations overflow
241 from a.buf1 into a.buf2. */
242 strcpy (a.buf1 + 4, str1 + 5);
243 if (memcmp (a.buf1, "aabcEDCBA", 10))
244 FAIL ();
246 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
247 || memcmp (a.buf1, "aabcEDCBF", 10))
248 FAIL ();
250 strncpy (a.buf1 + 6, "X", l0 + 4);
251 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
252 FAIL ();
254 if (sprintf (a.buf1 + 7, "%d", num1) != 2
255 || memcmp (a.buf1, "aabcEDX67", 10))
256 FAIL ();
258 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
259 || memcmp (a.buf1, "aabcEDX98", 10))
260 FAIL ();
262 a.buf1[l0 + 8] = '\0';
263 strcat (a.buf1, "A");
264 if (memcmp (a.buf1, "aabcEDX9A", 10))
265 FAIL ();
267 a.buf1[l0 + 7] = '\0';
268 strncat (a.buf1, "ZYXWV", l0 + 2);
269 if (memcmp (a.buf1, "aabcEDXZY", 10))
270 FAIL ();
272 #endif
274 #if __USE_FORTIFY_LEVEL >= 1
275 /* Now check if all buffer overflows are caught at runtime. */
277 CHK_FAIL_START
278 memcpy (buf + 1, "abcdefghij", l0 + 10);
279 CHK_FAIL_END
281 CHK_FAIL_START
282 memmove (buf + 2, buf + 1, l0 + 9);
283 CHK_FAIL_END
285 CHK_FAIL_START
286 p = mempcpy (buf + 6, "abcde", l0 + 5);
287 CHK_FAIL_END
289 CHK_FAIL_START
290 memset (buf + 9, 'j', l0 + 2);
291 CHK_FAIL_END
293 CHK_FAIL_START
294 strcpy (buf + 5, str1 + 5);
295 CHK_FAIL_END
297 CHK_FAIL_START
298 p = stpcpy (buf + 9, str2);
299 CHK_FAIL_END
301 CHK_FAIL_START
302 strncpy (buf + 7, "X", l0 + 4);
303 CHK_FAIL_END
305 CHK_FAIL_START
306 stpncpy (buf + 6, "cd", l0 + 5);
307 CHK_FAIL_END
309 CHK_FAIL_START
310 sprintf (buf + 8, "%d", num1);
311 CHK_FAIL_END
313 CHK_FAIL_START
314 snprintf (buf + 8, l0 + 3, "%d", num2);
315 CHK_FAIL_END
317 memcpy (buf, str1 + 2, l0 + 9);
318 CHK_FAIL_START
319 strcat (buf, "AB");
320 CHK_FAIL_END
322 memcpy (buf, str1 + 3, l0 + 8);
323 CHK_FAIL_START
324 strncat (buf, "ZYXWV", l0 + 3);
325 CHK_FAIL_END
327 CHK_FAIL_START
328 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
329 CHK_FAIL_END
331 CHK_FAIL_START
332 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
333 CHK_FAIL_END
335 CHK_FAIL_START
336 p = mempcpy (a.buf1 + 6, "abcde", l0 + 5);
337 CHK_FAIL_END
339 CHK_FAIL_START
340 memset (a.buf1 + 9, 'j', l0 + 2);
341 CHK_FAIL_END
343 #if __USE_FORTIFY_LEVEL >= 2 && __GNUC_PREREQ (4, 0)
344 # define O 0
345 #else
346 # define O 1
347 #endif
349 CHK_FAIL_START
350 strcpy (a.buf1 + (O + 4), str1 + 5);
351 CHK_FAIL_END
353 CHK_FAIL_START
354 p = stpcpy (a.buf1 + (O + 8), str2);
355 CHK_FAIL_END
357 CHK_FAIL_START
358 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
359 CHK_FAIL_END
361 CHK_FAIL_START
362 sprintf (a.buf1 + (O + 7), "%d", num1);
363 CHK_FAIL_END
365 CHK_FAIL_START
366 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
367 CHK_FAIL_END
369 memcpy (a.buf1, str1 + (3 - O), l0 + 8 + O);
370 CHK_FAIL_START
371 strcat (a.buf1, "AB");
372 CHK_FAIL_END
374 memcpy (a.buf1, str1 + (4 - O), l0 + 7 + O);
375 CHK_FAIL_START
376 strncat (a.buf1, "ZYXWV", l0 + 3);
377 CHK_FAIL_END
378 #endif
381 /* These ops can be done without runtime checking of object size. */
382 wmemcpy (wbuf, L"abcdefghij", 10);
383 wmemmove (wbuf + 1, wbuf, 9);
384 if (wmemcmp (wbuf, L"aabcdefghi", 10))
385 FAIL ();
387 if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
388 || wmemcmp (wbuf, L"aabcdabcde", 10))
389 FAIL ();
391 wmemset (wbuf + 8, L'j', 2);
392 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
393 FAIL ();
395 wcscpy (wbuf + 4, L"EDCBA");
396 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
397 FAIL ();
399 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
400 FAIL ();
402 wcsncpy (wbuf + 6, L"X", 4);
403 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
404 FAIL ();
406 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
407 || wmemcmp (wbuf, L"aabcEDX98", 10))
408 FAIL ();
410 if (swprintf (wbuf + 7, 3, L"64") != 2
411 || wmemcmp (wbuf, L"aabcEDX64", 10))
412 FAIL ();
414 /* These ops need runtime checking, but shouldn't __chk_fail. */
415 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
416 wmemmove (wbuf + 1, wbuf, l0 + 9);
417 if (wmemcmp (wbuf, L"aabcdefghi", 10))
418 FAIL ();
420 if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
421 || wmemcmp (wbuf, L"aabcdabcde", 10))
422 FAIL ();
424 wmemset (wbuf + 8, L'j', l0 + 2);
425 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
426 FAIL ();
428 wcscpy (wbuf + 4, wstr1 + 5);
429 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
430 FAIL ();
432 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
433 FAIL ();
435 wcsncpy (wbuf + 6, L"X", l0 + 4);
436 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
437 FAIL ();
439 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
440 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
441 FAIL ();
443 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
444 || wmemcmp (wbuf, L"aabcEcd98", 10))
445 FAIL ();
447 wbuf[l0 + 8] = L'\0';
448 wcscat (wbuf, L"A");
449 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
450 FAIL ();
452 wbuf[l0 + 7] = L'\0';
453 wcsncat (wbuf, L"ZYXWV", l0 + 2);
454 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
455 FAIL ();
457 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
458 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
459 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
460 FAIL ();
462 if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
463 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
464 FAIL ();
466 wmemset (wa.buf1 + 8, L'j', l0 + 2);
467 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
468 FAIL ();
470 #if __USE_FORTIFY_LEVEL < 2
471 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
472 and sufficient GCC support, as the string operations overflow
473 from a.buf1 into a.buf2. */
474 wcscpy (wa.buf1 + 4, wstr1 + 5);
475 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
476 FAIL ();
478 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
479 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
480 FAIL ();
482 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
483 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
484 FAIL ();
486 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
487 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
488 FAIL ();
490 wa.buf1[l0 + 8] = L'\0';
491 wcscat (wa.buf1, L"A");
492 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
493 FAIL ();
495 wa.buf1[l0 + 7] = L'\0';
496 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
497 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
498 FAIL ();
500 #endif
502 #if __USE_FORTIFY_LEVEL >= 1
503 /* Now check if all buffer overflows are caught at runtime. */
505 CHK_FAIL_START
506 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
507 CHK_FAIL_END
509 CHK_FAIL_START
510 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
511 CHK_FAIL_END
513 CHK_FAIL_START
514 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
515 CHK_FAIL_END
517 CHK_FAIL_START
518 wmemset (wbuf + 9, L'j', l0 + 2);
519 CHK_FAIL_END
521 CHK_FAIL_START
522 wcscpy (wbuf + 5, wstr1 + 5);
523 CHK_FAIL_END
525 CHK_FAIL_START
526 wp = wcpcpy (wbuf + 9, wstr2);
527 CHK_FAIL_END
529 CHK_FAIL_START
530 wcsncpy (wbuf + 7, L"X", l0 + 4);
531 CHK_FAIL_END
533 CHK_FAIL_START
534 wcpncpy (wbuf + 6, L"cd", l0 + 5);
535 CHK_FAIL_END
537 wmemcpy (wbuf, wstr1 + 2, l0 + 9);
538 CHK_FAIL_START
539 wcscat (wbuf, L"AB");
540 CHK_FAIL_END
542 wmemcpy (wbuf, wstr1 + 3, l0 + 8);
543 CHK_FAIL_START
544 wcsncat (wbuf, L"ZYXWV", l0 + 3);
545 CHK_FAIL_END
547 CHK_FAIL_START
548 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
549 CHK_FAIL_END
551 CHK_FAIL_START
552 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
553 CHK_FAIL_END
555 CHK_FAIL_START
556 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
557 CHK_FAIL_END
559 CHK_FAIL_START
560 wmemset (wa.buf1 + 9, L'j', l0 + 2);
561 CHK_FAIL_END
563 #if __USE_FORTIFY_LEVEL >= 2
564 # define O 0
565 #else
566 # define O 1
567 #endif
569 CHK_FAIL_START
570 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
571 CHK_FAIL_END
573 CHK_FAIL_START
574 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
575 CHK_FAIL_END
577 CHK_FAIL_START
578 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
579 CHK_FAIL_END
581 wmemcpy (wa.buf1, wstr1 + (3 - O), l0 + 8 + O);
582 CHK_FAIL_START
583 wcscat (wa.buf1, L"AB");
584 CHK_FAIL_END
586 wmemcpy (wa.buf1, wstr1 + (4 - O), l0 + 7 + O);
587 CHK_FAIL_START
588 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
589 CHK_FAIL_END
590 #endif
593 /* Now checks for %n protection. */
595 /* Constant literals passed directly are always ok
596 (even with warnings about possible bugs from GCC). */
597 int n1, n2;
598 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
599 || n1 != 1 || n2 != 2)
600 FAIL ();
602 /* In this case the format string is not known at compile time,
603 but resides in read-only memory, so is ok. */
604 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
605 || n1 != 1 || n2 != 2)
606 FAIL ();
608 strcpy (buf2 + 2, "%n%s%n");
609 /* When the format string is writable and contains %n,
610 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
611 CHK_FAIL2_START
612 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
613 FAIL ();
614 CHK_FAIL2_END
616 CHK_FAIL2_START
617 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
618 FAIL ();
619 CHK_FAIL2_END
621 /* But if there is no %n, even writable format string
622 should work. */
623 buf2[6] = '\0';
624 if (sprintf (buf, buf2 + 4, str2) != 1)
625 FAIL ();
627 /* Constant literals passed directly are always ok
628 (even with warnings about possible bugs from GCC). */
629 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
630 || n1 != 7 || n2 != 14)
631 FAIL ();
633 /* In this case the format string is not known at compile time,
634 but resides in read-only memory, so is ok. */
635 if (printf (str3, str4, &n1, str5, &n2) != 14
636 || n1 != 7 || n2 != 14)
637 FAIL ();
639 strcpy (buf2 + 2, "%n%s%n");
640 /* When the format string is writable and contains %n,
641 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
642 CHK_FAIL2_START
643 if (printf (buf2, str4, &n1, str5, &n1) != 14)
644 FAIL ();
645 CHK_FAIL2_END
647 /* But if there is no %n, even writable format string
648 should work. */
649 buf2[6] = '\0';
650 if (printf (buf2 + 4, str5) != 7)
651 FAIL ();
653 FILE *fp = stdout;
655 /* Constant literals passed directly are always ok
656 (even with warnings about possible bugs from GCC). */
657 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
658 || n1 != 7 || n2 != 14)
659 FAIL ();
661 /* In this case the format string is not known at compile time,
662 but resides in read-only memory, so is ok. */
663 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
664 || n1 != 7 || n2 != 14)
665 FAIL ();
667 strcpy (buf2 + 2, "%n%s%n");
668 /* When the format string is writable and contains %n,
669 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
670 CHK_FAIL2_START
671 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
672 FAIL ();
673 CHK_FAIL2_END
675 /* But if there is no %n, even writable format string
676 should work. */
677 buf2[6] = '\0';
678 if (fprintf (fp, buf2 + 4, str5) != 7)
679 FAIL ();
681 if (freopen (temp_filename, "r", stdin) == NULL)
683 puts ("could not open temporary file");
684 exit (1);
687 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
688 FAIL ();
689 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
690 FAIL ();
692 #if __USE_FORTIFY_LEVEL >= 1
693 CHK_FAIL_START
694 if (gets (buf) != buf)
695 FAIL ();
696 CHK_FAIL_END
697 #endif
699 rewind (stdin);
701 if (fgets (buf, sizeof (buf), stdin) != buf
702 || memcmp (buf, "abcdefgh\n", 10))
703 FAIL ();
704 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
705 FAIL ();
707 rewind (stdin);
709 if (fgets (buf, l0 + sizeof (buf), stdin) != buf
710 || memcmp (buf, "abcdefgh\n", 10))
711 FAIL ();
713 #if __USE_FORTIFY_LEVEL >= 1
714 CHK_FAIL_START
715 if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
716 FAIL ();
717 CHK_FAIL_END
719 CHK_FAIL_START
720 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
721 FAIL ();
722 CHK_FAIL_END
723 #endif
725 rewind (stdin);
727 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
728 || memcmp (buf, "abcdefgh\n", 10))
729 FAIL ();
730 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
731 || memcmp (buf, "ABCDEFGHI", 10))
732 FAIL ();
734 rewind (stdin);
736 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
737 || memcmp (buf, "abcdefgh\n", 10))
738 FAIL ();
740 #if __USE_FORTIFY_LEVEL >= 1
741 CHK_FAIL_START
742 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
743 FAIL ();
744 CHK_FAIL_END
746 CHK_FAIL_START
747 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
748 FAIL ();
749 CHK_FAIL_END
750 #endif
752 lseek (fileno (stdin), 0, SEEK_SET);
754 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
755 || memcmp (buf, "abcdefgh\n", 9))
756 FAIL ();
757 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
758 || memcmp (buf, "ABCDEFGHI", 9))
759 FAIL ();
761 lseek (fileno (stdin), 0, SEEK_SET);
763 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
764 || memcmp (buf, "abcdefgh\n", 9))
765 FAIL ();
767 #if __USE_FORTIFY_LEVEL >= 1
768 CHK_FAIL_START
769 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
770 FAIL ();
771 CHK_FAIL_END
772 #endif
774 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
775 != sizeof (buf) - 1
776 || memcmp (buf, "\nABCDEFGH", 9))
777 FAIL ();
778 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
779 || memcmp (buf, "abcdefgh\n", 9))
780 FAIL ();
781 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
782 != sizeof (buf) - 1
783 || memcmp (buf, "h\nABCDEFG", 9))
784 FAIL ();
786 #if __USE_FORTIFY_LEVEL >= 1
787 CHK_FAIL_START
788 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
789 != sizeof (buf) + 1)
790 FAIL ();
791 CHK_FAIL_END
792 #endif
794 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
795 != sizeof (buf) - 1
796 || memcmp (buf, "\nABCDEFGH", 9))
797 FAIL ();
798 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
799 || memcmp (buf, "abcdefgh\n", 9))
800 FAIL ();
801 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
802 != sizeof (buf) - 1
803 || memcmp (buf, "h\nABCDEFG", 9))
804 FAIL ();
806 #if __USE_FORTIFY_LEVEL >= 1
807 CHK_FAIL_START
808 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
809 != sizeof (buf) + 1)
810 FAIL ();
811 CHK_FAIL_END
812 #endif
814 if (freopen (temp_filename, "r", stdin) == NULL)
816 puts ("could not open temporary file");
817 exit (1);
820 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
822 puts ("could not seek in test file");
823 exit (1);
826 #if __USE_FORTIFY_LEVEL >= 1
827 CHK_FAIL_START
828 if (gets (buf) != buf)
829 FAIL ();
830 CHK_FAIL_END
831 #endif
833 /* Check whether missing N$ formats are detected. */
834 CHK_FAIL2_START
835 printf ("%3$d\n", 1, 2, 3, 4);
836 CHK_FAIL2_END
838 CHK_FAIL2_START
839 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
840 CHK_FAIL2_END
842 CHK_FAIL2_START
843 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
844 CHK_FAIL2_END
846 CHK_FAIL2_START
847 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
848 CHK_FAIL2_END
850 int sp[2];
851 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
852 FAIL ();
853 else
855 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
856 if (send (sp[0], sendstr, strlen (sendstr), 0) != strlen (sendstr))
857 FAIL ();
859 char recvbuf[12];
860 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
861 != sizeof recvbuf
862 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
863 FAIL ();
865 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
866 != sizeof recvbuf - 7
867 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
868 FAIL ();
870 #if __USE_FORTIFY_LEVEL >= 1
871 CHK_FAIL_START
872 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
873 != sizeof recvbuf)
874 FAIL ();
875 CHK_FAIL_END
877 CHK_FAIL_START
878 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
879 != sizeof recvbuf - 3)
880 FAIL ();
881 CHK_FAIL_END
882 #endif
884 socklen_t sl;
885 struct sockaddr_un sa_un;
887 sl = sizeof (sa_un);
888 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK, &sa_un, &sl)
889 != sizeof recvbuf
890 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
891 FAIL ();
893 sl = sizeof (sa_un);
894 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
895 &sa_un, &sl) != sizeof recvbuf - 7
896 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
897 FAIL ();
899 #if __USE_FORTIFY_LEVEL >= 1
900 CHK_FAIL_START
901 sl = sizeof (sa_un);
902 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK, &sa_un, &sl)
903 != sizeof recvbuf)
904 FAIL ();
905 CHK_FAIL_END
907 CHK_FAIL_START
908 sl = sizeof (sa_un);
909 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
910 &sa_un, &sl) != sizeof recvbuf - 3)
911 FAIL ();
912 CHK_FAIL_END
913 #endif
915 close (sp[0]);
916 close (sp[1]);
919 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
920 char *enddir = strchr (fname, '\0');
921 if (mkdtemp (fname) == NULL)
923 printf ("mkdtemp failed: %m\n");
924 return 1;
926 *enddir = '/';
927 if (symlink ("bar", fname) != 0)
928 FAIL ();
930 char readlinkbuf[4];
931 if (readlink (fname, readlinkbuf, 4) != 3
932 || memcmp (readlinkbuf, "bar", 3) != 0)
933 FAIL ();
934 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
935 || memcmp (readlinkbuf, "bbar", 4) != 0)
936 FAIL ();
938 #if __USE_FORTIFY_LEVEL >= 1
939 CHK_FAIL_START
940 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
941 FAIL ();
942 CHK_FAIL_END
944 CHK_FAIL_START
945 if (readlink (fname, readlinkbuf + 3, 4) != 3)
946 FAIL ();
947 CHK_FAIL_END
948 #endif
950 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
951 if (tmpfd < 0)
952 FAIL ();
954 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
955 || memcmp (readlinkbuf, "bar", 3) != 0)
956 FAIL ();
957 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
958 l0 + 3) != 3
959 || memcmp (readlinkbuf, "bbar", 4) != 0)
960 FAIL ();
962 #if __USE_FORTIFY_LEVEL >= 1
963 CHK_FAIL_START
964 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
965 l0 + 3) != 3)
966 FAIL ();
967 CHK_FAIL_END
969 CHK_FAIL_START
970 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
971 4) != 3)
972 FAIL ();
973 CHK_FAIL_END
974 #endif
976 close (tmpfd);
978 char *cwd1 = getcwd (NULL, 0);
979 if (cwd1 == NULL)
980 FAIL ();
982 char *cwd2 = getcwd (NULL, 250);
983 if (cwd2 == NULL)
984 FAIL ();
986 if (cwd1 && cwd2)
988 if (strcmp (cwd1, cwd2) != 0)
989 FAIL ();
991 *enddir = '\0';
992 if (chdir (fname))
993 FAIL ();
995 char *cwd3 = getcwd (NULL, 0);
996 if (cwd3 == NULL)
997 FAIL ();
998 if (strcmp (fname, cwd3) != 0)
999 printf ("getcwd after chdir is '%s' != '%s',"
1000 "get{c,}wd tests skipped\n", cwd3, fname);
1001 else
1003 char getcwdbuf[sizeof fname - 3];
1005 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1006 if (cwd4 != getcwdbuf
1007 || strcmp (getcwdbuf, fname) != 0)
1008 FAIL ();
1010 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1011 if (cwd4 != getcwdbuf + 1
1012 || getcwdbuf[0] != fname[0]
1013 || strcmp (getcwdbuf + 1, fname) != 0)
1014 FAIL ();
1016 #if __USE_FORTIFY_LEVEL >= 1
1017 CHK_FAIL_START
1018 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1019 != getcwdbuf + 2)
1020 FAIL ();
1021 CHK_FAIL_END
1023 CHK_FAIL_START
1024 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1025 != getcwdbuf + 2)
1026 FAIL ();
1027 CHK_FAIL_END
1028 #endif
1030 if (getwd (getcwdbuf) != getcwdbuf
1031 || strcmp (getcwdbuf, fname) != 0)
1032 FAIL ();
1034 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1035 || strcmp (getcwdbuf + 1, fname) != 0)
1036 FAIL ();
1038 #if __USE_FORTIFY_LEVEL >= 1
1039 CHK_FAIL_START
1040 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1041 FAIL ();
1042 CHK_FAIL_END
1043 #endif
1046 if (chdir (cwd1) != 0)
1047 FAIL ();
1048 free (cwd3);
1051 free (cwd1);
1052 free (cwd2);
1053 *enddir = '/';
1054 if (unlink (fname) != 0)
1055 FAIL ();
1057 *enddir = '\0';
1058 if (rmdir (fname) != 0)
1059 FAIL ();
1062 #if PATH_MAX > 0
1063 char largebuf[PATH_MAX];
1064 char *realres = realpath (".", largebuf);
1065 if (realres != largebuf)
1066 FAIL ();
1068 # if __USE_FORTIFY_LEVEL >= 1
1069 CHK_FAIL_START
1070 char realbuf[1];
1071 realres = realpath (".", realbuf);
1072 if (realres != realbuf)
1073 FAIL ();
1074 CHK_FAIL_END
1075 # endif
1076 #endif
1078 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1080 assert (MB_CUR_MAX <= 10);
1082 /* First a simple test. */
1083 char enough[10];
1084 if (wctomb (enough, L'A') != 1)
1085 FAIL ();
1087 #if __USE_FORTIFY_LEVEL >= 1
1088 /* We know the wchar_t encoding is ISO 10646. So pick a
1089 character which has a multibyte representation which does not
1090 fit. */
1091 CHK_FAIL_START
1092 char smallbuf[2];
1093 if (wctomb (smallbuf, L'\x100') != 2)
1094 FAIL ();
1095 CHK_FAIL_END
1096 #endif
1098 mbstate_t s;
1099 memset (&s, '\0', sizeof (s));
1100 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1101 FAIL ();
1103 #if __USE_FORTIFY_LEVEL >= 1
1104 /* We know the wchar_t encoding is ISO 10646. So pick a
1105 character which has a multibyte representation which does not
1106 fit. */
1107 CHK_FAIL_START
1108 char smallbuf[2];
1109 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1110 FAIL ();
1111 CHK_FAIL_END
1112 #endif
1114 wchar_t wenough[10];
1115 memset (&s, '\0', sizeof (s));
1116 const char *cp = "A";
1117 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1118 || wcscmp (wenough, L"A") != 0)
1119 FAIL ();
1121 cp = "BC";
1122 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1123 || wcscmp (wenough, L"BC") != 0)
1124 FAIL ();
1126 #if __USE_FORTIFY_LEVEL >= 1
1127 CHK_FAIL_START
1128 wchar_t wsmallbuf[2];
1129 cp = "ABC";
1130 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1131 CHK_FAIL_END
1132 #endif
1134 cp = "A";
1135 if (mbstowcs (wenough, cp, 10) != 1
1136 || wcscmp (wenough, L"A") != 0)
1137 FAIL ();
1139 cp = "DEF";
1140 if (mbstowcs (wenough, cp, l0 + 10) != 3
1141 || wcscmp (wenough, L"DEF") != 0)
1142 FAIL ();
1144 #if __USE_FORTIFY_LEVEL >= 1
1145 CHK_FAIL_START
1146 wchar_t wsmallbuf[2];
1147 cp = "ABC";
1148 mbstowcs (wsmallbuf, cp, 10);
1149 CHK_FAIL_END
1150 #endif
1152 memset (&s, '\0', sizeof (s));
1153 cp = "ABC";
1154 wcscpy (wenough, L"DEF");
1155 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1156 || wcscmp (wenough, L"AEF") != 0)
1157 FAIL ();
1159 cp = "IJ";
1160 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1161 || wcscmp (wenough, L"IEF") != 0)
1162 FAIL ();
1164 #if __USE_FORTIFY_LEVEL >= 1
1165 CHK_FAIL_START
1166 wchar_t wsmallbuf[2];
1167 cp = "ABC";
1168 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1169 CHK_FAIL_END
1170 #endif
1172 memset (&s, '\0', sizeof (s));
1173 const wchar_t *wcp = L"A";
1174 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1175 || strcmp (enough, "A") != 0)
1176 FAIL ();
1178 wcp = L"BC";
1179 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1180 || strcmp (enough, "BC") != 0)
1181 FAIL ();
1183 #if __USE_FORTIFY_LEVEL >= 1
1184 CHK_FAIL_START
1185 char smallbuf[2];
1186 wcp = L"ABC";
1187 wcsrtombs (smallbuf, &wcp, 10, &s);
1188 CHK_FAIL_END
1189 #endif
1191 memset (enough, 'Z', sizeof (enough));
1192 wcp = L"EF";
1193 if (wcstombs (enough, wcp, 10) != 2
1194 || strcmp (enough, "EF") != 0)
1195 FAIL ();
1197 wcp = L"G";
1198 if (wcstombs (enough, wcp, l0 + 10) != 1
1199 || strcmp (enough, "G") != 0)
1200 FAIL ();
1202 #if __USE_FORTIFY_LEVEL >= 1
1203 CHK_FAIL_START
1204 char smallbuf[2];
1205 wcp = L"ABC";
1206 wcstombs (smallbuf, wcp, 10);
1207 CHK_FAIL_END
1208 #endif
1210 memset (&s, '\0', sizeof (s));
1211 wcp = L"AB";
1212 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1213 || strcmp (enough, "A") != 0)
1214 FAIL ();
1216 wcp = L"BCD";
1217 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1218 || strcmp (enough, "B") != 0)
1219 FAIL ();
1221 #if __USE_FORTIFY_LEVEL >= 1
1222 CHK_FAIL_START
1223 char smallbuf[2];
1224 wcp = L"ABC";
1225 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1226 CHK_FAIL_END
1227 #endif
1229 else
1231 puts ("cannot set locale");
1232 ret = 1;
1235 fd = posix_openpt (O_RDWR);
1236 if (fd != -1)
1238 char enough[1000];
1239 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1240 FAIL ();
1242 #if __USE_FORTIFY_LEVEL >= 1
1243 CHK_FAIL_START
1244 char smallbuf[2];
1245 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1246 FAIL ();
1247 CHK_FAIL_END
1248 #endif
1249 close (fd);
1252 #if PATH_MAX > 0
1253 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1254 # if __USE_FORTIFY_LEVEL >= 1
1255 CHK_FAIL_START
1256 char smallbuf[1];
1257 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1258 CHK_FAIL_END
1259 # endif
1260 #endif
1262 gid_t grpslarge[5];
1263 int ngr = getgroups (5, grpslarge);
1264 asm volatile ("" : : "r" (ngr));
1265 #if __USE_FORTIFY_LEVEL >= 1
1266 CHK_FAIL_START
1267 char smallbuf[1];
1268 ngr = getgroups (5, (gid_t *) smallbuf);
1269 asm volatile ("" : : "r" (ngr));
1270 CHK_FAIL_END
1271 #endif
1273 fd = open (_PATH_TTY, O_RDONLY);
1274 if (fd != -1)
1276 char enough[1000];
1277 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1278 FAIL ();
1280 #if __USE_FORTIFY_LEVEL >= 1
1281 CHK_FAIL_START
1282 char smallbuf[2];
1283 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1284 FAIL ();
1285 CHK_FAIL_END
1286 #endif
1287 close (fd);
1290 char hostnamelarge[1000];
1291 gethostname (hostnamelarge, sizeof (hostnamelarge));
1292 #if __USE_FORTIFY_LEVEL >= 1
1293 CHK_FAIL_START
1294 char smallbuf[1];
1295 gethostname (smallbuf, sizeof (hostnamelarge));
1296 CHK_FAIL_END
1297 #endif
1299 char loginlarge[1000];
1300 getlogin_r (loginlarge, sizeof (hostnamelarge));
1301 #if __USE_FORTIFY_LEVEL >= 1
1302 CHK_FAIL_START
1303 char smallbuf[1];
1304 getlogin_r (smallbuf, sizeof (loginlarge));
1305 CHK_FAIL_END
1306 #endif
1308 char domainnamelarge[1000];
1309 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1310 asm volatile ("" : : "r" (res));
1311 #if __USE_FORTIFY_LEVEL >= 1
1312 CHK_FAIL_START
1313 char smallbuf[1];
1314 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1315 asm volatile ("" : : "r" (res));
1316 CHK_FAIL_END
1317 #endif
1319 return ret;