Fix ubsan bug

This commit is contained in:
Robert Schulze 2023-11-23 18:18:57 +00:00
parent 51aaee95e3
commit aee3c9651b
No known key found for this signature in database
GPG Key ID: 26703B55FB13728A
3 changed files with 10 additions and 1 deletions

View File

@ -97,7 +97,9 @@ public:
auto value = src_data[i];
if (value < 0)
throw Exception(ErrorCodes::ARGUMENT_OUT_OF_BOUND, "Expected a non-negative integer, got: {}", std::to_string(value));
dst_data[i] = static_cast<RawReturnType>(value) - static_cast<RawReturnType>(ToDaysSinceYearZeroImpl::DAYS_BETWEEN_YEARS_0_AND_1970);
/// prevent potential signed integer overflows (aka. undefined behavior) with Date32 results
auto value_uint64 = static_cast<UInt64>(value); /// NOLINT(bugprone-signed-char-misuse,cert-str34-c)
dst_data[i] = static_cast<RawReturnType>(value_uint64 - ToDaysSinceYearZeroImpl::DAYS_BETWEEN_YEARS_0_AND_1970);
}
}
};

View File

@ -18,5 +18,8 @@
719529 719529 1970-01-02 1970-01-02 1970-01-02 1970-01-02
-- NULL handling
\N \N
-- ubsan bug
2299-12-31
2299-12-31
-- Alias
1973-10-01

View File

@ -33,5 +33,9 @@ SELECT toUInt64(719529) AS u, toInt64(719529) AS s, fromDaysSinceYearZero(u), fr
SELECT '-- NULL handling';
SELECT fromDaysSinceYearZero(NULL), fromDaysSinceYearZero32(NULL);
SELECT '-- ubsan bugs';
SELECT fromDaysSinceYearZero32(2147483648);
SELECT fromDaysSinceYearZero32(3);
SELECT '-- Alias';
SELECT FROM_DAYS(1);