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