Merge pull request #16677 from ClickHouse/field-infinite-convert-to-int

Fix UBSan report when trying to convert infinite floating point number to integer
This commit is contained in:
alexey-milovidov 2020-11-05 23:20:25 +03:00 committed by GitHub
commit 6d5e852356
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <Core/DecimalFunctions.h>
#include <Core/Field.h>
#include <common/demangle.h>
#include <Common/NaNUtils.h>
class SipHash;
@ -142,6 +143,19 @@ public:
T operator() (const Float64 & x) const
{
if constexpr (!std::is_floating_point_v<T>)
{
if (!isFinite(x))
{
/// When converting to bool it's ok (non-zero converts to true, NaN including).
if (std::is_same_v<T, bool>)
return true;
/// Conversion of infinite values to integer is undefined.
throw Exception("Cannot convert infinite value to integer type", ErrorCodes::CANNOT_CONVERT_TYPE);
}
}
if constexpr (std::is_same_v<Decimal256, T>)
return Int256(x);
else

View File

@ -0,0 +1 @@
SET max_threads = nan; -- { serverError 70 }