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);