From 3639e62c97fe42345aa7c50e5d4d2b1c0b7ffef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20M=C3=BCller?= Date: Thu, 1 May 2025 19:44:07 +0200 Subject: [PATCH 1/2] ``: Fix quadratic complexity in `regex_search` when regex starts with `?` quantifier or several alternatives --- stl/inc/regex | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/stl/inc/regex b/stl/inc/regex index b2023ebd87c..4ae9d15d7ae 100644 --- a/stl/inc/regex +++ b/stl/inc/regex @@ -3931,23 +3931,22 @@ _BidIt _Matcher<_BidIt, _Elem, _RxTraits, _It>::_Skip(_BidIt _First_arg, _BidIt break; case _N_if: - { // check for soonest string match + { + // GH-5452: If this node has two or more branches, + // examining all these branches has quadratic worst-case complexity. + // Thus, we only continue if this node has a single branch only. _Node_if* _Node = static_cast<_Node_if*>(_Nx); - for (; _First_arg != _Last && _Node; _Node = _Node->_Child) { - _Last = _Skip(_First_arg, _Last, _Node->_Next); + if (_Node->_Child != nullptr) { + return _First_arg; } - - return _Last; + break; } case _N_begin: break; case _N_end: - _Nx = nullptr; - break; - case _N_none: case _N_wbound: case _N_dot: From 776a8ee238f12127111daa8297898a3968d9dd94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20M=C3=BCller?= Date: Thu, 1 May 2025 21:00:48 +0200 Subject: [PATCH 2/2] remove nullptr --- stl/inc/regex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/regex b/stl/inc/regex index 4ae9d15d7ae..987117334cd 100644 --- a/stl/inc/regex +++ b/stl/inc/regex @@ -3937,7 +3937,7 @@ _BidIt _Matcher<_BidIt, _Elem, _RxTraits, _It>::_Skip(_BidIt _First_arg, _BidIt // Thus, we only continue if this node has a single branch only. _Node_if* _Node = static_cast<_Node_if*>(_Nx); - if (_Node->_Child != nullptr) { + if (_Node->_Child) { return _First_arg; } break;