From 3f3dbe82eef928e437b3730c91e231f7211c0e10 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Mon, 12 Aug 2024 13:11:35 +0000 Subject: [PATCH 1/2] Backport #68098 to 23.8: Fix UB in hopEnd, hopStart, tumbleEnd, and tumbleStart --- src/Functions/FunctionsTimeWindow.cpp | 14 ++++++++++++-- .../01049_window_view_window_functions.reference | 4 ++++ .../01049_window_view_window_functions.sql | 5 +++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Functions/FunctionsTimeWindow.cpp b/src/Functions/FunctionsTimeWindow.cpp index 231e8b6fa77..dd8a45230c3 100644 --- a/src/Functions/FunctionsTimeWindow.cpp +++ b/src/Functions/FunctionsTimeWindow.cpp @@ -222,7 +222,12 @@ struct TimeWindowImpl { auto type = WhichDataType(arguments[0].type); if (type.isTuple()) - return std::static_pointer_cast(arguments[0].type)->getElement(0); + { + const auto & tuple_elems = std::static_pointer_cast(arguments[0].type)->getElements(); + if (tuple_elems.empty()) + throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Tuple passed to {} should not be empty", function_name); + return tuple_elems[0]; + } else if (type.isUInt32()) return std::make_shared(); else @@ -594,7 +599,12 @@ struct TimeWindowImpl { auto type = WhichDataType(arguments[0].type); if (type.isTuple()) - return std::static_pointer_cast(arguments[0].type)->getElement(0); + { + const auto & tuple_elems = std::static_pointer_cast(arguments[0].type)->getElements(); + if (tuple_elems.empty()) + throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Tuple passed to {} should not be empty", function_name); + return tuple_elems[0]; + } else if (type.isUInt32()) return std::make_shared(); else diff --git a/tests/queries/0_stateless/01049_window_view_window_functions.reference b/tests/queries/0_stateless/01049_window_view_window_functions.reference index 2d49664b280..073301104d2 100644 --- a/tests/queries/0_stateless/01049_window_view_window_functions.reference +++ b/tests/queries/0_stateless/01049_window_view_window_functions.reference @@ -67,3 +67,7 @@ SELECT toDateTime(hopEnd(toDateTime('2020-01-09 12:00:01', 'US/Samoa'), INTERVAL 2020-01-10 00:00:00 SELECT hopEnd(hop(toDateTime('2019-01-09 12:00:01', 'US/Samoa'), INTERVAL '1' DAY, INTERVAL '3' DAY, 'US/Samoa')); 2019-01-10 00:00:00 +SELECT hopStart(tuple()); -- { serverError ILLEGAL_COLUMN } +SELECT hopEnd(tuple()); -- { serverError ILLEGAL_COLUMN } +SELECT tumbleStart(tuple()); -- { serverError ILLEGAL_COLUMN } +SELECT tumbleEnd(tuple()); -- { serverError ILLEGAL_COLUMN } diff --git a/tests/queries/0_stateless/01049_window_view_window_functions.sql b/tests/queries/0_stateless/01049_window_view_window_functions.sql index 617019bd2c6..fb2b4b4949a 100644 --- a/tests/queries/0_stateless/01049_window_view_window_functions.sql +++ b/tests/queries/0_stateless/01049_window_view_window_functions.sql @@ -36,3 +36,8 @@ SELECT hopEnd(toDateTime('2020-01-09 12:00:01', 'US/Samoa'), INTERVAL '1' DAY, I SELECT toDateTime(hopEnd(toDateTime('2020-01-09 12:00:01', 'US/Samoa'), INTERVAL '1' DAY, INTERVAL '3' DAY, 'US/Samoa'), 'US/Samoa'); SELECT toDateTime(hopEnd(toDateTime('2020-01-09 12:00:01', 'US/Samoa'), INTERVAL '1' DAY, INTERVAL '3' DAY, 'US/Samoa'), 'US/Samoa'); SELECT hopEnd(hop(toDateTime('2019-01-09 12:00:01', 'US/Samoa'), INTERVAL '1' DAY, INTERVAL '3' DAY, 'US/Samoa')); + +SELECT hopStart(tuple()); -- { serverError ILLEGAL_COLUMN } +SELECT hopEnd(tuple()); -- { serverError ILLEGAL_COLUMN } +SELECT tumbleStart(tuple()); -- { serverError ILLEGAL_COLUMN } +SELECT tumbleEnd(tuple()); -- { serverError ILLEGAL_COLUMN } From aba08a9162e33b772735194248fb7720bd3a36dd Mon Sep 17 00:00:00 2001 From: Nikita Taranov Date: Mon, 9 Sep 2024 10:51:10 +0100 Subject: [PATCH 2/2] fix test --- .../01049_window_view_window_functions.reference | 8 ++++---- .../0_stateless/01049_window_view_window_functions.sql | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/queries/0_stateless/01049_window_view_window_functions.reference b/tests/queries/0_stateless/01049_window_view_window_functions.reference index 073301104d2..ab33ab46f06 100644 --- a/tests/queries/0_stateless/01049_window_view_window_functions.reference +++ b/tests/queries/0_stateless/01049_window_view_window_functions.reference @@ -67,7 +67,7 @@ SELECT toDateTime(hopEnd(toDateTime('2020-01-09 12:00:01', 'US/Samoa'), INTERVAL 2020-01-10 00:00:00 SELECT hopEnd(hop(toDateTime('2019-01-09 12:00:01', 'US/Samoa'), INTERVAL '1' DAY, INTERVAL '3' DAY, 'US/Samoa')); 2019-01-10 00:00:00 -SELECT hopStart(tuple()); -- { serverError ILLEGAL_COLUMN } -SELECT hopEnd(tuple()); -- { serverError ILLEGAL_COLUMN } -SELECT tumbleStart(tuple()); -- { serverError ILLEGAL_COLUMN } -SELECT tumbleEnd(tuple()); -- { serverError ILLEGAL_COLUMN } +SELECT hopStart(tuple()); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } +SELECT hopEnd(tuple()); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } +SELECT tumbleStart(tuple()); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } +SELECT tumbleEnd(tuple()); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } diff --git a/tests/queries/0_stateless/01049_window_view_window_functions.sql b/tests/queries/0_stateless/01049_window_view_window_functions.sql index fb2b4b4949a..abb4eb30e37 100644 --- a/tests/queries/0_stateless/01049_window_view_window_functions.sql +++ b/tests/queries/0_stateless/01049_window_view_window_functions.sql @@ -37,7 +37,7 @@ SELECT toDateTime(hopEnd(toDateTime('2020-01-09 12:00:01', 'US/Samoa'), INTERVAL SELECT toDateTime(hopEnd(toDateTime('2020-01-09 12:00:01', 'US/Samoa'), INTERVAL '1' DAY, INTERVAL '3' DAY, 'US/Samoa'), 'US/Samoa'); SELECT hopEnd(hop(toDateTime('2019-01-09 12:00:01', 'US/Samoa'), INTERVAL '1' DAY, INTERVAL '3' DAY, 'US/Samoa')); -SELECT hopStart(tuple()); -- { serverError ILLEGAL_COLUMN } -SELECT hopEnd(tuple()); -- { serverError ILLEGAL_COLUMN } -SELECT tumbleStart(tuple()); -- { serverError ILLEGAL_COLUMN } -SELECT tumbleEnd(tuple()); -- { serverError ILLEGAL_COLUMN } +SELECT hopStart(tuple()); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } +SELECT hopEnd(tuple()); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } +SELECT tumbleStart(tuple()); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } +SELECT tumbleEnd(tuple()); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH }