1 /* Fortify check for wprintf.
2 Copyright (C) 2023-2025 Free Software Foundation, Inc.
3 Copyright The GNU Toolchain Authors.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
25 #include <support/support.h>
27 static volatile int chk_fail_ok
;
28 static volatile int ret
;
29 static sigjmp_buf chk_fail_buf
;
37 longjmp (chk_fail_buf
, 1);
43 static const wchar_t *wstr3
= L
"%ls%n%ls%n";
44 static const wchar_t *wstr4
= L
"Hello, ";
45 static const wchar_t *wstr5
= L
"World!\n";
46 static wchar_t wbuf2
[20] = L
"%ls";
49 do { wprintf (L"Failure on line %d\n", __LINE__); ret = 1; } while (0)
50 #define CHK_FAIL_START \
52 if (! sigsetjmp (chk_fail_buf, 1)) \
54 #define CHK_FAIL_END \
62 set_fortify_handler (handler
);
66 int orientation
= fwide (stdout
, 1);
70 /* Constant literals passed directly are always ok
71 (even with warnings about possible bugs from GCC). */
72 if (wprintf (L
"%ls%n%ls%n", wstr4
, &n1
, wstr5
, &n2
) != 14
73 || n1
!= 7 || n2
!= 14)
76 /* In this case the format string is not known at compile time,
77 but resides in read-only memory, so is ok. */
78 if (wprintf (wstr3
, wstr4
, &n1
, wstr5
, &n2
) != 14
79 || n1
!= 7 || n2
!= 14)
82 wcpcpy (&wbuf2
[3], L
"%n%ls%n");
83 /* When the format string is writable and contains %n,
84 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
86 if (wprintf (wbuf2
, wstr4
, &n1
, wstr5
, &n1
) != 14)
90 /* But if there is no %n, even writable format string
93 if (wprintf (&wbuf2
[5], wstr5
) != 7)
96 /* Check whether missing N$ formats are detected. */
98 wprintf (L
"%3$d\n", 1, 2, 3, 4);
104 #include <support/test-driver.c>