1 /* Testcase for BZ 30932.
2 Copyright (C) 2023-2025 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
24 #include <sys/resource.h>
27 #include <support/check.h>
28 #include <support/support.h>
29 #include <support/temp_file.h>
34 const char *str2
= "F";
43 if (getrlimit (RLIMIT_NOFILE
, &rl
) == -1)
44 FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");
46 max_fd
= (rl
.rlim_cur
< max_fd
? rl
.rlim_cur
: max_fd
);
49 if (setrlimit (RLIMIT_NOFILE
, &rl
) == 1)
50 FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
52 /* Exhaust the file descriptor limit with temporary files. */
54 for (; nfiles
< max_fd
; nfiles
++)
56 int fd
= create_temp_file ("tst-sprintf-fortify-rdonly-.", NULL
);
60 FAIL_EXIT1 ("create_temp_file: %m");
64 TEST_VERIFY_EXIT (nfiles
!= 0);
66 /* When the format string is writable and contains %n,
67 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. However, if libc can not
68 open procfs to check if the input format string in within a writable
69 memory segment, the fortify version can not perform the check. */
74 strcpy (buf2
+ 2, "%n%s%n");
75 if (sprintf (buf
, buf2
, str2
, &n1
, str2
, &n2
) != 2
76 || n1
!= 1 || n2
!= 2)
77 FAIL_EXIT1 ("sprintf failed: %s %d %d", buf
, n1
, n2
);
82 #include <support/test-driver.c>