* locales/en_US: Add first_weekday and first_workday.
[glibc.git] / debug / tst-chk1.c
blob487b0710265a437f912db546aff28e5620ee891f
1 /* Copyright (C) 2004, 2005, 2006, 2007 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 #include <assert.h>
21 #include <fcntl.h>
22 #include <locale.h>
23 #include <paths.h>
24 #include <setjmp.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <wchar.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
34 char *temp_filename;
35 static void do_prepare (void);
36 static int do_test (void);
37 #define PREPARE(argc, argv) do_prepare ()
38 #define TEST_FUNCTION do_test ()
39 #include "../test-skeleton.c"
41 static void
42 do_prepare (void)
44 int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
45 if (temp_fd == -1)
47 printf ("cannot create temporary file: %m\n");
48 exit (1);
51 const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
52 if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
54 puts ("could not write test strings into file");
55 unlink (temp_filename);
56 exit (1);
60 volatile int chk_fail_ok;
61 volatile int ret;
62 jmp_buf chk_fail_buf;
64 static void
65 handler (int sig)
67 if (chk_fail_ok)
69 chk_fail_ok = 0;
70 longjmp (chk_fail_buf, 1);
72 else
73 _exit (127);
76 char buf[10];
77 wchar_t wbuf[10];
78 volatile size_t l0;
79 volatile char *p;
80 volatile wchar_t *wp;
81 const char *str1 = "JIHGFEDCBA";
82 const char *str2 = "F";
83 const char *str3 = "%s%n%s%n";
84 const char *str4 = "Hello, ";
85 const char *str5 = "World!\n";
86 const wchar_t *wstr1 = L"JIHGFEDCBA";
87 const wchar_t *wstr2 = L"F";
88 const wchar_t *wstr3 = L"%s%n%s%n";
89 const wchar_t *wstr4 = L"Hello, ";
90 const wchar_t *wstr5 = L"World!\n";
91 char buf2[10] = "%s";
92 int num1 = 67;
93 int num2 = 987654;
95 #define FAIL() \
96 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
97 #define CHK_FAIL_START \
98 chk_fail_ok = 1; \
99 if (! setjmp (chk_fail_buf)) \
101 #define CHK_FAIL_END \
102 chk_fail_ok = 0; \
103 FAIL (); \
105 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
106 #define CHK_FAIL2_START CHK_FAIL_START
107 #define CHK_FAIL2_END CHK_FAIL_END
108 #else
109 #define CHK_FAIL2_START
110 #define CHK_FAIL2_END
111 #endif
113 static int
114 do_test (void)
116 struct sigaction sa;
117 sa.sa_handler = handler;
118 sa.sa_flags = 0;
119 sigemptyset (&sa.sa_mask);
121 sigaction (SIGABRT, &sa, NULL);
123 /* Avoid all the buffer overflow messages on stderr. */
124 int fd = open (_PATH_DEVNULL, O_WRONLY);
125 if (fd == -1)
126 close (STDERR_FILENO);
127 else
129 dup2 (fd, STDERR_FILENO);
130 close (fd);
132 setenv ("LIBC_FATAL_STDERR_", "1", 1);
134 struct A { char buf1[9]; char buf2[1]; } a;
135 struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
137 printf ("Test checking routines at fortify level %d\n",
138 #ifdef __USE_FORTIFY_LEVEL
139 (int) __USE_FORTIFY_LEVEL
140 #else
142 #endif
145 #if defined __USE_FORTIFY_LEVEL && !defined __extern_always_inline
146 printf ("Test skipped");
147 if (l0 == 0)
148 return 0;
149 #endif
151 /* These ops can be done without runtime checking of object size. */
152 memcpy (buf, "abcdefghij", 10);
153 memmove (buf + 1, buf, 9);
154 if (memcmp (buf, "aabcdefghi", 10))
155 FAIL ();
157 if (mempcpy (buf + 5, "abcde", 5) != buf + 10
158 || memcmp (buf, "aabcdabcde", 10))
159 FAIL ();
161 memset (buf + 8, 'j', 2);
162 if (memcmp (buf, "aabcdabcjj", 10))
163 FAIL ();
165 strcpy (buf + 4, "EDCBA");
166 if (memcmp (buf, "aabcEDCBA", 10))
167 FAIL ();
169 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
170 FAIL ();
172 strncpy (buf + 6, "X", 4);
173 if (memcmp (buf, "aabcEDX\0\0", 10))
174 FAIL ();
176 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
177 FAIL ();
179 if (snprintf (buf + 7, 3, "%s", "987654") != 6
180 || memcmp (buf, "aabcEDX98", 10))
181 FAIL ();
183 /* These ops need runtime checking, but shouldn't __chk_fail. */
184 memcpy (buf, "abcdefghij", l0 + 10);
185 memmove (buf + 1, buf, l0 + 9);
186 if (memcmp (buf, "aabcdefghi", 10))
187 FAIL ();
189 if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
190 || memcmp (buf, "aabcdabcde", 10))
191 FAIL ();
193 memset (buf + 8, 'j', l0 + 2);
194 if (memcmp (buf, "aabcdabcjj", 10))
195 FAIL ();
197 strcpy (buf + 4, str1 + 5);
198 if (memcmp (buf, "aabcEDCBA", 10))
199 FAIL ();
201 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
202 FAIL ();
204 strncpy (buf + 6, "X", l0 + 4);
205 if (memcmp (buf, "aabcEDX\0\0", 10))
206 FAIL ();
208 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
209 || memcmp (buf, "aabcEcd\0\0", 10))
210 FAIL ();
212 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
213 FAIL ();
215 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
216 FAIL ();
218 buf[l0 + 8] = '\0';
219 strcat (buf, "A");
220 if (memcmp (buf, "aabcEcd9A", 10))
221 FAIL ();
223 buf[l0 + 7] = '\0';
224 strncat (buf, "ZYXWV", l0 + 2);
225 if (memcmp (buf, "aabcEcdZY", 10))
226 FAIL ();
228 memcpy (a.buf1, "abcdefghij", l0 + 10);
229 memmove (a.buf1 + 1, a.buf1, l0 + 9);
230 if (memcmp (a.buf1, "aabcdefghi", 10))
231 FAIL ();
233 if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
234 || memcmp (a.buf1, "aabcdabcde", 10))
235 FAIL ();
237 memset (a.buf1 + 8, 'j', l0 + 2);
238 if (memcmp (a.buf1, "aabcdabcjj", 10))
239 FAIL ();
241 #if __USE_FORTIFY_LEVEL < 2
242 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
243 and sufficient GCC support, as the string operations overflow
244 from a.buf1 into a.buf2. */
245 strcpy (a.buf1 + 4, str1 + 5);
246 if (memcmp (a.buf1, "aabcEDCBA", 10))
247 FAIL ();
249 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
250 || memcmp (a.buf1, "aabcEDCBF", 10))
251 FAIL ();
253 strncpy (a.buf1 + 6, "X", l0 + 4);
254 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
255 FAIL ();
257 if (sprintf (a.buf1 + 7, "%d", num1) != 2
258 || memcmp (a.buf1, "aabcEDX67", 10))
259 FAIL ();
261 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
262 || memcmp (a.buf1, "aabcEDX98", 10))
263 FAIL ();
265 a.buf1[l0 + 8] = '\0';
266 strcat (a.buf1, "A");
267 if (memcmp (a.buf1, "aabcEDX9A", 10))
268 FAIL ();
270 a.buf1[l0 + 7] = '\0';
271 strncat (a.buf1, "ZYXWV", l0 + 2);
272 if (memcmp (a.buf1, "aabcEDXZY", 10))
273 FAIL ();
275 #endif
277 #if __USE_FORTIFY_LEVEL >= 1
278 /* Now check if all buffer overflows are caught at runtime. */
280 CHK_FAIL_START
281 memcpy (buf + 1, "abcdefghij", l0 + 10);
282 CHK_FAIL_END
284 CHK_FAIL_START
285 memmove (buf + 2, buf + 1, l0 + 9);
286 CHK_FAIL_END
288 CHK_FAIL_START
289 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
290 CHK_FAIL_END
292 CHK_FAIL_START
293 memset (buf + 9, 'j', l0 + 2);
294 CHK_FAIL_END
296 CHK_FAIL_START
297 strcpy (buf + 5, str1 + 5);
298 CHK_FAIL_END
300 CHK_FAIL_START
301 p = stpcpy (buf + 9, str2);
302 CHK_FAIL_END
304 CHK_FAIL_START
305 strncpy (buf + 7, "X", l0 + 4);
306 CHK_FAIL_END
308 CHK_FAIL_START
309 stpncpy (buf + 6, "cd", l0 + 5);
310 CHK_FAIL_END
312 # if !defined __cplusplus || defined __va_arg_pack
313 CHK_FAIL_START
314 sprintf (buf + 8, "%d", num1);
315 CHK_FAIL_END
317 CHK_FAIL_START
318 snprintf (buf + 8, l0 + 3, "%d", num2);
319 CHK_FAIL_END
321 CHK_FAIL_START
322 swprintf (wbuf + 8, 3, L"%d", num1);
323 CHK_FAIL_END
325 CHK_FAIL_START
326 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
327 CHK_FAIL_END
328 # endif
330 memcpy (buf, str1 + 2, l0 + 9);
331 CHK_FAIL_START
332 strcat (buf, "AB");
333 CHK_FAIL_END
335 memcpy (buf, str1 + 3, l0 + 8);
336 CHK_FAIL_START
337 strncat (buf, "ZYXWV", l0 + 3);
338 CHK_FAIL_END
340 CHK_FAIL_START
341 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
342 CHK_FAIL_END
344 CHK_FAIL_START
345 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
346 CHK_FAIL_END
348 CHK_FAIL_START
349 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
350 CHK_FAIL_END
352 CHK_FAIL_START
353 memset (a.buf1 + 9, 'j', l0 + 2);
354 CHK_FAIL_END
356 # if __USE_FORTIFY_LEVEL >= 2
357 # define O 0
358 # else
359 # define O 1
360 # endif
362 CHK_FAIL_START
363 strcpy (a.buf1 + (O + 4), str1 + 5);
364 CHK_FAIL_END
366 CHK_FAIL_START
367 p = stpcpy (a.buf1 + (O + 8), str2);
368 CHK_FAIL_END
370 CHK_FAIL_START
371 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
372 CHK_FAIL_END
374 # if !defined __cplusplus || defined __va_arg_pack
375 CHK_FAIL_START
376 sprintf (a.buf1 + (O + 7), "%d", num1);
377 CHK_FAIL_END
379 CHK_FAIL_START
380 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
381 CHK_FAIL_END
382 # endif
384 memcpy (a.buf1, str1 + (3 - O), l0 + 8 + O);
385 CHK_FAIL_START
386 strcat (a.buf1, "AB");
387 CHK_FAIL_END
389 memcpy (a.buf1, str1 + (4 - O), l0 + 7 + O);
390 CHK_FAIL_START
391 strncat (a.buf1, "ZYXWV", l0 + 3);
392 CHK_FAIL_END
393 #endif
396 /* These ops can be done without runtime checking of object size. */
397 wmemcpy (wbuf, L"abcdefghij", 10);
398 wmemmove (wbuf + 1, wbuf, 9);
399 if (wmemcmp (wbuf, L"aabcdefghi", 10))
400 FAIL ();
402 if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
403 || wmemcmp (wbuf, L"aabcdabcde", 10))
404 FAIL ();
406 wmemset (wbuf + 8, L'j', 2);
407 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
408 FAIL ();
410 wcscpy (wbuf + 4, L"EDCBA");
411 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
412 FAIL ();
414 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
415 FAIL ();
417 wcsncpy (wbuf + 6, L"X", 4);
418 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
419 FAIL ();
421 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
422 || wmemcmp (wbuf, L"aabcEDX98", 10))
423 FAIL ();
425 if (swprintf (wbuf + 7, 3, L"64") != 2
426 || wmemcmp (wbuf, L"aabcEDX64", 10))
427 FAIL ();
429 /* These ops need runtime checking, but shouldn't __chk_fail. */
430 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
431 wmemmove (wbuf + 1, wbuf, l0 + 9);
432 if (wmemcmp (wbuf, L"aabcdefghi", 10))
433 FAIL ();
435 if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
436 || wmemcmp (wbuf, L"aabcdabcde", 10))
437 FAIL ();
439 wmemset (wbuf + 8, L'j', l0 + 2);
440 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
441 FAIL ();
443 wcscpy (wbuf + 4, wstr1 + 5);
444 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
445 FAIL ();
447 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
448 FAIL ();
450 wcsncpy (wbuf + 6, L"X", l0 + 4);
451 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
452 FAIL ();
454 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
455 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
456 FAIL ();
458 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
459 || wmemcmp (wbuf, L"aabcEcd98", 10))
460 FAIL ();
462 wbuf[l0 + 8] = L'\0';
463 wcscat (wbuf, L"A");
464 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
465 FAIL ();
467 wbuf[l0 + 7] = L'\0';
468 wcsncat (wbuf, L"ZYXWV", l0 + 2);
469 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
470 FAIL ();
472 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
473 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
474 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
475 FAIL ();
477 if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
478 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
479 FAIL ();
481 wmemset (wa.buf1 + 8, L'j', l0 + 2);
482 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
483 FAIL ();
485 #if __USE_FORTIFY_LEVEL < 2
486 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
487 and sufficient GCC support, as the string operations overflow
488 from a.buf1 into a.buf2. */
489 wcscpy (wa.buf1 + 4, wstr1 + 5);
490 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
491 FAIL ();
493 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
494 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
495 FAIL ();
497 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
498 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
499 FAIL ();
501 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
502 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
503 FAIL ();
505 wa.buf1[l0 + 8] = L'\0';
506 wcscat (wa.buf1, L"A");
507 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
508 FAIL ();
510 wa.buf1[l0 + 7] = L'\0';
511 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
512 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
513 FAIL ();
515 #endif
517 #if __USE_FORTIFY_LEVEL >= 1
518 /* Now check if all buffer overflows are caught at runtime. */
520 CHK_FAIL_START
521 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
522 CHK_FAIL_END
524 CHK_FAIL_START
525 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
526 CHK_FAIL_END
528 CHK_FAIL_START
529 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
530 CHK_FAIL_END
532 CHK_FAIL_START
533 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
534 CHK_FAIL_END
536 CHK_FAIL_START
537 wmemset (wbuf + 9, L'j', l0 + 2);
538 CHK_FAIL_END
540 CHK_FAIL_START
541 wcscpy (wbuf + 5, wstr1 + 5);
542 CHK_FAIL_END
544 CHK_FAIL_START
545 wp = wcpcpy (wbuf + 9, wstr2);
546 CHK_FAIL_END
548 CHK_FAIL_START
549 wcsncpy (wbuf + 7, L"X", l0 + 4);
550 CHK_FAIL_END
552 CHK_FAIL_START
553 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
554 CHK_FAIL_END
556 CHK_FAIL_START
557 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
558 CHK_FAIL_END
560 CHK_FAIL_START
561 wcpncpy (wbuf + 6, L"cd", l0 + 5);
562 CHK_FAIL_END
564 wmemcpy (wbuf, wstr1 + 2, l0 + 9);
565 CHK_FAIL_START
566 wcscat (wbuf, L"AB");
567 CHK_FAIL_END
569 wmemcpy (wbuf, wstr1 + 3, l0 + 8);
570 CHK_FAIL_START
571 wcsncat (wbuf, L"ZYXWV", l0 + 3);
572 CHK_FAIL_END
574 CHK_FAIL_START
575 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
576 CHK_FAIL_END
578 CHK_FAIL_START
579 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
580 CHK_FAIL_END
582 CHK_FAIL_START
583 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
584 CHK_FAIL_END
586 CHK_FAIL_START
587 wmemset (wa.buf1 + 9, L'j', l0 + 2);
588 CHK_FAIL_END
590 #if __USE_FORTIFY_LEVEL >= 2
591 # define O 0
592 #else
593 # define O 1
594 #endif
596 CHK_FAIL_START
597 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
598 CHK_FAIL_END
600 CHK_FAIL_START
601 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
602 CHK_FAIL_END
604 CHK_FAIL_START
605 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
606 CHK_FAIL_END
608 wmemcpy (wa.buf1, wstr1 + (3 - O), l0 + 8 + O);
609 CHK_FAIL_START
610 wcscat (wa.buf1, L"AB");
611 CHK_FAIL_END
613 wmemcpy (wa.buf1, wstr1 + (4 - O), l0 + 7 + O);
614 CHK_FAIL_START
615 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
616 CHK_FAIL_END
617 #endif
620 /* Now checks for %n protection. */
622 /* Constant literals passed directly are always ok
623 (even with warnings about possible bugs from GCC). */
624 int n1, n2;
625 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
626 || n1 != 1 || n2 != 2)
627 FAIL ();
629 /* In this case the format string is not known at compile time,
630 but resides in read-only memory, so is ok. */
631 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
632 || n1 != 1 || n2 != 2)
633 FAIL ();
635 strcpy (buf2 + 2, "%n%s%n");
636 /* When the format string is writable and contains %n,
637 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
638 CHK_FAIL2_START
639 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
640 FAIL ();
641 CHK_FAIL2_END
643 CHK_FAIL2_START
644 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
645 FAIL ();
646 CHK_FAIL2_END
648 /* But if there is no %n, even writable format string
649 should work. */
650 buf2[6] = '\0';
651 if (sprintf (buf, buf2 + 4, str2) != 1)
652 FAIL ();
654 /* Constant literals passed directly are always ok
655 (even with warnings about possible bugs from GCC). */
656 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
657 || n1 != 7 || n2 != 14)
658 FAIL ();
660 /* In this case the format string is not known at compile time,
661 but resides in read-only memory, so is ok. */
662 if (printf (str3, str4, &n1, str5, &n2) != 14
663 || n1 != 7 || n2 != 14)
664 FAIL ();
666 strcpy (buf2 + 2, "%n%s%n");
667 /* When the format string is writable and contains %n,
668 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
669 CHK_FAIL2_START
670 if (printf (buf2, str4, &n1, str5, &n1) != 14)
671 FAIL ();
672 CHK_FAIL2_END
674 /* But if there is no %n, even writable format string
675 should work. */
676 buf2[6] = '\0';
677 if (printf (buf2 + 4, str5) != 7)
678 FAIL ();
680 FILE *fp = stdout;
682 /* Constant literals passed directly are always ok
683 (even with warnings about possible bugs from GCC). */
684 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
685 || n1 != 7 || n2 != 14)
686 FAIL ();
688 /* In this case the format string is not known at compile time,
689 but resides in read-only memory, so is ok. */
690 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
691 || n1 != 7 || n2 != 14)
692 FAIL ();
694 strcpy (buf2 + 2, "%n%s%n");
695 /* When the format string is writable and contains %n,
696 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
697 CHK_FAIL2_START
698 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
699 FAIL ();
700 CHK_FAIL2_END
702 /* But if there is no %n, even writable format string
703 should work. */
704 buf2[6] = '\0';
705 if (fprintf (fp, buf2 + 4, str5) != 7)
706 FAIL ();
708 if (freopen (temp_filename, "r", stdin) == NULL)
710 puts ("could not open temporary file");
711 exit (1);
714 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
715 FAIL ();
716 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
717 FAIL ();
719 #if __USE_FORTIFY_LEVEL >= 1
720 CHK_FAIL_START
721 if (gets (buf) != buf)
722 FAIL ();
723 CHK_FAIL_END
724 #endif
726 rewind (stdin);
728 if (fgets (buf, sizeof (buf), stdin) != buf
729 || memcmp (buf, "abcdefgh\n", 10))
730 FAIL ();
731 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
732 FAIL ();
734 rewind (stdin);
736 if (fgets (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 (buf, sizeof (buf) + 1, stdin) != buf)
743 FAIL ();
744 CHK_FAIL_END
746 CHK_FAIL_START
747 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
748 FAIL ();
749 CHK_FAIL_END
750 #endif
752 rewind (stdin);
754 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
755 || memcmp (buf, "abcdefgh\n", 10))
756 FAIL ();
757 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
758 || memcmp (buf, "ABCDEFGHI", 10))
759 FAIL ();
761 rewind (stdin);
763 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
764 || memcmp (buf, "abcdefgh\n", 10))
765 FAIL ();
767 #if __USE_FORTIFY_LEVEL >= 1
768 CHK_FAIL_START
769 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
770 FAIL ();
771 CHK_FAIL_END
773 CHK_FAIL_START
774 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
775 FAIL ();
776 CHK_FAIL_END
777 #endif
779 rewind (stdin);
781 if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
782 || memcmp (buf, "abcdefgh\nA", 10))
783 FAIL ();
784 if (fread (buf, sizeof (buf), 1, stdin) != 1
785 || memcmp (buf, "BCDEFGHI\na", 10))
786 FAIL ();
788 rewind (stdin);
790 if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
791 || memcmp (buf, "abcdefgh\nA", 10))
792 FAIL ();
793 if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
794 || memcmp (buf, "BCDEFGHI\na", 10))
795 FAIL ();
797 #if __USE_FORTIFY_LEVEL >= 1
798 CHK_FAIL_START
799 if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
800 FAIL ();
801 CHK_FAIL_END
803 CHK_FAIL_START
804 if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
805 FAIL ();
806 CHK_FAIL_END
807 #endif
809 rewind (stdin);
811 if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
812 || memcmp (buf, "abcdefgh\nA", 10))
813 FAIL ();
814 if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
815 || memcmp (buf, "BCDEFGHI\na", 10))
816 FAIL ();
818 rewind (stdin);
820 if (fread_unlocked (buf, 1, 4, stdin) != 4
821 || memcmp (buf, "abcdFGHI\na", 10))
822 FAIL ();
823 if (fread_unlocked (buf, 4, 1, stdin) != 1
824 || memcmp (buf, "efghFGHI\na", 10))
825 FAIL ();
827 rewind (stdin);
829 if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
830 || memcmp (buf, "abcdefgh\nA", 10))
831 FAIL ();
832 if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
833 || memcmp (buf, "BCDEFGHI\na", 10))
834 FAIL ();
836 #if __USE_FORTIFY_LEVEL >= 1
837 CHK_FAIL_START
838 if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
839 FAIL ();
840 CHK_FAIL_END
842 CHK_FAIL_START
843 if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
844 FAIL ();
845 CHK_FAIL_END
846 #endif
848 lseek (fileno (stdin), 0, SEEK_SET);
850 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
851 || memcmp (buf, "abcdefgh\n", 9))
852 FAIL ();
853 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
854 || memcmp (buf, "ABCDEFGHI", 9))
855 FAIL ();
857 lseek (fileno (stdin), 0, SEEK_SET);
859 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
860 || memcmp (buf, "abcdefgh\n", 9))
861 FAIL ();
863 #if __USE_FORTIFY_LEVEL >= 1
864 CHK_FAIL_START
865 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
866 FAIL ();
867 CHK_FAIL_END
868 #endif
870 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
871 != sizeof (buf) - 1
872 || memcmp (buf, "\nABCDEFGH", 9))
873 FAIL ();
874 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
875 || memcmp (buf, "abcdefgh\n", 9))
876 FAIL ();
877 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
878 != sizeof (buf) - 1
879 || memcmp (buf, "h\nABCDEFG", 9))
880 FAIL ();
882 #if __USE_FORTIFY_LEVEL >= 1
883 CHK_FAIL_START
884 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
885 != sizeof (buf) + 1)
886 FAIL ();
887 CHK_FAIL_END
888 #endif
890 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
891 != sizeof (buf) - 1
892 || memcmp (buf, "\nABCDEFGH", 9))
893 FAIL ();
894 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
895 || memcmp (buf, "abcdefgh\n", 9))
896 FAIL ();
897 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
898 != sizeof (buf) - 1
899 || memcmp (buf, "h\nABCDEFG", 9))
900 FAIL ();
902 #if __USE_FORTIFY_LEVEL >= 1
903 CHK_FAIL_START
904 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
905 != sizeof (buf) + 1)
906 FAIL ();
907 CHK_FAIL_END
908 #endif
910 if (freopen (temp_filename, "r", stdin) == NULL)
912 puts ("could not open temporary file");
913 exit (1);
916 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
918 puts ("could not seek in test file");
919 exit (1);
922 #if __USE_FORTIFY_LEVEL >= 1
923 CHK_FAIL_START
924 if (gets (buf) != buf)
925 FAIL ();
926 CHK_FAIL_END
927 #endif
929 /* Check whether missing N$ formats are detected. */
930 CHK_FAIL2_START
931 printf ("%3$d\n", 1, 2, 3, 4);
932 CHK_FAIL2_END
934 CHK_FAIL2_START
935 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
936 CHK_FAIL2_END
938 CHK_FAIL2_START
939 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
940 CHK_FAIL2_END
942 CHK_FAIL2_START
943 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
944 CHK_FAIL2_END
946 int sp[2];
947 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
948 FAIL ();
949 else
951 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
952 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
953 != strlen (sendstr))
954 FAIL ();
956 char recvbuf[12];
957 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
958 != sizeof recvbuf
959 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
960 FAIL ();
962 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
963 != sizeof recvbuf - 7
964 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
965 FAIL ();
967 #if __USE_FORTIFY_LEVEL >= 1
968 CHK_FAIL_START
969 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
970 != sizeof recvbuf)
971 FAIL ();
972 CHK_FAIL_END
974 CHK_FAIL_START
975 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
976 != sizeof recvbuf - 3)
977 FAIL ();
978 CHK_FAIL_END
979 #endif
981 socklen_t sl;
982 struct sockaddr_un sa_un;
984 sl = sizeof (sa_un);
985 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
986 (struct sockaddr *) &sa_un, &sl)
987 != sizeof recvbuf
988 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
989 FAIL ();
991 sl = sizeof (sa_un);
992 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
993 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
994 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
995 FAIL ();
997 #if __USE_FORTIFY_LEVEL >= 1
998 CHK_FAIL_START
999 sl = sizeof (sa_un);
1000 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1001 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1002 FAIL ();
1003 CHK_FAIL_END
1005 CHK_FAIL_START
1006 sl = sizeof (sa_un);
1007 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1008 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1009 FAIL ();
1010 CHK_FAIL_END
1011 #endif
1013 close (sp[0]);
1014 close (sp[1]);
1017 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1018 char *enddir = strchr (fname, '\0');
1019 if (mkdtemp (fname) == NULL)
1021 printf ("mkdtemp failed: %m\n");
1022 return 1;
1024 *enddir = '/';
1025 if (symlink ("bar", fname) != 0)
1026 FAIL ();
1028 char readlinkbuf[4];
1029 if (readlink (fname, readlinkbuf, 4) != 3
1030 || memcmp (readlinkbuf, "bar", 3) != 0)
1031 FAIL ();
1032 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1033 || memcmp (readlinkbuf, "bbar", 4) != 0)
1034 FAIL ();
1036 #if __USE_FORTIFY_LEVEL >= 1
1037 CHK_FAIL_START
1038 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1039 FAIL ();
1040 CHK_FAIL_END
1042 CHK_FAIL_START
1043 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1044 FAIL ();
1045 CHK_FAIL_END
1046 #endif
1048 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1049 if (tmpfd < 0)
1050 FAIL ();
1052 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1053 || memcmp (readlinkbuf, "bar", 3) != 0)
1054 FAIL ();
1055 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1056 l0 + 3) != 3
1057 || memcmp (readlinkbuf, "bbar", 4) != 0)
1058 FAIL ();
1060 #if __USE_FORTIFY_LEVEL >= 1
1061 CHK_FAIL_START
1062 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1063 l0 + 3) != 3)
1064 FAIL ();
1065 CHK_FAIL_END
1067 CHK_FAIL_START
1068 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1069 4) != 3)
1070 FAIL ();
1071 CHK_FAIL_END
1072 #endif
1074 close (tmpfd);
1076 char *cwd1 = getcwd (NULL, 0);
1077 if (cwd1 == NULL)
1078 FAIL ();
1080 char *cwd2 = getcwd (NULL, 250);
1081 if (cwd2 == NULL)
1082 FAIL ();
1084 if (cwd1 && cwd2)
1086 if (strcmp (cwd1, cwd2) != 0)
1087 FAIL ();
1089 *enddir = '\0';
1090 if (chdir (fname))
1091 FAIL ();
1093 char *cwd3 = getcwd (NULL, 0);
1094 if (cwd3 == NULL)
1095 FAIL ();
1096 if (strcmp (fname, cwd3) != 0)
1097 printf ("getcwd after chdir is '%s' != '%s',"
1098 "get{c,}wd tests skipped\n", cwd3, fname);
1099 else
1101 char getcwdbuf[sizeof fname - 3];
1103 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1104 if (cwd4 != getcwdbuf
1105 || strcmp (getcwdbuf, fname) != 0)
1106 FAIL ();
1108 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1109 if (cwd4 != getcwdbuf + 1
1110 || getcwdbuf[0] != fname[0]
1111 || strcmp (getcwdbuf + 1, fname) != 0)
1112 FAIL ();
1114 #if __USE_FORTIFY_LEVEL >= 1
1115 CHK_FAIL_START
1116 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1117 != getcwdbuf + 2)
1118 FAIL ();
1119 CHK_FAIL_END
1121 CHK_FAIL_START
1122 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1123 != getcwdbuf + 2)
1124 FAIL ();
1125 CHK_FAIL_END
1126 #endif
1128 if (getwd (getcwdbuf) != getcwdbuf
1129 || strcmp (getcwdbuf, fname) != 0)
1130 FAIL ();
1132 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1133 || strcmp (getcwdbuf + 1, fname) != 0)
1134 FAIL ();
1136 #if __USE_FORTIFY_LEVEL >= 1
1137 CHK_FAIL_START
1138 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1139 FAIL ();
1140 CHK_FAIL_END
1141 #endif
1144 if (chdir (cwd1) != 0)
1145 FAIL ();
1146 free (cwd3);
1149 free (cwd1);
1150 free (cwd2);
1151 *enddir = '/';
1152 if (unlink (fname) != 0)
1153 FAIL ();
1155 *enddir = '\0';
1156 if (rmdir (fname) != 0)
1157 FAIL ();
1160 #if PATH_MAX > 0
1161 char largebuf[PATH_MAX];
1162 char *realres = realpath (".", largebuf);
1163 if (realres != largebuf)
1164 FAIL ();
1166 # if __USE_FORTIFY_LEVEL >= 1
1167 CHK_FAIL_START
1168 char realbuf[1];
1169 realres = realpath (".", realbuf);
1170 if (realres != realbuf)
1171 FAIL ();
1172 CHK_FAIL_END
1173 # endif
1174 #endif
1176 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1178 assert (MB_CUR_MAX <= 10);
1180 /* First a simple test. */
1181 char enough[10];
1182 if (wctomb (enough, L'A') != 1)
1183 FAIL ();
1185 #if __USE_FORTIFY_LEVEL >= 1
1186 /* We know the wchar_t encoding is ISO 10646. So pick a
1187 character which has a multibyte representation which does not
1188 fit. */
1189 CHK_FAIL_START
1190 char smallbuf[2];
1191 if (wctomb (smallbuf, L'\x100') != 2)
1192 FAIL ();
1193 CHK_FAIL_END
1194 #endif
1196 mbstate_t s;
1197 memset (&s, '\0', sizeof (s));
1198 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1199 FAIL ();
1201 #if __USE_FORTIFY_LEVEL >= 1
1202 /* We know the wchar_t encoding is ISO 10646. So pick a
1203 character which has a multibyte representation which does not
1204 fit. */
1205 CHK_FAIL_START
1206 char smallbuf[2];
1207 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1208 FAIL ();
1209 CHK_FAIL_END
1210 #endif
1212 wchar_t wenough[10];
1213 memset (&s, '\0', sizeof (s));
1214 const char *cp = "A";
1215 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1216 || wcscmp (wenough, L"A") != 0)
1217 FAIL ();
1219 cp = "BC";
1220 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1221 || wcscmp (wenough, L"BC") != 0)
1222 FAIL ();
1224 #if __USE_FORTIFY_LEVEL >= 1
1225 CHK_FAIL_START
1226 wchar_t wsmallbuf[2];
1227 cp = "ABC";
1228 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1229 CHK_FAIL_END
1230 #endif
1232 cp = "A";
1233 if (mbstowcs (wenough, cp, 10) != 1
1234 || wcscmp (wenough, L"A") != 0)
1235 FAIL ();
1237 cp = "DEF";
1238 if (mbstowcs (wenough, cp, l0 + 10) != 3
1239 || wcscmp (wenough, L"DEF") != 0)
1240 FAIL ();
1242 #if __USE_FORTIFY_LEVEL >= 1
1243 CHK_FAIL_START
1244 wchar_t wsmallbuf[2];
1245 cp = "ABC";
1246 mbstowcs (wsmallbuf, cp, 10);
1247 CHK_FAIL_END
1248 #endif
1250 memset (&s, '\0', sizeof (s));
1251 cp = "ABC";
1252 wcscpy (wenough, L"DEF");
1253 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1254 || wcscmp (wenough, L"AEF") != 0)
1255 FAIL ();
1257 cp = "IJ";
1258 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1259 || wcscmp (wenough, L"IEF") != 0)
1260 FAIL ();
1262 #if __USE_FORTIFY_LEVEL >= 1
1263 CHK_FAIL_START
1264 wchar_t wsmallbuf[2];
1265 cp = "ABC";
1266 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1267 CHK_FAIL_END
1268 #endif
1270 memset (&s, '\0', sizeof (s));
1271 const wchar_t *wcp = L"A";
1272 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1273 || strcmp (enough, "A") != 0)
1274 FAIL ();
1276 wcp = L"BC";
1277 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1278 || strcmp (enough, "BC") != 0)
1279 FAIL ();
1281 #if __USE_FORTIFY_LEVEL >= 1
1282 CHK_FAIL_START
1283 char smallbuf[2];
1284 wcp = L"ABC";
1285 wcsrtombs (smallbuf, &wcp, 10, &s);
1286 CHK_FAIL_END
1287 #endif
1289 memset (enough, 'Z', sizeof (enough));
1290 wcp = L"EF";
1291 if (wcstombs (enough, wcp, 10) != 2
1292 || strcmp (enough, "EF") != 0)
1293 FAIL ();
1295 wcp = L"G";
1296 if (wcstombs (enough, wcp, l0 + 10) != 1
1297 || strcmp (enough, "G") != 0)
1298 FAIL ();
1300 #if __USE_FORTIFY_LEVEL >= 1
1301 CHK_FAIL_START
1302 char smallbuf[2];
1303 wcp = L"ABC";
1304 wcstombs (smallbuf, wcp, 10);
1305 CHK_FAIL_END
1306 #endif
1308 memset (&s, '\0', sizeof (s));
1309 wcp = L"AB";
1310 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1311 || strcmp (enough, "A") != 0)
1312 FAIL ();
1314 wcp = L"BCD";
1315 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1316 || strcmp (enough, "B") != 0)
1317 FAIL ();
1319 #if __USE_FORTIFY_LEVEL >= 1
1320 CHK_FAIL_START
1321 char smallbuf[2];
1322 wcp = L"ABC";
1323 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1324 CHK_FAIL_END
1325 #endif
1327 else
1329 puts ("cannot set locale");
1330 ret = 1;
1333 fd = posix_openpt (O_RDWR);
1334 if (fd != -1)
1336 char enough[1000];
1337 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1338 FAIL ();
1340 #if __USE_FORTIFY_LEVEL >= 1
1341 CHK_FAIL_START
1342 char smallbuf[2];
1343 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1344 FAIL ();
1345 CHK_FAIL_END
1346 #endif
1347 close (fd);
1350 #if PATH_MAX > 0
1351 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1352 # if __USE_FORTIFY_LEVEL >= 1
1353 CHK_FAIL_START
1354 char smallbuf[1];
1355 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1356 CHK_FAIL_END
1357 # endif
1358 #endif
1360 gid_t grpslarge[5];
1361 int ngr = getgroups (5, grpslarge);
1362 asm volatile ("" : : "r" (ngr));
1363 #if __USE_FORTIFY_LEVEL >= 1
1364 CHK_FAIL_START
1365 char smallbuf[1];
1366 ngr = getgroups (5, (gid_t *) smallbuf);
1367 asm volatile ("" : : "r" (ngr));
1368 CHK_FAIL_END
1369 #endif
1371 fd = open (_PATH_TTY, O_RDONLY);
1372 if (fd != -1)
1374 char enough[1000];
1375 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1376 FAIL ();
1378 #if __USE_FORTIFY_LEVEL >= 1
1379 CHK_FAIL_START
1380 char smallbuf[2];
1381 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1382 FAIL ();
1383 CHK_FAIL_END
1384 #endif
1385 close (fd);
1388 char hostnamelarge[1000];
1389 gethostname (hostnamelarge, sizeof (hostnamelarge));
1390 #if __USE_FORTIFY_LEVEL >= 1
1391 CHK_FAIL_START
1392 char smallbuf[1];
1393 gethostname (smallbuf, sizeof (hostnamelarge));
1394 CHK_FAIL_END
1395 #endif
1397 char loginlarge[1000];
1398 getlogin_r (loginlarge, sizeof (hostnamelarge));
1399 #if __USE_FORTIFY_LEVEL >= 1
1400 CHK_FAIL_START
1401 char smallbuf[1];
1402 getlogin_r (smallbuf, sizeof (loginlarge));
1403 CHK_FAIL_END
1404 #endif
1406 char domainnamelarge[1000];
1407 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1408 asm volatile ("" : : "r" (res));
1409 #if __USE_FORTIFY_LEVEL >= 1
1410 CHK_FAIL_START
1411 char smallbuf[1];
1412 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1413 asm volatile ("" : : "r" (res));
1414 CHK_FAIL_END
1415 #endif
1417 return ret;