Skip to content

Make backstep calculation non-recursive. #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions include/boost/regex/v5/basic_regex_creator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,12 @@ template <class charT, class traits>
int basic_regex_creator<charT, traits>::calculate_backstep(re_syntax_base* state)
{
typedef typename traits::char_class_type m_type;

int result = 0;
int last_alternative_result = -1;

std::vector<std::tuple<int, re_syntax_base*>> stack;

while(state)
{
switch(state->type)
Expand All @@ -993,9 +998,28 @@ int basic_regex_creator<charT, traits>::calculate_backstep(re_syntax_base* state
}
break;
case syntax_element_endmark:
if((static_cast<re_brace*>(state)->index == -1)
if ((static_cast<re_brace*>(state)->index == -1)
|| (static_cast<re_brace*>(state)->index == -2))
return result;
{
// We've finished the calculation, check against any previous alternatives:
if (last_alternative_result >= 0)
{
if (last_alternative_result != result)
return -1;
}
else
last_alternative_result = result;

if (stack.size())
{
// Skip to next alternative and calculate that as well:
std::tie(result, state) = stack.back();
stack.pop_back();
continue;
}
else
return result;
}
break;
case syntax_element_literal:
result += static_cast<re_literal*>(state)->length;
Expand Down Expand Up @@ -1051,11 +1075,13 @@ int basic_regex_creator<charT, traits>::calculate_backstep(re_syntax_base* state
continue;
case syntax_element_alt:
{
int r1 = calculate_backstep(state->next.p);
int r2 = calculate_backstep(static_cast<re_alt*>(state)->alt.p);
if((r1 < 0) || (r1 != r2))
// Push the alternative if we haven't pushed too many already:
if(stack.size() > BOOST_REGEX_MAX_BLOCKS)
return -1;
return result + r1;
stack.push_back(std::make_tuple(result, static_cast<re_alt*>(state)->alt.p));
// and take the first one:
state = state->next.p;
continue;
}
default:
break;
Expand Down
2 changes: 2 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,5 @@ compile test_windows_defs_4.cpp ;
run issue153.cpp : : : "<toolset>msvc:<linkflags>-STACK:2097152" ;
run issue227.cpp ;
run issue232.cpp ;
run lookbehind_recursion_stress_test.cpp ;

28 changes: 28 additions & 0 deletions test/lookbehind_recursion_stress_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
*
* Copyright (c) 2024
* John Maddock
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/

#include <boost/regex.hpp>

int main()
{
std::string s("(?<=(");
s.append(1000, '|');
s += "))";
boost::regex rx(s);

s = "(?<=(a";
for (unsigned i = 0; i < 1000; ++i)
{
s += "|a";
}
s += "))";
boost::regex rx2(s);
}
Loading