From 9048add999a4e77420fee949f743b9aca0f0bac3 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 10 Dec 2023 07:26:45 -0800 Subject: [PATCH] Report out-of-range errors in chrono --- include/fmt/chrono.h | 8 ++++---- test/chrono-test.cc | 17 +++++++++++------ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 57cd0b70..bed584b6 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -1049,10 +1049,10 @@ inline void tzset_once() { // Converts value to Int and checks that it's in the range [0, upper). template ::value)> inline Int to_nonnegative_int(T value, Int upper) { - FMT_ASSERT(std::is_unsigned::value || - (value >= 0 && to_unsigned(value) <= to_unsigned(upper)), - "invalid value"); - (void)upper; + if (!std::is_unsigned::value && + (value < 0 || to_unsigned(value) > to_unsigned(upper))) { + FMT_THROW(fmt::format_error("chrono value is out of range")); + } return static_cast(value); } template ::value)> diff --git a/test/chrono-test.cc b/test/chrono-test.cc index cb672816..56fbd382 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -740,8 +740,8 @@ TEST(chrono_test, special_durations) { EXPECT_EQ(fmt::format("{:%T}", std::chrono::duration{2}), "03:33:20"); EXPECT_EQ("01.234", - fmt::format("{:.3%S}", std::chrono::duration( - 1.234e12))); + fmt::format("{:.3%S}", + std::chrono::duration(1.234e12))); } TEST(chrono_test, unsigned_duration) { @@ -864,18 +864,18 @@ TEST(chrono_test, timestamps_ratios) { EXPECT_EQ(fmt::format("{:%M:%S}", t1), "01:07.890"); - std::chrono::time_point - t2(std::chrono::minutes(7)); + std::chrono::time_point t2( + std::chrono::minutes(7)); EXPECT_EQ(fmt::format("{:%M:%S}", t2), "07:00"); - std::chrono::time_point>> t3(std::chrono::duration>(7)); EXPECT_EQ(fmt::format("{:%M:%S}", t3), "01:03"); - std::chrono::time_point>> t4(std::chrono::duration>(1)); @@ -1023,3 +1023,8 @@ TEST(chrono_test, glibc_extensions) { EXPECT_EQ(fmt::format("{:%-S}", d), "3.140000"); } } + +TEST(chrono_test, out_of_range) { + auto d = std::chrono::duration(538976288); + EXPECT_THROW((void)fmt::format("{:%j}", d), fmt::format_error); +} \ No newline at end of file