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 }