fix out-of-bounds read in basic_format_args::get (#4800)

This commit is contained in:
aizu-m 2026-06-10 10:48:35 +05:30 committed by GitHub
parent e60274b29c
commit 128ba144ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View File

@ -2593,7 +2593,7 @@ template <typename Context> class basic_format_args {
FMT_CONSTEXPR auto get(int id) const -> format_arg { FMT_CONSTEXPR auto get(int id) const -> format_arg {
auto arg = format_arg(); auto arg = format_arg();
if (!is_packed()) { if (!is_packed()) {
if (id < max_size()) arg = args_[id]; if (unsigned(id) < unsigned(max_size())) arg = args_[id];
return arg; return arg;
} }
if (unsigned(id) >= detail::max_packed_args) return arg; if (unsigned(id) >= detail::max_packed_args) return arg;

View File

@ -116,6 +116,17 @@ TEST(printf_test, invalid_arg_index) {
"argument not found"); "argument not found");
} }
TEST(printf_test, zero_positional_width_precision) {
// A '0' positional index for a '*' width or precision must be rejected. Use
// enough arguments to exercise the unpacked argument storage path.
EXPECT_THROW_MSG(test_sprintf("%*0$d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16),
format_error, "argument not found");
EXPECT_THROW_MSG(test_sprintf("%.*0$d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16),
format_error, "argument not found");
}
TEST(printf_test, default_align_right) { TEST(printf_test, default_align_right) {
EXPECT_PRINTF(" 42", "%5d", 42); EXPECT_PRINTF(" 42", "%5d", 42);
EXPECT_PRINTF(" abc", "%5s", "abc"); EXPECT_PRINTF(" abc", "%5s", "abc");