Merge pull request #5645 from coraxster/decimal-overflow-2

Fix float to decimal convert overflow, extreme values
This commit is contained in:
Artem Zuikov 2019-06-19 14:29:33 +03:00 committed by GitHub
commit 41302c9910
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -332,12 +332,12 @@ convertToDecimal(const typename FromDataType::FieldType & value, UInt32 scale)
{
static constexpr __int128 min_int128 = __int128(0x8000000000000000ll) << 64;
static constexpr __int128 max_int128 = (__int128(0x7fffffffffffffffll) << 64) + 0xffffffffffffffffll;
if (out < min_int128 || out > max_int128)
if (out <= static_cast<ToNativeType>(min_int128) || out >= static_cast<ToNativeType>(max_int128))
throw Exception("Decimal convert overflow. Float is out of Decimal range", ErrorCodes::DECIMAL_OVERFLOW);
}
else
{
if (out < std::numeric_limits<ToNativeType>::min() || out > std::numeric_limits<ToNativeType>::max())
if (out <= std::numeric_limits<ToNativeType>::min() || out >= std::numeric_limits<ToNativeType>::max())
throw Exception("Decimal convert overflow. Float is out of Decimal range", ErrorCodes::DECIMAL_OVERFLOW);
}
return out;

View File

@ -258,3 +258,11 @@ select toDecimal128(1000000000000000000000.1, 18); -- { serverError 407 }
select toDecimal32(-10000.1, 6); -- { serverError 407 }
select toDecimal64(-10000.1, 18); -- { serverError 407 }
select toDecimal128(-1000000000000000000000.1, 18); -- { serverError 407 }
select toDecimal32(2147483647.0 + 1.0, 0); -- { serverError 407 }
select toDecimal64(9223372036854775807.0, 0); -- { serverError 407 }
select toDecimal128(170141183460469231731687303715884105729.0, 0); -- { serverError 407 }
select toDecimal32(-2147483647.0 - 1.0, 0); -- { serverError 407 }
select toDecimal64(-9223372036854775807.0, 0); -- { serverError 407 }
select toDecimal128(-170141183460469231731687303715884105729.0, 0); -- { serverError 407 }