From ce2025676f71bc915807b57bb0ce65845495fc71 Mon Sep 17 00:00:00 2001 From: Francisco Javier Jurado Moreno <9376816+Beetelbrox@users.noreply.github.com> Date: Sat, 25 May 2024 20:25:37 +0200 Subject: [PATCH] add check for result being too big to be represented in output --- src/Functions/fromReadableSize.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Functions/fromReadableSize.cpp b/src/Functions/fromReadableSize.cpp index 428ee769d9c..685099b2398 100644 --- a/src/Functions/fromReadableSize.cpp +++ b/src/Functions/fromReadableSize.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,6 +7,7 @@ #include #include #include +#include "base/types.h" namespace DB { @@ -40,6 +42,8 @@ namespace {"pb", 1000000000000000}, // 10e15 {"eb", 1000000000000000000}, // 10e18 }; + + constexpr UInt64 MAX_UINT64 = std::numeric_limits::max(); class FunctionFromReadableSize : public IFunction { @@ -174,9 +178,19 @@ namespace throw Exception( ErrorCodes::BAD_ARGUMENTS, "Invalid expression for function {}, parse unit failed: \"{}\".", getName(), unit); } - // Due to a pontentially limited precision on the input value we might end up with a non-integer amount of bytes when parsing binary units. + Float64 raw_num_bytes = base * iter->second; + if (raw_num_bytes > MAX_UINT64) + { + throw Exception( + ErrorCodes::BAD_ARGUMENTS, + "Invalid expression for function {}, result is too big for output data type (UInt64): \"{}\".", + getName(), + raw_num_bytes + ); + } + // As the input might be an arbitrary decimal number we might end up with a non-integer amount of bytes when parsing binary (eg MiB) units. // This doesn't make sense so we round up to indicate the byte size that can fit the passed size. - result = static_cast(std::ceil(base * iter->second)); + result = static_cast(std::ceil(raw_num_bytes)); res_data.emplace_back(result); }