Merge pull request #20458 from ClickHouse/array-difference-ubsan

Fix UBSan report in arrayDifference
This commit is contained in:
alexey-milovidov 2021-02-13 19:34:44 +03:00 committed by GitHub
commit 3e9f1dc83b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 7 deletions

View File

@ -47,6 +47,29 @@ struct ArrayDifferenceImpl
}
template <typename Element, typename Result>
static void NO_SANITIZE_UNDEFINED impl(const Element * __restrict src, Result * __restrict dst, size_t begin, size_t end)
{
/// First element is zero, then the differences of ith and i-1th elements.
Element prev{};
for (size_t pos = begin; pos < end; ++pos)
{
if (pos == begin)
{
dst[pos] = 0;
prev = src[pos];
}
else
{
Element curr = src[pos];
dst[pos] = curr - prev;
prev = curr;
}
}
}
template <typename Element, typename Result>
static bool executeType(const ColumnPtr & mapped, const ColumnArray & array, ColumnPtr & res_ptr)
{
@ -73,14 +96,10 @@ struct ArrayDifferenceImpl
size_t pos = 0;
for (auto offset : offsets)
{
// skip empty arrays
if (pos < offset)
{
res_values[pos] = 0;
for (++pos; pos < offset; ++pos)
res_values[pos] = static_cast<Result>(data[pos]) - static_cast<Result>(data[pos - 1]);
}
impl(data.data(), res_values.data(), pos, offset);
pos = offset;
}
res_ptr = ColumnArray::create(std::move(res_nested), array.getOffsetsPtr());
return true;
}

View File

@ -0,0 +1 @@
[0,9223372036854710272]

View File

@ -0,0 +1,2 @@
-- Overflow is Ok and behaves as the CPU does it.
SELECT arrayDifference([65536, -9223372036854775808]);