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

source code of glibc/debug/tst-fortify.c