diff --git a/stl/inc/regex b/stl/inc/regex index b2023ebd87c..987117334cd 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) { + 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: