Consolidate non cancellable writev call
[glibc.git] / debug / tst-chk1.c
blob60c8e1e1d05200a52280f512fdd4d0bedd98b7ab
1 /* Copyright (C) 2004-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* 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 <locale.h>
27 #include <obstack.h>
28 #include <setjmp.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <wchar.h>
35 #include <sys/poll.h>
36 #include <sys/select.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
41 #define obstack_chunk_alloc malloc
42 #define obstack_chunk_free free
44 char *temp_filename;
45 static void do_prepare (void);
46 static int do_test (void);
47 #define PREPARE(argc, argv) do_prepare ()
48 #define TEST_FUNCTION do_test ()
49 #include "../test-skeleton.c"
51 static void
52 do_prepare (void)
54 int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
55 if (temp_fd == -1)
57 printf ("cannot create temporary file: %m\n");
58 exit (1);
61 const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
62 if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
64 puts ("could not write test strings into file");
65 unlink (temp_filename);
66 exit (1);
70 volatile int chk_fail_ok;
71 volatile int ret;
72 jmp_buf chk_fail_buf;
74 static void
75 handler (int sig)
77 if (chk_fail_ok)
79 chk_fail_ok = 0;
80 longjmp (chk_fail_buf, 1);
82 else
83 _exit (127);
86 char buf[10];
87 wchar_t wbuf[10];
88 volatile size_t l0;
89 volatile char *p;
90 volatile wchar_t *wp;
91 const char *str1 = "JIHGFEDCBA";
92 const char *str2 = "F";
93 const char *str3 = "%s%n%s%n";
94 const char *str4 = "Hello, ";
95 const char *str5 = "World!\n";
96 const wchar_t *wstr1 = L"JIHGFEDCBA";
97 const wchar_t *wstr2 = L"F";
98 const wchar_t *wstr3 = L"%s%n%s%n";
99 const wchar_t *wstr4 = L"Hello, ";
100 const wchar_t *wstr5 = L"World!\n";
101 char buf2[10] = "%s";
102 int num1 = 67;
103 int num2 = 987654;
105 #define FAIL() \
106 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
107 #define CHK_FAIL_START \
108 chk_fail_ok = 1; \
109 if (! setjmp (chk_fail_buf)) \
111 #define CHK_FAIL_END \
112 chk_fail_ok = 0; \
113 FAIL (); \
115 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
116 # define CHK_FAIL2_START CHK_FAIL_START
117 # define CHK_FAIL2_END CHK_FAIL_END
118 #else
119 # define CHK_FAIL2_START
120 # define CHK_FAIL2_END
121 #endif
123 static int
124 do_test (void)
126 set_fortify_handler (handler);
128 struct A { char buf1[9]; char buf2[1]; } a;
129 struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
131 printf ("Test checking routines at fortify level %d\n",
132 #ifdef __USE_FORTIFY_LEVEL
133 (int) __USE_FORTIFY_LEVEL
134 #else
136 #endif
139 #if defined __USE_FORTIFY_LEVEL && !defined __fortify_function
140 printf ("Test skipped");
141 if (l0 == 0)
142 return 0;
143 #endif
145 /* These ops can be done without runtime checking of object size. */
146 memcpy (buf, "abcdefghij", 10);
147 memmove (buf + 1, buf, 9);
148 if (memcmp (buf, "aabcdefghi", 10))
149 FAIL ();
151 memcpy (buf, "abcdefghij", 10);
152 bcopy (buf, buf + 1, 9);
153 if (memcmp (buf, "aabcdefghi", 10))
154 FAIL ();
156 if (mempcpy (buf + 5, "abcde", 5) != buf + 10
157 || memcmp (buf, "aabcdabcde", 10))
158 FAIL ();
160 memset (buf + 8, 'j', 2);
161 if (memcmp (buf, "aabcdabcjj", 10))
162 FAIL ();
164 bzero (buf + 8, 2);
165 if (memcmp (buf, "aabcdabc\0\0", 10))
166 FAIL ();
168 explicit_bzero (buf + 6, 4);
169 if (memcmp (buf, "aabcda\0\0\0\0", 10))
170 FAIL ();
172 strcpy (buf + 4, "EDCBA");
173 if (memcmp (buf, "aabcEDCBA", 10))
174 FAIL ();
176 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
177 FAIL ();
179 strncpy (buf + 6, "X", 4);
180 if (memcmp (buf, "aabcEDX\0\0", 10))
181 FAIL ();
183 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
184 FAIL ();
186 if (snprintf (buf + 7, 3, "%s", "987654") != 6
187 || memcmp (buf, "aabcEDX98", 10))
188 FAIL ();
190 /* These ops need runtime checking, but shouldn't __chk_fail. */
191 memcpy (buf, "abcdefghij", l0 + 10);
192 memmove (buf + 1, buf, l0 + 9);
193 if (memcmp (buf, "aabcdefghi", 10))
194 FAIL ();
196 memcpy (buf, "abcdefghij", l0 + 10);
197 bcopy (buf, buf + 1, l0 + 9);
198 if (memcmp (buf, "aabcdefghi", 10))
199 FAIL ();
201 if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
202 || memcmp (buf, "aabcdabcde", 10))
203 FAIL ();
205 memset (buf + 8, 'j', l0 + 2);
206 if (memcmp (buf, "aabcdabcjj", 10))
207 FAIL ();
209 bzero (buf + 8, l0 + 2);
210 if (memcmp (buf, "aabcdabc\0\0", 10))
211 FAIL ();
213 explicit_bzero (buf + 6, l0 + 4);
214 if (memcmp (buf, "aabcda\0\0\0\0", 10))
215 FAIL ();
217 strcpy (buf + 4, str1 + 5);
218 if (memcmp (buf, "aabcEDCBA", 10))
219 FAIL ();
221 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
222 FAIL ();
224 strncpy (buf + 6, "X", l0 + 4);
225 if (memcmp (buf, "aabcEDX\0\0", 10))
226 FAIL ();
228 if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
229 || memcmp (buf, "aabcEcd\0\0", 10))
230 FAIL ();
232 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
233 FAIL ();
235 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
236 FAIL ();
238 buf[l0 + 8] = '\0';
239 strcat (buf, "A");
240 if (memcmp (buf, "aabcEcd9A", 10))
241 FAIL ();
243 buf[l0 + 7] = '\0';
244 strncat (buf, "ZYXWV", l0 + 2);
245 if (memcmp (buf, "aabcEcdZY", 10))
246 FAIL ();
248 /* The following tests are supposed to succeed at all fortify
249 levels, even though they overflow a.buf1 into a.buf2. */
250 memcpy (a.buf1, "abcdefghij", l0 + 10);
251 memmove (a.buf1 + 1, a.buf1, l0 + 9);
252 if (memcmp (a.buf1, "aabcdefghi", 10))
253 FAIL ();
255 memcpy (a.buf1, "abcdefghij", l0 + 10);
256 bcopy (a.buf1, a.buf1 + 1, l0 + 9);
257 if (memcmp (a.buf1, "aabcdefghi", 10))
258 FAIL ();
260 if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
261 || memcmp (a.buf1, "aabcdabcde", 10))
262 FAIL ();
264 memset (a.buf1 + 8, 'j', l0 + 2);
265 if (memcmp (a.buf1, "aabcdabcjj", 10))
266 FAIL ();
268 bzero (a.buf1 + 8, l0 + 2);
269 if (memcmp (a.buf1, "aabcdabc\0\0", 10))
270 FAIL ();
272 explicit_bzero (a.buf1 + 6, l0 + 4);
273 if (memcmp (a.buf1, "aabcda\0\0\0\0", 10))
274 FAIL ();
276 #if __USE_FORTIFY_LEVEL < 2
277 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
278 and sufficient GCC support, as the string operations overflow
279 from a.buf1 into a.buf2. */
280 strcpy (a.buf1 + 4, str1 + 5);
281 if (memcmp (a.buf1, "aabcEDCBA", 10))
282 FAIL ();
284 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
285 || memcmp (a.buf1, "aabcEDCBF", 10))
286 FAIL ();
288 strncpy (a.buf1 + 6, "X", l0 + 4);
289 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
290 FAIL ();
292 if (sprintf (a.buf1 + 7, "%d", num1) != 2
293 || memcmp (a.buf1, "aabcEDX67", 10))
294 FAIL ();
296 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
297 || memcmp (a.buf1, "aabcEDX98", 10))
298 FAIL ();
300 a.buf1[l0 + 8] = '\0';
301 strcat (a.buf1, "A");
302 if (memcmp (a.buf1, "aabcEDX9A", 10))
303 FAIL ();
305 a.buf1[l0 + 7] = '\0';
306 strncat (a.buf1, "ZYXWV", l0 + 2);
307 if (memcmp (a.buf1, "aabcEDXZY", 10))
308 FAIL ();
310 #endif
312 #if __USE_FORTIFY_LEVEL >= 1
313 /* Now check if all buffer overflows are caught at runtime.
314 N.B. All tests involving a length parameter need to be done
315 twice: once with the length a compile-time constant, once without. */
317 CHK_FAIL_START
318 memcpy (buf + 1, "abcdefghij", 10);
319 CHK_FAIL_END
321 CHK_FAIL_START
322 memcpy (buf + 1, "abcdefghij", l0 + 10);
323 CHK_FAIL_END
325 CHK_FAIL_START
326 memmove (buf + 2, buf + 1, 9);
327 CHK_FAIL_END
329 CHK_FAIL_START
330 memmove (buf + 2, buf + 1, l0 + 9);
331 CHK_FAIL_END
333 CHK_FAIL_START
334 bcopy (buf + 1, buf + 2, 9);
335 CHK_FAIL_END
337 CHK_FAIL_START
338 bcopy (buf + 1, buf + 2, l0 + 9);
339 CHK_FAIL_END
341 CHK_FAIL_START
342 p = (char *) mempcpy (buf + 6, "abcde", 5);
343 CHK_FAIL_END
345 CHK_FAIL_START
346 p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
347 CHK_FAIL_END
349 CHK_FAIL_START
350 memset (buf + 9, 'j', 2);
351 CHK_FAIL_END
353 CHK_FAIL_START
354 memset (buf + 9, 'j', l0 + 2);
355 CHK_FAIL_END
357 CHK_FAIL_START
358 bzero (buf + 9, 2);
359 CHK_FAIL_END
361 CHK_FAIL_START
362 bzero (buf + 9, l0 + 2);
363 CHK_FAIL_END
365 CHK_FAIL_START
366 explicit_bzero (buf + 9, 2);
367 CHK_FAIL_END
369 CHK_FAIL_START
370 explicit_bzero (buf + 9, l0 + 2);
371 CHK_FAIL_END
373 CHK_FAIL_START
374 strcpy (buf + 5, str1 + 5);
375 CHK_FAIL_END
377 CHK_FAIL_START
378 p = stpcpy (buf + 9, str2);
379 CHK_FAIL_END
381 CHK_FAIL_START
382 strncpy (buf + 7, "X", 4);
383 CHK_FAIL_END
385 CHK_FAIL_START
386 strncpy (buf + 7, "X", l0 + 4);
387 CHK_FAIL_END
389 CHK_FAIL_START
390 stpncpy (buf + 6, "cd", 5);
391 CHK_FAIL_END
393 CHK_FAIL_START
394 stpncpy (buf + 6, "cd", l0 + 5);
395 CHK_FAIL_END
397 # if !defined __cplusplus || defined __va_arg_pack
398 CHK_FAIL_START
399 sprintf (buf + 8, "%d", num1);
400 CHK_FAIL_END
402 CHK_FAIL_START
403 snprintf (buf + 8, 3, "%d", num2);
404 CHK_FAIL_END
406 CHK_FAIL_START
407 snprintf (buf + 8, l0 + 3, "%d", num2);
408 CHK_FAIL_END
410 CHK_FAIL_START
411 swprintf (wbuf + 8, 3, L"%d", num1);
412 CHK_FAIL_END
414 CHK_FAIL_START
415 swprintf (wbuf + 8, l0 + 3, L"%d", num1);
416 CHK_FAIL_END
417 # endif
419 memcpy (buf, str1 + 2, 9);
420 CHK_FAIL_START
421 strcat (buf, "AB");
422 CHK_FAIL_END
424 memcpy (buf, str1 + 3, 8);
425 CHK_FAIL_START
426 strncat (buf, "ZYXWV", 3);
427 CHK_FAIL_END
429 memcpy (buf, str1 + 3, 8);
430 CHK_FAIL_START
431 strncat (buf, "ZYXWV", l0 + 3);
432 CHK_FAIL_END
434 CHK_FAIL_START
435 memcpy (a.buf1 + 1, "abcdefghij", 10);
436 CHK_FAIL_END
438 CHK_FAIL_START
439 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
440 CHK_FAIL_END
442 CHK_FAIL_START
443 memmove (a.buf1 + 2, a.buf1 + 1, 9);
444 CHK_FAIL_END
446 CHK_FAIL_START
447 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
448 CHK_FAIL_END
450 CHK_FAIL_START
451 bcopy (a.buf1 + 1, a.buf1 + 2, 9);
452 CHK_FAIL_END
454 CHK_FAIL_START
455 bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
456 CHK_FAIL_END
458 CHK_FAIL_START
459 p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
460 CHK_FAIL_END
462 CHK_FAIL_START
463 p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
464 CHK_FAIL_END
466 CHK_FAIL_START
467 memset (a.buf1 + 9, 'j', 2);
468 CHK_FAIL_END
470 CHK_FAIL_START
471 memset (a.buf1 + 9, 'j', l0 + 2);
472 CHK_FAIL_END
474 CHK_FAIL_START
475 bzero (a.buf1 + 9, 2);
476 CHK_FAIL_END
478 CHK_FAIL_START
479 bzero (a.buf1 + 9, l0 + 2);
480 CHK_FAIL_END
482 CHK_FAIL_START
483 explicit_bzero (a.buf1 + 9, 2);
484 CHK_FAIL_END
486 CHK_FAIL_START
487 explicit_bzero (a.buf1 + 9, l0 + 2);
488 CHK_FAIL_END
490 # if __USE_FORTIFY_LEVEL >= 2
491 # define O 0
492 # else
493 # define O 1
494 # endif
496 CHK_FAIL_START
497 strcpy (a.buf1 + (O + 4), str1 + 5);
498 CHK_FAIL_END
500 CHK_FAIL_START
501 p = stpcpy (a.buf1 + (O + 8), str2);
502 CHK_FAIL_END
504 CHK_FAIL_START
505 strncpy (a.buf1 + (O + 6), "X", 4);
506 CHK_FAIL_END
508 CHK_FAIL_START
509 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
510 CHK_FAIL_END
512 # if !defined __cplusplus || defined __va_arg_pack
513 CHK_FAIL_START
514 sprintf (a.buf1 + (O + 7), "%d", num1);
515 CHK_FAIL_END
517 CHK_FAIL_START
518 snprintf (a.buf1 + (O + 7), 3, "%d", num2);
519 CHK_FAIL_END
521 CHK_FAIL_START
522 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
523 CHK_FAIL_END
524 # endif
526 memcpy (a.buf1, str1 + (3 - O), 8 + O);
527 CHK_FAIL_START
528 strcat (a.buf1, "AB");
529 CHK_FAIL_END
531 memcpy (a.buf1, str1 + (4 - O), 7 + O);
532 CHK_FAIL_START
533 strncat (a.buf1, "ZYXWV", l0 + 3);
534 CHK_FAIL_END
535 #endif
538 /* These ops can be done without runtime checking of object size. */
539 wmemcpy (wbuf, L"abcdefghij", 10);
540 wmemmove (wbuf + 1, wbuf, 9);
541 if (wmemcmp (wbuf, L"aabcdefghi", 10))
542 FAIL ();
544 if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
545 || wmemcmp (wbuf, L"aabcdabcde", 10))
546 FAIL ();
548 wmemset (wbuf + 8, L'j', 2);
549 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
550 FAIL ();
552 wcscpy (wbuf + 4, L"EDCBA");
553 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
554 FAIL ();
556 if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
557 FAIL ();
559 wcsncpy (wbuf + 6, L"X", 4);
560 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
561 FAIL ();
563 if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
564 || wmemcmp (wbuf, L"aabcEDX98", 10))
565 FAIL ();
567 if (swprintf (wbuf + 7, 3, L"64") != 2
568 || wmemcmp (wbuf, L"aabcEDX64", 10))
569 FAIL ();
571 /* These ops need runtime checking, but shouldn't __chk_fail. */
572 wmemcpy (wbuf, L"abcdefghij", l0 + 10);
573 wmemmove (wbuf + 1, wbuf, l0 + 9);
574 if (wmemcmp (wbuf, L"aabcdefghi", 10))
575 FAIL ();
577 if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
578 || wmemcmp (wbuf, L"aabcdabcde", 10))
579 FAIL ();
581 wmemset (wbuf + 8, L'j', l0 + 2);
582 if (wmemcmp (wbuf, L"aabcdabcjj", 10))
583 FAIL ();
585 wcscpy (wbuf + 4, wstr1 + 5);
586 if (wmemcmp (wbuf, L"aabcEDCBA", 10))
587 FAIL ();
589 if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
590 FAIL ();
592 wcsncpy (wbuf + 6, L"X", l0 + 4);
593 if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
594 FAIL ();
596 if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
597 || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
598 FAIL ();
600 if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
601 || wmemcmp (wbuf, L"aabcEcd98", 10))
602 FAIL ();
604 wbuf[l0 + 8] = L'\0';
605 wcscat (wbuf, L"A");
606 if (wmemcmp (wbuf, L"aabcEcd9A", 10))
607 FAIL ();
609 wbuf[l0 + 7] = L'\0';
610 wcsncat (wbuf, L"ZYXWV", l0 + 2);
611 if (wmemcmp (wbuf, L"aabcEcdZY", 10))
612 FAIL ();
614 wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
615 wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
616 if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
617 FAIL ();
619 if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
620 || wmemcmp (wa.buf1, L"aabcdabcde", 10))
621 FAIL ();
623 wmemset (wa.buf1 + 8, L'j', l0 + 2);
624 if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
625 FAIL ();
627 #if __USE_FORTIFY_LEVEL < 2
628 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
629 and sufficient GCC support, as the string operations overflow
630 from a.buf1 into a.buf2. */
631 wcscpy (wa.buf1 + 4, wstr1 + 5);
632 if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
633 FAIL ();
635 if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
636 || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
637 FAIL ();
639 wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
640 if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
641 FAIL ();
643 if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
644 || wmemcmp (wa.buf1, L"aabcEDX98", 10))
645 FAIL ();
647 wa.buf1[l0 + 8] = L'\0';
648 wcscat (wa.buf1, L"A");
649 if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
650 FAIL ();
652 wa.buf1[l0 + 7] = L'\0';
653 wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
654 if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
655 FAIL ();
657 #endif
659 #if __USE_FORTIFY_LEVEL >= 1
660 /* Now check if all buffer overflows are caught at runtime.
661 N.B. All tests involving a length parameter need to be done
662 twice: once with the length a compile-time constant, once without. */
664 CHK_FAIL_START
665 wmemcpy (wbuf + 1, L"abcdefghij", 10);
666 CHK_FAIL_END
668 CHK_FAIL_START
669 wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
670 CHK_FAIL_END
672 CHK_FAIL_START
673 wmemcpy (wbuf + 9, L"abcdefghij", 10);
674 CHK_FAIL_END
676 CHK_FAIL_START
677 wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
678 CHK_FAIL_END
680 CHK_FAIL_START
681 wmemmove (wbuf + 2, wbuf + 1, 9);
682 CHK_FAIL_END
684 CHK_FAIL_START
685 wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
686 CHK_FAIL_END
688 CHK_FAIL_START
689 wp = wmempcpy (wbuf + 6, L"abcde", 5);
690 CHK_FAIL_END
692 CHK_FAIL_START
693 wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
694 CHK_FAIL_END
696 CHK_FAIL_START
697 wmemset (wbuf + 9, L'j', 2);
698 CHK_FAIL_END
700 CHK_FAIL_START
701 wmemset (wbuf + 9, L'j', l0 + 2);
702 CHK_FAIL_END
704 CHK_FAIL_START
705 wcscpy (wbuf + 5, wstr1 + 5);
706 CHK_FAIL_END
708 CHK_FAIL_START
709 wp = wcpcpy (wbuf + 9, wstr2);
710 CHK_FAIL_END
712 CHK_FAIL_START
713 wcsncpy (wbuf + 7, L"X", 4);
714 CHK_FAIL_END
716 CHK_FAIL_START
717 wcsncpy (wbuf + 7, L"X", l0 + 4);
718 CHK_FAIL_END
720 CHK_FAIL_START
721 wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
722 CHK_FAIL_END
724 CHK_FAIL_START
725 wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
726 CHK_FAIL_END
728 CHK_FAIL_START
729 wcpncpy (wbuf + 6, L"cd", 5);
730 CHK_FAIL_END
732 CHK_FAIL_START
733 wcpncpy (wbuf + 6, L"cd", l0 + 5);
734 CHK_FAIL_END
736 wmemcpy (wbuf, wstr1 + 2, 9);
737 CHK_FAIL_START
738 wcscat (wbuf, L"AB");
739 CHK_FAIL_END
741 wmemcpy (wbuf, wstr1 + 3, 8);
742 CHK_FAIL_START
743 wcsncat (wbuf, L"ZYXWV", l0 + 3);
744 CHK_FAIL_END
746 CHK_FAIL_START
747 wmemcpy (wa.buf1 + 1, L"abcdefghij", 10);
748 CHK_FAIL_END
750 CHK_FAIL_START
751 wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
752 CHK_FAIL_END
754 CHK_FAIL_START
755 wmemmove (wa.buf1 + 2, wa.buf1 + 1, 9);
756 CHK_FAIL_END
758 CHK_FAIL_START
759 wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
760 CHK_FAIL_END
762 CHK_FAIL_START
763 wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
764 CHK_FAIL_END
766 CHK_FAIL_START
767 wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
768 CHK_FAIL_END
770 CHK_FAIL_START
771 wmemset (wa.buf1 + 9, L'j', 2);
772 CHK_FAIL_END
774 CHK_FAIL_START
775 wmemset (wa.buf1 + 9, L'j', l0 + 2);
776 CHK_FAIL_END
778 #if __USE_FORTIFY_LEVEL >= 2
779 # define O 0
780 #else
781 # define O 1
782 #endif
784 CHK_FAIL_START
785 wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
786 CHK_FAIL_END
788 CHK_FAIL_START
789 wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
790 CHK_FAIL_END
792 CHK_FAIL_START
793 wcsncpy (wa.buf1 + (O + 6), L"X", 4);
794 CHK_FAIL_END
796 CHK_FAIL_START
797 wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
798 CHK_FAIL_END
800 wmemcpy (wa.buf1, wstr1 + (3 - O), 8 + O);
801 CHK_FAIL_START
802 wcscat (wa.buf1, L"AB");
803 CHK_FAIL_END
805 wmemcpy (wa.buf1, wstr1 + (4 - O), 7 + O);
806 CHK_FAIL_START
807 wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
808 CHK_FAIL_END
809 #endif
812 /* Now checks for %n protection. */
814 /* Constant literals passed directly are always ok
815 (even with warnings about possible bugs from GCC). */
816 int n1, n2;
817 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
818 || n1 != 1 || n2 != 2)
819 FAIL ();
821 /* In this case the format string is not known at compile time,
822 but resides in read-only memory, so is ok. */
823 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
824 || n1 != 1 || n2 != 2)
825 FAIL ();
827 strcpy (buf2 + 2, "%n%s%n");
828 /* When the format string is writable and contains %n,
829 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
830 CHK_FAIL2_START
831 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
832 FAIL ();
833 CHK_FAIL2_END
835 CHK_FAIL2_START
836 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
837 FAIL ();
838 CHK_FAIL2_END
840 /* But if there is no %n, even writable format string
841 should work. */
842 buf2[6] = '\0';
843 if (sprintf (buf, buf2 + 4, str2) != 1)
844 FAIL ();
846 /* Constant literals passed directly are always ok
847 (even with warnings about possible bugs from GCC). */
848 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
849 || n1 != 7 || n2 != 14)
850 FAIL ();
852 /* In this case the format string is not known at compile time,
853 but resides in read-only memory, so is ok. */
854 if (printf (str3, str4, &n1, str5, &n2) != 14
855 || n1 != 7 || n2 != 14)
856 FAIL ();
858 strcpy (buf2 + 2, "%n%s%n");
859 /* When the format string is writable and contains %n,
860 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
861 CHK_FAIL2_START
862 if (printf (buf2, str4, &n1, str5, &n1) != 14)
863 FAIL ();
864 CHK_FAIL2_END
866 /* But if there is no %n, even writable format string
867 should work. */
868 buf2[6] = '\0';
869 if (printf (buf2 + 4, str5) != 7)
870 FAIL ();
872 FILE *fp = stdout;
874 /* Constant literals passed directly are always ok
875 (even with warnings about possible bugs from GCC). */
876 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
877 || n1 != 7 || n2 != 14)
878 FAIL ();
880 /* In this case the format string is not known at compile time,
881 but resides in read-only memory, so is ok. */
882 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
883 || n1 != 7 || n2 != 14)
884 FAIL ();
886 strcpy (buf2 + 2, "%n%s%n");
887 /* When the format string is writable and contains %n,
888 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
889 CHK_FAIL2_START
890 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
891 FAIL ();
892 CHK_FAIL2_END
894 /* But if there is no %n, even writable format string
895 should work. */
896 buf2[6] = '\0';
897 if (fprintf (fp, buf2 + 4, str5) != 7)
898 FAIL ();
900 char *my_ptr = NULL;
901 strcpy (buf2 + 2, "%n%s%n");
902 /* When the format string is writable and contains %n,
903 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
904 CHK_FAIL2_START
905 if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
906 FAIL ();
907 else
908 free (my_ptr);
909 CHK_FAIL2_END
911 struct obstack obs;
912 obstack_init (&obs);
913 CHK_FAIL2_START
914 if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
915 FAIL ();
916 CHK_FAIL2_END
917 obstack_free (&obs, NULL);
919 my_ptr = NULL;
920 if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
921 FAIL ();
922 else
923 free (my_ptr);
925 obstack_init (&obs);
926 if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
927 FAIL ();
928 obstack_free (&obs, NULL);
930 if (freopen (temp_filename, "r", stdin) == NULL)
932 puts ("could not open temporary file");
933 exit (1);
936 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
937 FAIL ();
938 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
939 FAIL ();
941 #if __USE_FORTIFY_LEVEL >= 1
942 CHK_FAIL_START
943 if (gets (buf) != buf)
944 FAIL ();
945 CHK_FAIL_END
946 #endif
948 rewind (stdin);
950 if (fgets (buf, sizeof (buf), stdin) != buf
951 || memcmp (buf, "abcdefgh\n", 10))
952 FAIL ();
953 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
954 FAIL ();
956 rewind (stdin);
958 if (fgets (buf, l0 + sizeof (buf), stdin) != buf
959 || memcmp (buf, "abcdefgh\n", 10))
960 FAIL ();
962 #if __USE_FORTIFY_LEVEL >= 1
963 CHK_FAIL_START
964 if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
965 FAIL ();
966 CHK_FAIL_END
968 CHK_FAIL_START
969 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
970 FAIL ();
971 CHK_FAIL_END
972 #endif
974 rewind (stdin);
976 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
977 || memcmp (buf, "abcdefgh\n", 10))
978 FAIL ();
979 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
980 || memcmp (buf, "ABCDEFGHI", 10))
981 FAIL ();
983 rewind (stdin);
985 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
986 || memcmp (buf, "abcdefgh\n", 10))
987 FAIL ();
989 #if __USE_FORTIFY_LEVEL >= 1
990 CHK_FAIL_START
991 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
992 FAIL ();
993 CHK_FAIL_END
995 CHK_FAIL_START
996 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
997 FAIL ();
998 CHK_FAIL_END
999 #endif
1001 rewind (stdin);
1003 if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
1004 || memcmp (buf, "abcdefgh\nA", 10))
1005 FAIL ();
1006 if (fread (buf, sizeof (buf), 1, stdin) != 1
1007 || memcmp (buf, "BCDEFGHI\na", 10))
1008 FAIL ();
1010 rewind (stdin);
1012 if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
1013 || memcmp (buf, "abcdefgh\nA", 10))
1014 FAIL ();
1015 if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
1016 || memcmp (buf, "BCDEFGHI\na", 10))
1017 FAIL ();
1019 #if __USE_FORTIFY_LEVEL >= 1
1020 CHK_FAIL_START
1021 if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
1022 FAIL ();
1023 CHK_FAIL_END
1025 CHK_FAIL_START
1026 if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
1027 FAIL ();
1028 CHK_FAIL_END
1029 #endif
1031 rewind (stdin);
1033 if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
1034 || memcmp (buf, "abcdefgh\nA", 10))
1035 FAIL ();
1036 if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
1037 || memcmp (buf, "BCDEFGHI\na", 10))
1038 FAIL ();
1040 rewind (stdin);
1042 if (fread_unlocked (buf, 1, 4, stdin) != 4
1043 || memcmp (buf, "abcdFGHI\na", 10))
1044 FAIL ();
1045 if (fread_unlocked (buf, 4, 1, stdin) != 1
1046 || memcmp (buf, "efghFGHI\na", 10))
1047 FAIL ();
1049 rewind (stdin);
1051 if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
1052 || memcmp (buf, "abcdefgh\nA", 10))
1053 FAIL ();
1054 if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
1055 || memcmp (buf, "BCDEFGHI\na", 10))
1056 FAIL ();
1058 #if __USE_FORTIFY_LEVEL >= 1
1059 CHK_FAIL_START
1060 if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
1061 FAIL ();
1062 CHK_FAIL_END
1064 CHK_FAIL_START
1065 if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
1066 FAIL ();
1067 CHK_FAIL_END
1068 #endif
1070 lseek (fileno (stdin), 0, SEEK_SET);
1072 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
1073 || memcmp (buf, "abcdefgh\n", 9))
1074 FAIL ();
1075 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
1076 || memcmp (buf, "ABCDEFGHI", 9))
1077 FAIL ();
1079 lseek (fileno (stdin), 0, SEEK_SET);
1081 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
1082 || memcmp (buf, "abcdefgh\n", 9))
1083 FAIL ();
1085 #if __USE_FORTIFY_LEVEL >= 1
1086 CHK_FAIL_START
1087 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
1088 FAIL ();
1089 CHK_FAIL_END
1091 CHK_FAIL_START
1092 if (read (fileno (stdin), buf, l0 + sizeof (buf) + 1) != sizeof (buf) + 1)
1093 FAIL ();
1094 CHK_FAIL_END
1095 #endif
1097 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
1098 != sizeof (buf) - 1
1099 || memcmp (buf, "\nABCDEFGH", 9))
1100 FAIL ();
1101 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
1102 || memcmp (buf, "abcdefgh\n", 9))
1103 FAIL ();
1104 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
1105 != sizeof (buf) - 1
1106 || memcmp (buf, "h\nABCDEFG", 9))
1107 FAIL ();
1109 #if __USE_FORTIFY_LEVEL >= 1
1110 CHK_FAIL_START
1111 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
1112 != sizeof (buf) + 1)
1113 FAIL ();
1114 CHK_FAIL_END
1116 CHK_FAIL_START
1117 if (pread (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
1118 != sizeof (buf) + 1)
1119 FAIL ();
1120 CHK_FAIL_END
1121 #endif
1123 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
1124 != sizeof (buf) - 1
1125 || memcmp (buf, "\nABCDEFGH", 9))
1126 FAIL ();
1127 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
1128 || memcmp (buf, "abcdefgh\n", 9))
1129 FAIL ();
1130 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
1131 != sizeof (buf) - 1
1132 || memcmp (buf, "h\nABCDEFG", 9))
1133 FAIL ();
1135 #if __USE_FORTIFY_LEVEL >= 1
1136 CHK_FAIL_START
1137 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
1138 != sizeof (buf) + 1)
1139 FAIL ();
1140 CHK_FAIL_END
1142 CHK_FAIL_START
1143 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
1144 != sizeof (buf) + 1)
1145 FAIL ();
1146 CHK_FAIL_END
1147 #endif
1149 if (freopen (temp_filename, "r", stdin) == NULL)
1151 puts ("could not open temporary file");
1152 exit (1);
1155 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
1157 puts ("could not seek in test file");
1158 exit (1);
1161 #if __USE_FORTIFY_LEVEL >= 1
1162 CHK_FAIL_START
1163 if (gets (buf) != buf)
1164 FAIL ();
1165 CHK_FAIL_END
1166 #endif
1168 /* Check whether missing N$ formats are detected. */
1169 CHK_FAIL2_START
1170 printf ("%3$d\n", 1, 2, 3, 4);
1171 CHK_FAIL2_END
1173 CHK_FAIL2_START
1174 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
1175 CHK_FAIL2_END
1177 CHK_FAIL2_START
1178 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
1179 CHK_FAIL2_END
1181 CHK_FAIL2_START
1182 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
1183 CHK_FAIL2_END
1185 int sp[2];
1186 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
1187 FAIL ();
1188 else
1190 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
1191 if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
1192 != strlen (sendstr))
1193 FAIL ();
1195 char recvbuf[12];
1196 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
1197 != sizeof recvbuf
1198 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1199 FAIL ();
1201 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
1202 != sizeof recvbuf - 7
1203 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1204 FAIL ();
1206 #if __USE_FORTIFY_LEVEL >= 1
1207 CHK_FAIL_START
1208 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
1209 != sizeof recvbuf)
1210 FAIL ();
1211 CHK_FAIL_END
1213 CHK_FAIL_START
1214 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
1215 != sizeof recvbuf - 3)
1216 FAIL ();
1217 CHK_FAIL_END
1218 #endif
1220 socklen_t sl;
1221 struct sockaddr_un sa_un;
1223 sl = sizeof (sa_un);
1224 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
1225 (struct sockaddr *) &sa_un, &sl)
1226 != sizeof recvbuf
1227 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
1228 FAIL ();
1230 sl = sizeof (sa_un);
1231 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
1232 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
1233 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
1234 FAIL ();
1236 #if __USE_FORTIFY_LEVEL >= 1
1237 CHK_FAIL_START
1238 sl = sizeof (sa_un);
1239 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
1240 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
1241 FAIL ();
1242 CHK_FAIL_END
1244 CHK_FAIL_START
1245 sl = sizeof (sa_un);
1246 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
1247 (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
1248 FAIL ();
1249 CHK_FAIL_END
1250 #endif
1252 close (sp[0]);
1253 close (sp[1]);
1256 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
1257 char *enddir = strchr (fname, '\0');
1258 if (mkdtemp (fname) == NULL)
1260 printf ("mkdtemp failed: %m\n");
1261 return 1;
1263 *enddir = '/';
1264 if (symlink ("bar", fname) != 0)
1265 FAIL ();
1267 char readlinkbuf[4];
1268 if (readlink (fname, readlinkbuf, 4) != 3
1269 || memcmp (readlinkbuf, "bar", 3) != 0)
1270 FAIL ();
1271 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
1272 || memcmp (readlinkbuf, "bbar", 4) != 0)
1273 FAIL ();
1275 #if __USE_FORTIFY_LEVEL >= 1
1276 CHK_FAIL_START
1277 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
1278 FAIL ();
1279 CHK_FAIL_END
1281 CHK_FAIL_START
1282 if (readlink (fname, readlinkbuf + 3, 4) != 3)
1283 FAIL ();
1284 CHK_FAIL_END
1285 #endif
1287 int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
1288 if (tmpfd < 0)
1289 FAIL ();
1291 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
1292 || memcmp (readlinkbuf, "bar", 3) != 0)
1293 FAIL ();
1294 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
1295 l0 + 3) != 3
1296 || memcmp (readlinkbuf, "bbar", 4) != 0)
1297 FAIL ();
1299 #if __USE_FORTIFY_LEVEL >= 1
1300 CHK_FAIL_START
1301 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
1302 l0 + 3) != 3)
1303 FAIL ();
1304 CHK_FAIL_END
1306 CHK_FAIL_START
1307 if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
1308 4) != 3)
1309 FAIL ();
1310 CHK_FAIL_END
1311 #endif
1313 close (tmpfd);
1315 char *cwd1 = getcwd (NULL, 0);
1316 if (cwd1 == NULL)
1317 FAIL ();
1319 char *cwd2 = getcwd (NULL, 250);
1320 if (cwd2 == NULL)
1321 FAIL ();
1323 if (cwd1 && cwd2)
1325 if (strcmp (cwd1, cwd2) != 0)
1326 FAIL ();
1328 *enddir = '\0';
1329 if (chdir (fname))
1330 FAIL ();
1332 char *cwd3 = getcwd (NULL, 0);
1333 if (cwd3 == NULL)
1334 FAIL ();
1335 if (strcmp (fname, cwd3) != 0)
1336 printf ("getcwd after chdir is '%s' != '%s',"
1337 "get{c,}wd tests skipped\n", cwd3, fname);
1338 else
1340 char getcwdbuf[sizeof fname - 3];
1342 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
1343 if (cwd4 != getcwdbuf
1344 || strcmp (getcwdbuf, fname) != 0)
1345 FAIL ();
1347 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
1348 if (cwd4 != getcwdbuf + 1
1349 || getcwdbuf[0] != fname[0]
1350 || strcmp (getcwdbuf + 1, fname) != 0)
1351 FAIL ();
1353 #if __USE_FORTIFY_LEVEL >= 1
1354 CHK_FAIL_START
1355 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
1356 != getcwdbuf + 2)
1357 FAIL ();
1358 CHK_FAIL_END
1360 CHK_FAIL_START
1361 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
1362 != getcwdbuf + 2)
1363 FAIL ();
1364 CHK_FAIL_END
1365 #endif
1367 if (getwd (getcwdbuf) != getcwdbuf
1368 || strcmp (getcwdbuf, fname) != 0)
1369 FAIL ();
1371 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
1372 || strcmp (getcwdbuf + 1, fname) != 0)
1373 FAIL ();
1375 #if __USE_FORTIFY_LEVEL >= 1
1376 CHK_FAIL_START
1377 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
1378 FAIL ();
1379 CHK_FAIL_END
1380 #endif
1383 if (chdir (cwd1) != 0)
1384 FAIL ();
1385 free (cwd3);
1388 free (cwd1);
1389 free (cwd2);
1390 *enddir = '/';
1391 if (unlink (fname) != 0)
1392 FAIL ();
1394 *enddir = '\0';
1395 if (rmdir (fname) != 0)
1396 FAIL ();
1399 #if PATH_MAX > 0
1400 char largebuf[PATH_MAX];
1401 char *realres = realpath (".", largebuf);
1402 if (realres != largebuf)
1403 FAIL ();
1405 # if __USE_FORTIFY_LEVEL >= 1
1406 CHK_FAIL_START
1407 char realbuf[1];
1408 realres = realpath (".", realbuf);
1409 if (realres != realbuf)
1410 FAIL ();
1411 CHK_FAIL_END
1412 # endif
1413 #endif
1415 if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
1417 assert (MB_CUR_MAX <= 10);
1419 /* First a simple test. */
1420 char enough[10];
1421 if (wctomb (enough, L'A') != 1)
1422 FAIL ();
1424 #if __USE_FORTIFY_LEVEL >= 1
1425 /* We know the wchar_t encoding is ISO 10646. So pick a
1426 character which has a multibyte representation which does not
1427 fit. */
1428 CHK_FAIL_START
1429 char smallbuf[2];
1430 if (wctomb (smallbuf, L'\x100') != 2)
1431 FAIL ();
1432 CHK_FAIL_END
1433 #endif
1435 mbstate_t s;
1436 memset (&s, '\0', sizeof (s));
1437 if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
1438 FAIL ();
1440 #if __USE_FORTIFY_LEVEL >= 1
1441 /* We know the wchar_t encoding is ISO 10646. So pick a
1442 character which has a multibyte representation which does not
1443 fit. */
1444 CHK_FAIL_START
1445 char smallbuf[2];
1446 if (wcrtomb (smallbuf, L'\x100', &s) != 2)
1447 FAIL ();
1448 CHK_FAIL_END
1449 #endif
1451 wchar_t wenough[10];
1452 memset (&s, '\0', sizeof (s));
1453 const char *cp = "A";
1454 if (mbsrtowcs (wenough, &cp, 10, &s) != 1
1455 || wcscmp (wenough, L"A") != 0)
1456 FAIL ();
1458 cp = "BC";
1459 if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
1460 || wcscmp (wenough, L"BC") != 0)
1461 FAIL ();
1463 #if __USE_FORTIFY_LEVEL >= 1
1464 CHK_FAIL_START
1465 wchar_t wsmallbuf[2];
1466 cp = "ABC";
1467 mbsrtowcs (wsmallbuf, &cp, 10, &s);
1468 CHK_FAIL_END
1469 #endif
1471 cp = "A";
1472 if (mbstowcs (wenough, cp, 10) != 1
1473 || wcscmp (wenough, L"A") != 0)
1474 FAIL ();
1476 cp = "DEF";
1477 if (mbstowcs (wenough, cp, l0 + 10) != 3
1478 || wcscmp (wenough, L"DEF") != 0)
1479 FAIL ();
1481 #if __USE_FORTIFY_LEVEL >= 1
1482 CHK_FAIL_START
1483 wchar_t wsmallbuf[2];
1484 cp = "ABC";
1485 mbstowcs (wsmallbuf, cp, 10);
1486 CHK_FAIL_END
1487 #endif
1489 memset (&s, '\0', sizeof (s));
1490 cp = "ABC";
1491 wcscpy (wenough, L"DEF");
1492 if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
1493 || wcscmp (wenough, L"AEF") != 0)
1494 FAIL ();
1496 cp = "IJ";
1497 if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
1498 || wcscmp (wenough, L"IEF") != 0)
1499 FAIL ();
1501 #if __USE_FORTIFY_LEVEL >= 1
1502 CHK_FAIL_START
1503 wchar_t wsmallbuf[2];
1504 cp = "ABC";
1505 mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
1506 CHK_FAIL_END
1507 #endif
1509 memset (&s, '\0', sizeof (s));
1510 const wchar_t *wcp = L"A";
1511 if (wcsrtombs (enough, &wcp, 10, &s) != 1
1512 || strcmp (enough, "A") != 0)
1513 FAIL ();
1515 wcp = L"BC";
1516 if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
1517 || strcmp (enough, "BC") != 0)
1518 FAIL ();
1520 #if __USE_FORTIFY_LEVEL >= 1
1521 CHK_FAIL_START
1522 char smallbuf[2];
1523 wcp = L"ABC";
1524 wcsrtombs (smallbuf, &wcp, 10, &s);
1525 CHK_FAIL_END
1526 #endif
1528 memset (enough, 'Z', sizeof (enough));
1529 wcp = L"EF";
1530 if (wcstombs (enough, wcp, 10) != 2
1531 || strcmp (enough, "EF") != 0)
1532 FAIL ();
1534 wcp = L"G";
1535 if (wcstombs (enough, wcp, l0 + 10) != 1
1536 || strcmp (enough, "G") != 0)
1537 FAIL ();
1539 #if __USE_FORTIFY_LEVEL >= 1
1540 CHK_FAIL_START
1541 char smallbuf[2];
1542 wcp = L"ABC";
1543 wcstombs (smallbuf, wcp, 10);
1544 CHK_FAIL_END
1545 #endif
1547 memset (&s, '\0', sizeof (s));
1548 wcp = L"AB";
1549 if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
1550 || strcmp (enough, "A") != 0)
1551 FAIL ();
1553 wcp = L"BCD";
1554 if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
1555 || strcmp (enough, "B") != 0)
1556 FAIL ();
1558 #if __USE_FORTIFY_LEVEL >= 1
1559 CHK_FAIL_START
1560 char smallbuf[2];
1561 wcp = L"ABC";
1562 wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
1563 CHK_FAIL_END
1564 #endif
1566 else
1568 puts ("cannot set locale");
1569 ret = 1;
1572 int fd = posix_openpt (O_RDWR);
1573 if (fd != -1)
1575 char enough[1000];
1576 if (ptsname_r (fd, enough, sizeof (enough)) != 0)
1577 FAIL ();
1579 #if __USE_FORTIFY_LEVEL >= 1
1580 CHK_FAIL_START
1581 char smallbuf[2];
1582 if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1583 FAIL ();
1584 CHK_FAIL_END
1585 #endif
1586 close (fd);
1589 #if PATH_MAX > 0
1590 confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
1591 # if __USE_FORTIFY_LEVEL >= 1
1592 CHK_FAIL_START
1593 char smallbuf[1];
1594 confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
1595 CHK_FAIL_END
1596 # endif
1597 #endif
1599 gid_t grpslarge[5];
1600 int ngr = getgroups (5, grpslarge);
1601 asm volatile ("" : : "r" (ngr));
1602 #if __USE_FORTIFY_LEVEL >= 1
1603 CHK_FAIL_START
1604 char smallbuf[1];
1605 ngr = getgroups (5, (gid_t *) smallbuf);
1606 asm volatile ("" : : "r" (ngr));
1607 CHK_FAIL_END
1608 #endif
1610 fd = open (_PATH_TTY, O_RDONLY);
1611 if (fd != -1)
1613 char enough[1000];
1614 if (ttyname_r (fd, enough, sizeof (enough)) != 0)
1615 FAIL ();
1617 #if __USE_FORTIFY_LEVEL >= 1
1618 CHK_FAIL_START
1619 char smallbuf[2];
1620 if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
1621 FAIL ();
1622 CHK_FAIL_END
1623 #endif
1624 close (fd);
1627 char hostnamelarge[1000];
1628 gethostname (hostnamelarge, sizeof (hostnamelarge));
1629 #if __USE_FORTIFY_LEVEL >= 1
1630 CHK_FAIL_START
1631 char smallbuf[1];
1632 gethostname (smallbuf, sizeof (hostnamelarge));
1633 CHK_FAIL_END
1634 #endif
1636 char loginlarge[1000];
1637 getlogin_r (loginlarge, sizeof (hostnamelarge));
1638 #if __USE_FORTIFY_LEVEL >= 1
1639 CHK_FAIL_START
1640 char smallbuf[1];
1641 getlogin_r (smallbuf, sizeof (loginlarge));
1642 CHK_FAIL_END
1643 #endif
1645 char domainnamelarge[1000];
1646 int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
1647 asm volatile ("" : : "r" (res));
1648 #if __USE_FORTIFY_LEVEL >= 1
1649 CHK_FAIL_START
1650 char smallbuf[1];
1651 res = getdomainname (smallbuf, sizeof (domainnamelarge));
1652 asm volatile ("" : : "r" (res));
1653 CHK_FAIL_END
1654 #endif
1656 fd_set s;
1657 FD_ZERO (&s);
1659 FD_SET (FD_SETSIZE - 1, &s);
1660 #if __USE_FORTIFY_LEVEL >= 1
1661 CHK_FAIL_START
1662 FD_SET (FD_SETSIZE, &s);
1663 CHK_FAIL_END
1665 CHK_FAIL_START
1666 FD_SET (l0 + FD_SETSIZE, &s);
1667 CHK_FAIL_END
1668 #endif
1670 FD_CLR (FD_SETSIZE - 1, &s);
1671 #if __USE_FORTIFY_LEVEL >= 1
1672 CHK_FAIL_START
1673 FD_CLR (FD_SETSIZE, &s);
1674 CHK_FAIL_END
1676 CHK_FAIL_START
1677 FD_SET (l0 + FD_SETSIZE, &s);
1678 CHK_FAIL_END
1679 #endif
1681 FD_ISSET (FD_SETSIZE - 1, &s);
1682 #if __USE_FORTIFY_LEVEL >= 1
1683 CHK_FAIL_START
1684 FD_ISSET (FD_SETSIZE, &s);
1685 CHK_FAIL_END
1687 CHK_FAIL_START
1688 FD_ISSET (l0 + FD_SETSIZE, &s);
1689 CHK_FAIL_END
1690 #endif
1692 struct pollfd fds[1];
1693 fds[0].fd = STDOUT_FILENO;
1694 fds[0].events = POLLOUT;
1695 poll (fds, 1, 0);
1696 #if __USE_FORTIFY_LEVEL >= 1
1697 CHK_FAIL_START
1698 poll (fds, 2, 0);
1699 CHK_FAIL_END
1701 CHK_FAIL_START
1702 poll (fds, l0 + 2, 0);
1703 CHK_FAIL_END
1704 #endif
1705 ppoll (fds, 1, NULL, NULL);
1706 #if __USE_FORTIFY_LEVEL >= 1
1707 CHK_FAIL_START
1708 ppoll (fds, 2, NULL, NULL);
1709 CHK_FAIL_END
1711 CHK_FAIL_START
1712 ppoll (fds, l0 + 2, NULL, NULL);
1713 CHK_FAIL_END
1714 #endif
1716 return ret;