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