From e07114a6e3c17df9253a352438b83f07b838a7c4 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Thu, 23 Mar 2023 12:13:51 +0800 Subject: [PATCH] fix overflow issue of readNumberWithVariableLength --- src/Functions/parseDateTime.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Functions/parseDateTime.cpp b/src/Functions/parseDateTime.cpp index 1857764557a..7799520b8e5 100644 --- a/src/Functions/parseDateTime.cpp +++ b/src/Functions/parseDateTime.cpp @@ -1035,7 +1035,7 @@ namespace int repetitions, int max_digits_to_read, const String & fragment, - Int32 & number) + Int32 & result) { bool negative = false; if (allow_negative && cur < end && *cur == '-') @@ -1049,7 +1049,7 @@ namespace ++cur; } - number = 0; + Int64 number = 0; const Pos start = cur; if (is_year && repetitions == 2) { @@ -1102,6 +1102,15 @@ namespace if (negative) number *= -1; + /// Check if number exceeds the range of Int32 + if (number < std::numeric_limits::lowest() || number > std::numeric_limits::max()) + throw Exception( + ErrorCodes::CANNOT_PARSE_DATETIME, + "Unable to parse fragment {} from {} because number is out of range of Int32", + fragment, + std::string_view(start, cur - start)); + result = static_cast(number); + return cur; }