From ae8de8259f9066f4890cdb09321f7f6d15a7df91 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 22 Jan 2021 23:40:04 +0300 Subject: [PATCH] Fix UBSan report in substring --- src/Functions/substring.cpp | 12 ++++++++---- .../0_stateless/01665_substring_ubsan.reference | 7 +++++++ tests/queries/0_stateless/01665_substring_ubsan.sql | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 tests/queries/0_stateless/01665_substring_ubsan.reference create mode 100644 tests/queries/0_stateless/01665_substring_ubsan.sql diff --git a/src/Functions/substring.cpp b/src/Functions/substring.cpp index cc2e1c70fd5..3fe82b5e0b0 100644 --- a/src/Functions/substring.cpp +++ b/src/Functions/substring.cpp @@ -91,9 +91,11 @@ public: if (column_start_const) { if (start_value > 0) - sliceFromLeftConstantOffsetUnbounded(source, StringSink(*col_res, input_rows_count), start_value - 1); + sliceFromLeftConstantOffsetUnbounded( + source, StringSink(*col_res, input_rows_count), static_cast(start_value - 1)); else if (start_value < 0) - sliceFromRightConstantOffsetUnbounded(source, StringSink(*col_res, input_rows_count), -start_value); + sliceFromRightConstantOffsetUnbounded( + source, StringSink(*col_res, input_rows_count), -static_cast(start_value)); else throw Exception("Indices in strings are 1-based", ErrorCodes::ZERO_ARRAY_OR_TUPLE_INDEX); } @@ -105,9 +107,11 @@ public: if (column_start_const && column_length_const) { if (start_value > 0) - sliceFromLeftConstantOffsetBounded(source, StringSink(*col_res, input_rows_count), start_value - 1, length_value); + sliceFromLeftConstantOffsetBounded( + source, StringSink(*col_res, input_rows_count), static_cast(start_value - 1), length_value); else if (start_value < 0) - sliceFromRightConstantOffsetBounded(source, StringSink(*col_res, input_rows_count), -start_value, length_value); + sliceFromRightConstantOffsetBounded( + source, StringSink(*col_res, input_rows_count), -static_cast(start_value), length_value); else throw Exception("Indices in strings are 1-based", ErrorCodes::ZERO_ARRAY_OR_TUPLE_INDEX); } diff --git a/tests/queries/0_stateless/01665_substring_ubsan.reference b/tests/queries/0_stateless/01665_substring_ubsan.reference new file mode 100644 index 00000000000..9e5f5f8a47c --- /dev/null +++ b/tests/queries/0_stateless/01665_substring_ubsan.reference @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tests/queries/0_stateless/01665_substring_ubsan.sql b/tests/queries/0_stateless/01665_substring_ubsan.sql new file mode 100644 index 00000000000..670998aee31 --- /dev/null +++ b/tests/queries/0_stateless/01665_substring_ubsan.sql @@ -0,0 +1 @@ +SELECT substringUTF8(materialize(''), -9223372036854775808) FROM numbers(7);