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