aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Zverovich <viz@meta.com>2024-01-01 09:17:10 -0800
committerVictor Zverovich <viz@meta.com>2024-01-01 09:17:10 -0800
commitce3161887aed8e189bec195fac8993db38c3bc5a (patch)
treee8f2fe8f8140d5422d516c32544b7f9579228da0
parent1a95e5d1b41c638f5c27aaac153c1fddd366da5c (diff)
downloadfmtlib-ce3161887aed8e189bec195fac8993db38c3bc5a.tar.gz
Add overflow check
-rw-r--r--test/scan-test.cc3
-rw-r--r--test/scan.h11
2 files changed, 9 insertions, 5 deletions
diff --git a/test/scan-test.cc b/test/scan-test.cc
index 93084c1d..f0ede192 100644
--- a/test/scan-test.cc
+++ b/test/scan-test.cc
@@ -63,6 +63,9 @@ TEST(scan_test, read_hex) {
unsigned n = 0;
fmt::scan("2a", "{:x}", n);
EXPECT_EQ(n, 42);
+ auto num_digits = std::numeric_limits<unsigned>::digits / 4;
+ EXPECT_THROW_MSG(fmt::scan(fmt::format("1{:0{}}", 0, num_digits), "{:x}", n),
+ fmt::format_error, "number is too big");
}
TEST(scan_test, read_string) {
diff --git a/test/scan.h b/test/scan.h
index 0ced8f39..a68c77c3 100644
--- a/test/scan.h
+++ b/test/scan.h
@@ -499,18 +499,19 @@ auto read_hex(scan_iterator it, T& value)
if (digit < 0) break;
} while (it != scan_sentinel());
- // TODO: Check overflow.
- (void)num_digits;
- value = n;
+ // Check overflow.
+ if (num_digits <= (std::numeric_limits<T>::digits >> 2))
+ value = n;
+ else
+ throw_format_error("number is too big");
return it;
}
template <typename T, FMT_ENABLE_IF(std::is_unsigned<T>::value)>
auto read(scan_iterator it, T& value, const format_specs<>& specs)
-> scan_iterator {
- if (specs.type == presentation_type::hex_lower) {
+ if (specs.type == presentation_type::hex_lower)
return read_hex(it, value);
- }
return read(it, value);
}