From 1f0d95e5c33b2b679cf49f45d6bfca3d9d3808eb Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 8 May 2020 02:31:15 +0300 Subject: [PATCH] Added range check for extractGroups function --- src/Functions/extractAllGroups.cpp | 8 ++++++++ src/Functions/extractGroups.cpp | 8 ++++++++ .../01275_extract_groups_check.reference | 0 .../0_stateless/01275_extract_groups_check.sql | 14 ++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 tests/queries/0_stateless/01275_extract_groups_check.reference create mode 100644 tests/queries/0_stateless/01275_extract_groups_check.sql diff --git a/src/Functions/extractAllGroups.cpp b/src/Functions/extractAllGroups.cpp index 79da4761894..a79efe86356 100644 --- a/src/Functions/extractAllGroups.cpp +++ b/src/Functions/extractAllGroups.cpp @@ -18,6 +18,7 @@ namespace DB namespace ErrorCodes { extern const int ARGUMENT_OUT_OF_BOUND; + extern const int BAD_ARGUMENTS; } @@ -64,8 +65,15 @@ public: const auto regexp = Regexps::get(needle); const auto & re2 = regexp->getRE2(); + + if (!re2) + throw Exception("There is no groups in regexp: " + needle, ErrorCodes::BAD_ARGUMENTS); + const size_t groups_count = re2->NumberOfCapturingGroups(); + if (!groups_count) + throw Exception("There is no groups in regexp: " + needle, ErrorCodes::BAD_ARGUMENTS); + // Including 0-group, which is the whole regexp. PODArrayWithStackMemory matched_groups(groups_count + 1); diff --git a/src/Functions/extractGroups.cpp b/src/Functions/extractGroups.cpp index 73b39dd1397..882147ef664 100644 --- a/src/Functions/extractGroups.cpp +++ b/src/Functions/extractGroups.cpp @@ -18,6 +18,7 @@ namespace DB namespace ErrorCodes { extern const int ARGUMENT_OUT_OF_BOUND; + extern const int BAD_ARGUMENTS; } @@ -64,8 +65,15 @@ public: const auto regexp = Regexps::get(needle); const auto & re2 = regexp->getRE2(); + + if (!re2) + throw Exception("There is no groups in regexp: " + needle, ErrorCodes::BAD_ARGUMENTS); + const size_t groups_count = re2->NumberOfCapturingGroups(); + if (!groups_count) + throw Exception("There is no groups in regexp: " + needle, ErrorCodes::BAD_ARGUMENTS); + // Including 0-group, which is the whole regexp. PODArrayWithStackMemory matched_groups(groups_count + 1); diff --git a/tests/queries/0_stateless/01275_extract_groups_check.reference b/tests/queries/0_stateless/01275_extract_groups_check.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/01275_extract_groups_check.sql b/tests/queries/0_stateless/01275_extract_groups_check.sql new file mode 100644 index 00000000000..2dd236f2a3b --- /dev/null +++ b/tests/queries/0_stateless/01275_extract_groups_check.sql @@ -0,0 +1,14 @@ +SELECT extractGroups('hello', ''); -- { serverError 69 } +SELECT extractAllGroups('hello', ''); -- { serverError 69 } + +SELECT extractGroups('hello', ' '); -- { serverError 36 } +SELECT extractAllGroups('hello', ' '); -- { serverError 36 } + +SELECT extractGroups('hello', '\0'); -- { serverError 36 } +SELECT extractAllGroups('hello', '\0'); -- { serverError 36 } + +SELECT extractGroups('hello', 'world'); -- { serverError 36 } +SELECT extractAllGroups('hello', 'world'); -- { serverError 36 } + +SELECT extractGroups('hello', 'hello|world'); -- { serverError 36 } +SELECT extractAllGroups('hello', 'hello|world'); -- { serverError 36 }