From 1bfd4f756858ad4798b4c95f238f655b7115da20 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Mon, 19 Apr 2021 02:00:33 +0300 Subject: [PATCH] Function toDateTime decimal overflow ubsan fix --- src/Core/DecimalFunctions.h | 5 ++++- tests/queries/0_stateless/01821_to_date_time_ubsan.reference | 0 tests/queries/0_stateless/01821_to_date_time_ubsan.sql | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 tests/queries/0_stateless/01821_to_date_time_ubsan.reference create mode 100644 tests/queries/0_stateless/01821_to_date_time_ubsan.sql diff --git a/src/Core/DecimalFunctions.h b/src/Core/DecimalFunctions.h index 2cd50ab8d08..2131a6e3c33 100644 --- a/src/Core/DecimalFunctions.h +++ b/src/Core/DecimalFunctions.h @@ -103,7 +103,10 @@ inline DecimalType decimalFromComponentsWithMultiplier( if (common::mulOverflow(whole, scale_multiplier, whole_scaled)) throw Exception("Decimal math overflow", ErrorCodes::DECIMAL_OVERFLOW); - const T value = whole_scaled + fractional_sign * (fractional % scale_multiplier); + T value; + if (common::addOverflow(whole_scaled, fractional_sign * (fractional % scale_multiplier), value)) + throw Exception("Decimal math overflow", ErrorCodes::DECIMAL_OVERFLOW); + return DecimalType(value); } diff --git a/tests/queries/0_stateless/01821_to_date_time_ubsan.reference b/tests/queries/0_stateless/01821_to_date_time_ubsan.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/01821_to_date_time_ubsan.sql b/tests/queries/0_stateless/01821_to_date_time_ubsan.sql new file mode 100644 index 00000000000..74226fc221f --- /dev/null +++ b/tests/queries/0_stateless/01821_to_date_time_ubsan.sql @@ -0,0 +1,2 @@ +SELECT toDateTime('9223372036854775806', 7); -- { serverError 407 } +SELECT toDateTime('9223372036854775806', 8); -- { serverError 407 }