Skip to content

Commit d961318

Browse files
authored
Merge pull request #101 from boostorg/git_issue_87
Fix recursive expressions where the recursion appears more than once.
2 parents 574fad6 + afc4229 commit d961318

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

include/boost/regex/v4/basic_regex_creator.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ re_syntax_base* basic_regex_creator<charT, traits>::append_set(
594594
return 0;
595595
}
596596
// everything in range matches:
597-
std::memset(result->_map + static_cast<unsigned char>(c1), true, 1 + static_cast<unsigned char>(c2) - static_cast<unsigned char>(c1));
597+
std::memset(result->_map + static_cast<unsigned char>(c1), true, static_cast<unsigned char>(1u) + static_cast<unsigned char>(static_cast<unsigned char>(c2) - static_cast<unsigned char>(c1)));
598598
}
599599
}
600600
//
@@ -1070,9 +1070,21 @@ int basic_regex_creator<charT, traits>::calculate_backstep(re_syntax_base* state
10701070
return -1;
10711071
}
10721072

1073+
struct recursion_saver
1074+
{
1075+
std::vector<unsigned char> saved_state;
1076+
std::vector<unsigned char>* state;
1077+
recursion_saver(std::vector<unsigned char>* p) : saved_state(*p), state(p) {}
1078+
~recursion_saver()
1079+
{
1080+
state->swap(saved_state);
1081+
}
1082+
};
1083+
10731084
template <class charT, class traits>
10741085
void basic_regex_creator<charT, traits>::create_startmap(re_syntax_base* state, unsigned char* l_map, unsigned int* pnull, unsigned char mask)
10751086
{
1087+
recursion_saver saved_recursions(&m_recursion_checks);
10761088
int not_last_jump = 1;
10771089
re_syntax_base* recursion_start = 0;
10781090
int recursion_sub = 0;

include/boost/regex/v4/basic_regex_parser.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void basic_regex_parser<charT, traits>::parse(const charT* p1, const charT* p2,
197197
if(this->m_pdata->m_status)
198198
return;
199199
// fill in our sub-expression count:
200-
this->m_pdata->m_mark_count = 1 + m_mark_count;
200+
this->m_pdata->m_mark_count = 1u + (std::size_t)m_mark_count;
201201
this->finalize(p1, p2);
202202
}
203203

@@ -2723,7 +2723,7 @@ bool basic_regex_parser<charT, traits>::parse_perl_extension()
27232723
{
27242724
#ifndef BOOST_NO_STD_DISTANCE
27252725
if(this->flags() & regbase::save_subexpression_location)
2726-
this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position) - 1;
2726+
this->m_pdata->m_subs.at((std::size_t)markid - 1).second = std::distance(m_base, m_position) - 1;
27272727
#else
27282728
if(this->flags() & regbase::save_subexpression_location)
27292729
this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base) - 1;

include/boost/regex/v4/w32_regex_traits.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ typename w32_regex_traits_implementation<charT>::char_class_type
546546
if(pos != m_custom_class_names.end())
547547
return pos->second;
548548
}
549-
std::size_t state_id = 1 + BOOST_REGEX_DETAIL_NS::get_default_class_id(p1, p2);
549+
std::size_t state_id = 1u + (std::size_t)BOOST_REGEX_DETAIL_NS::get_default_class_id(p1, p2);
550550
if(state_id < sizeof(masks) / sizeof(masks[0]))
551551
return masks[state_id];
552552
return masks[0];

src/c_regex_traits.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ c_regex_traits<char>::char_class_type BOOST_REGEX_CALL c_regex_traits<char>::loo
157157
s[i] = static_cast<char>((std::tolower)(static_cast<unsigned char>(s[i])));
158158
idx = ::boost::BOOST_REGEX_DETAIL_NS::get_default_class_id(&*s.begin(), &*s.begin() + s.size());
159159
}
160-
BOOST_ASSERT(std::size_t(idx+1) < sizeof(masks) / sizeof(masks[0]));
160+
BOOST_ASSERT(std::size_t(idx) + 1u < sizeof(masks) / sizeof(masks[0]));
161161
return masks[idx+1];
162162
}
163163

test/regress/test_perl_ex.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ void test_recursion()
935935
TEST_REGEX_SEARCH("namespace\\s+(\\w+)\\s+(\\{(?:[^{}]*(?:(?2)[^{}]*)*)?\\})", perl, "namespace one { namespace two { int foo(){} } { {{{ } } } } {}}", match_default, make_array(0, 64, 10, 13, 14, 64, -2, -2));
936936
TEST_INVALID_REGEX("((?1)|a)", perl);
937937
TEST_REGEX_SEARCH("a(?0)?", perl, "aaaaa", match_default, make_array(0, 5, -2, -2));
938+
TEST_REGEX_SEARCH("((?(DEFINE)(?'a'A)(?'b'(?&a)?(?&a)))(?&b)?)", perl, "AA", match_default, make_array(0, 2, 0, 2, -1, -1, -2, 2, 2, 2, 2, -1, -1, -2, -2));
938939

939940
// Recursion to a named sub with a name that is used multiple times:
940941
TEST_REGEX_SEARCH("(?:(?<A>a+)|(?<A>b+))\\.(?&A)", perl, "aaaa.aa", match_default, make_array(0, 7, 0, 4, -1, -1, -2, -2));

0 commit comments

Comments
 (0)