mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-27 21:31:20 +00:00
Add overflow check
This commit is contained in:
parent
1a95e5d1b4
commit
ce3161887a
@ -63,6 +63,9 @@ TEST(scan_test, read_hex) {
|
|||||||
unsigned n = 0;
|
unsigned n = 0;
|
||||||
fmt::scan("2a", "{:x}", n);
|
fmt::scan("2a", "{:x}", n);
|
||||||
EXPECT_EQ(n, 42);
|
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) {
|
TEST(scan_test, read_string) {
|
||||||
|
11
test/scan.h
11
test/scan.h
@ -499,18 +499,19 @@ auto read_hex(scan_iterator it, T& value)
|
|||||||
if (digit < 0) break;
|
if (digit < 0) break;
|
||||||
} while (it != scan_sentinel());
|
} while (it != scan_sentinel());
|
||||||
|
|
||||||
// TODO: Check overflow.
|
// Check overflow.
|
||||||
(void)num_digits;
|
if (num_digits <= (std::numeric_limits<T>::digits >> 2))
|
||||||
value = n;
|
value = n;
|
||||||
|
else
|
||||||
|
throw_format_error("number is too big");
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, FMT_ENABLE_IF(std::is_unsigned<T>::value)>
|
template <typename T, FMT_ENABLE_IF(std::is_unsigned<T>::value)>
|
||||||
auto read(scan_iterator it, T& value, const format_specs<>& specs)
|
auto read(scan_iterator it, T& value, const format_specs<>& specs)
|
||||||
-> scan_iterator {
|
-> scan_iterator {
|
||||||
if (specs.type == presentation_type::hex_lower) {
|
if (specs.type == presentation_type::hex_lower)
|
||||||
return read_hex(it, value);
|
return read_hex(it, value);
|
||||||
}
|
|
||||||
return read(it, value);
|
return read(it, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user