diff --git a/include/fmt/base.h b/include/fmt/base.h index 20cccc2b..5d0241c8 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -2593,7 +2593,7 @@ template class basic_format_args { FMT_CONSTEXPR auto get(int id) const -> format_arg { auto arg = format_arg(); if (!is_packed()) { - if (id < max_size()) arg = args_[id]; + if (unsigned(id) < unsigned(max_size())) arg = args_[id]; return arg; } if (unsigned(id) >= detail::max_packed_args) return arg; diff --git a/test/printf-test.cc b/test/printf-test.cc index f2d2a141..baf2b168 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -116,6 +116,17 @@ TEST(printf_test, invalid_arg_index) { "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) { EXPECT_PRINTF(" 42", "%5d", 42); EXPECT_PRINTF(" abc", "%5s", "abc");