mirror of
https://github.com/scylladb/scylladb.git
synced 2026-04-20 16:40:35 +00:00
sstables: improve check_truncate_and_assign()
+ if (from >= std::numeric_limits<T>::max()) {
Avi explains an issue with the snippet above from the function:
This misses the case where either type is signed. At best you'd
get a compiler warning about comparing types with different
signedness, at worst a negative value can be truncated.
Signed-off-by: Raphael S. Carvalho <raphaelsc@cloudius-systems.com>
This commit is contained in:
committed by
Avi Kivity
parent
889e7c12b6
commit
f5f4b20d1b
@@ -130,10 +130,10 @@ static void check_buf_size(temporary_buffer<char>& buf, size_t expected) {
|
||||
template <typename T, typename U>
|
||||
static void check_truncate_and_assign(T& to, const U from) {
|
||||
static_assert(std::is_integral<T>::value && std::is_integral<U>::value, "T and U must be integral");
|
||||
if (from >= std::numeric_limits<T>::max()) {
|
||||
throw std::overflow_error("assigning U to T would cause an overflow");
|
||||
}
|
||||
to = from;
|
||||
if (to != from) {
|
||||
throw std::overflow_error("assigning U to T caused an overflow");
|
||||
}
|
||||
}
|
||||
|
||||
// Base parser, parses an integer type
|
||||
|
||||
Reference in New Issue
Block a user