Fix UBSan report in intDiv #21769

This commit is contained in:
Alexey Milovidov 2021-03-23 00:38:25 +03:00
parent 49cdd5e434
commit c94841bca5
3 changed files with 12 additions and 2 deletions

View File

@ -49,11 +49,10 @@ struct DivideIntegralByConstantImpl
#pragma GCC diagnostic ignored "-Wsign-compare"
/// Division by -1. By the way, we avoid FPE by division of the largest negative number by -1.
/// And signed integer overflow is well defined in C++20.
if (unlikely(is_signed_v<B> && b == -1))
{
for (size_t i = 0; i < size; ++i)
c_pos[i] = -a_pos[i];
c_pos[i] = -make_unsigned_t<A>(a_pos[i]); /// Avoid UBSan report in signed integer overflow.
return;
}

View File

@ -0,0 +1,10 @@
-9223372036854775807
-9223372036854775808
9223372036854775807
9223372036854775806
9223372036854775805
9223372036854775804
9223372036854775803
9223372036854775802
9223372036854775801
9223372036854775800

View File

@ -0,0 +1 @@
SELECT intDiv(toInt64(number), -1) FROM numbers(9223372036854775807, 10);