1 /* Tests for fortified sprintf with unknown buffer bounds (bug 30039).
2 Copyright (C) 2023 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/>. */
23 #include <support/check.h>
25 /* This test is not built with _FORTIFY_SOURCE. Instead it calls the
26 appropriate implementation directly. The fortify mode is specified
28 static int fortify_mode
;
30 /* This does not handle long-double redirects etc., but we test only
31 format strings that stay within the confines of the base
33 int __vsprintf_chk (char *s
, int flag
, size_t slen
, const char *format
,
36 /* Invoke vsprintf or __vsprintf_chk according to fortify_mode. */
38 my_vsprintf (char *buf
, const char *format
, va_list ap
)
41 if (fortify_mode
== 0)
42 result
= vsprintf (buf
, format
, ap
);
44 /* Call the fortified version with an unspecified length. */
45 result
= __vsprintf_chk (buf
, fortify_mode
- 1, -1, format
, ap
);
49 /* Run one test, with the specified expected output. */
50 static void __attribute ((format (printf
, 2, 3)))
51 do_check (const char *expected
, const char *format
, ...)
54 va_start (ap
, format
);
56 char buf_expected
[24];
57 memset (buf_expected
, '@', sizeof (buf_expected
));
58 TEST_VERIFY (strlen (expected
) < sizeof (buf_expected
));
59 strcpy (buf_expected
, expected
);
61 char buf
[sizeof (buf_expected
)];
62 memset (buf
, '@', sizeof (buf
));
64 int ret
= my_vsprintf (buf
, format
, ap
);
65 TEST_COMPARE_BLOB (buf_expected
, sizeof (buf_expected
), buf
, sizeof (buf
));
66 TEST_COMPARE (ret
, strlen (expected
));
71 /* Run the tests in all fortify modes. */
75 for (fortify_mode
= 0; fortify_mode
<= 3; ++fortify_mode
)
77 do_check ("0", "%d", 0);
78 do_check ("-2147483648", "%d", -2147483647 - 1);
79 do_check ("-9223372036854775808", "%lld", -9223372036854775807LL - 1);
80 do_check ("", "%s", "");
81 do_check (" ", "%22s", "");
82 do_check ("XXXXXXXXXXXXXXXXXXXXXX", "%s", "XXXXXXXXXXXXXXXXXXXXXX");
83 do_check ("1.125000", "%f", 1.125);
84 do_check ("1.125", "%g", 1.125);
85 do_check ("1.125", "%.8g", 1.125);
89 /* printf callback that falls back to the glibc-supplied
92 dummy_printf_function (FILE *__stream
,
93 const struct printf_info
*__info
,
94 const void *const *__args
)
96 return -2; /* Request fallback. */
99 /* Likewise for the type information. */
101 dummy_arginfo_function (const struct printf_info
*info
,
102 size_t n
, int *argtypes
, int *size
)
104 return -1; /* Request fallback. */
112 /* Activate __printf_function_invoke mode. */
113 register_printf_specifier ('d', dummy_printf_function
,
114 dummy_arginfo_function
);
115 register_printf_specifier ('g', dummy_printf_function
,
116 dummy_arginfo_function
);
117 register_printf_specifier ('s', dummy_printf_function
,
118 dummy_arginfo_function
);
120 /* Rerun the tests with callback functions. */
126 #include <support/test-driver.c>