From e01579231d030c52748c261253e8be81ad1feadd Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 8 Nov 2018 08:48:56 -0800 Subject: [PATCH] Disallow leading zeros in arg-id --- include/fmt/format.h | 8 ++++++++ test/format-test.cc | 1 + 2 files changed, 9 insertions(+) diff --git a/include/fmt/format.h b/include/fmt/format.h index ccb26454..66d8df5d 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1547,6 +1547,10 @@ FMT_CONSTEXPR bool is_name_start(Char c) { template FMT_CONSTEXPR unsigned parse_nonnegative_int(Iterator &it, ErrorHandler &&eh) { assert('0' <= *it && *it <= '9'); + if (*it == '0') { + ++it; + return 0; + } unsigned value = 0; // Convert to unsigned to prevent a warning. unsigned max_int = (std::numeric_limits::max)(); @@ -1574,6 +1578,10 @@ template FMT_CONSTEXPR unsigned parse_nonnegative_int( const Char *&begin, const Char *end, ErrorHandler &&eh) { assert(begin != end && '0' <= *begin && *begin <= '9'); + if (*begin == '0') { + ++begin; + return 0; + } unsigned value = 0; // Convert to unsigned to prevent a warning. unsigned max_int = (std::numeric_limits::max)(); diff --git a/test/format-test.cc b/test/format-test.cc index 743e44cd..927eb2a8 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -750,6 +750,7 @@ TEST(FormatterTest, ArgErrors) { EXPECT_THROW_MSG(format("{?}"), format_error, "invalid format string"); EXPECT_THROW_MSG(format("{0"), format_error, "invalid format string"); EXPECT_THROW_MSG(format("{0}"), format_error, "argument index out of range"); + EXPECT_THROW_MSG(format("{00}", 42), format_error, "invalid format string"); char format_str[BUFFER_SIZE]; safe_sprintf(format_str, "{%u", INT_MAX);