Improve name argument validation

This commit is contained in:
Victor Zverovich 2024-01-19 16:06:06 -08:00
parent 2eb363297b
commit 4c5b4af04d
4 changed files with 13 additions and 6 deletions

View File

@ -772,7 +772,9 @@ template <typename Char> class basic_format_parse_context {
next_arg_id_ = -1;
do_check_arg_id(id);
}
FMT_CONSTEXPR void check_arg_id(basic_string_view<Char>) {}
FMT_CONSTEXPR void check_arg_id(basic_string_view<Char>) {
next_arg_id_ = -1;
}
FMT_CONSTEXPR void check_dynamic_spec(int arg_id);
};

View File

@ -4264,9 +4264,11 @@ void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
return parse_context.next_arg_id();
}
FMT_CONSTEXPR auto on_arg_id(int id) -> int {
return parse_context.check_arg_id(id), id;
parse_context.check_arg_id(id);
return id;
}
FMT_CONSTEXPR auto on_arg_id(basic_string_view<Char> id) -> int {
parse_context.check_arg_id(id);
int arg_id = context.arg_id(id);
if (arg_id < 0) report_error("argument not found");
return arg_id;

View File

@ -144,9 +144,9 @@ TEST(args_test, reserve) {
fmt::dynamic_format_arg_store<fmt::format_context> store;
store.reserve(2, 1);
store.push_back(1.5f);
store.push_back(fmt::arg("a1", 42));
auto result = fmt::vformat("{a1} and {}", store);
EXPECT_EQ("42 and 1.5", result);
store.push_back(fmt::arg("a", 42));
auto result = fmt::vformat("{} and {a}", store);
EXPECT_EQ("1.5 and 42", result);
}
struct copy_throwable {

View File

@ -555,6 +555,9 @@ TEST(format_test, named_arg) {
"argument not found");
EXPECT_THROW_MSG((void)fmt::format(runtime("{a}"), 42), format_error,
"argument not found");
EXPECT_THROW_MSG((void)fmt::format(runtime("{a} {}"), fmt::arg("a", 2), 42),
format_error,
"cannot switch from manual to automatic argument indexing");
}
TEST(format_test, auto_arg_index) {
@ -1742,7 +1745,7 @@ TEST(format_test, print) {
}
TEST(format_test, big_print) {
enum {count = 5000};
enum { count = 5000 };
auto big_print = []() {
for (int i = 0; i < count / 5; ++i) fmt::print("xxxxx");
};