diff --git a/src/Functions/intDiv.cpp b/src/Functions/intDiv.cpp index 55396b1d1c7..804696f2776 100644 --- a/src/Functions/intDiv.cpp +++ b/src/Functions/intDiv.cpp @@ -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 == -1)) { for (size_t i = 0; i < size; ++i) - c_pos[i] = -a_pos[i]; + c_pos[i] = -make_unsigned_t(a_pos[i]); /// Avoid UBSan report in signed integer overflow. return; } diff --git a/tests/queries/0_stateless/01772_intdiv_minus_one_ubsan.reference b/tests/queries/0_stateless/01772_intdiv_minus_one_ubsan.reference new file mode 100644 index 00000000000..6b764d18a4d --- /dev/null +++ b/tests/queries/0_stateless/01772_intdiv_minus_one_ubsan.reference @@ -0,0 +1,10 @@ +-9223372036854775807 +-9223372036854775808 +9223372036854775807 +9223372036854775806 +9223372036854775805 +9223372036854775804 +9223372036854775803 +9223372036854775802 +9223372036854775801 +9223372036854775800 diff --git a/tests/queries/0_stateless/01772_intdiv_minus_one_ubsan.sql b/tests/queries/0_stateless/01772_intdiv_minus_one_ubsan.sql new file mode 100644 index 00000000000..20b4f585182 --- /dev/null +++ b/tests/queries/0_stateless/01772_intdiv_minus_one_ubsan.sql @@ -0,0 +1 @@ +SELECT intDiv(toInt64(number), -1) FROM numbers(9223372036854775807, 10);