Skip to content

Commit 70c3ffa

Browse files
authored
Merge pull request boostorg#98 from boostorg/issue_75
Remove limit on the number of backrefs possible.
2 parents f80a3ab + deb9104 commit 70c3ffa

File tree

7 files changed

+81
-20
lines changed

7 files changed

+81
-20
lines changed

include/boost/regex/v4/basic_regex.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ void bubble_down_one(I first, I last)
7070
}
7171
}
7272

73+
static const int hash_value_mask = 1 << (std::numeric_limits<int>::digits - 1);
74+
7375
template <class Iterator>
7476
inline int hash_value_from_capture_name(Iterator i, Iterator j)
7577
{
7678
std::size_t r = boost::hash_range(i, j);
77-
r %= ((std::numeric_limits<int>::max)() - 10001);
78-
r += 10000;
79-
return static_cast<int>(r);
79+
r %= ((std::numeric_limits<int>::max)());
80+
return static_cast<int>(r) | hash_value_mask;
8081
}
8182

8283
class named_subexpressions

include/boost/regex/v4/basic_regex_creator.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#ifndef BOOST_REGEX_V4_BASIC_REGEX_CREATOR_HPP
2121
#define BOOST_REGEX_V4_BASIC_REGEX_CREATOR_HPP
2222

23+
#include <boost/regex/v4/indexed_bit_flag.hpp>
24+
2325
#ifdef BOOST_MSVC
2426
#pragma warning(push)
2527
#pragma warning(disable: 4103)
@@ -239,7 +241,7 @@ class basic_regex_creator
239241
bool m_icase; // true for case insensitive matches
240242
unsigned m_repeater_id; // the state_id of the next repeater
241243
bool m_has_backrefs; // true if there are actually any backrefs
242-
unsigned m_backrefs; // bitmask of permitted backrefs
244+
indexed_bit_flag m_backrefs; // bitmask of permitted backrefs
243245
boost::uintmax_t m_bad_repeats; // bitmask of repeats we can't deduce a startmap for;
244246
bool m_has_recursions; // set when we have recursive expresisons to fixup
245247
std::vector<unsigned char> m_recursion_checks; // notes which recursions we've followed while analysing this expression
@@ -267,7 +269,7 @@ class basic_regex_creator
267269

268270
template <class charT, class traits>
269271
basic_regex_creator<charT, traits>::basic_regex_creator(regex_data<charT, traits>* data)
270-
: m_pdata(data), m_traits(*(data->m_ptraits)), m_last_state(0), m_repeater_id(0), m_has_backrefs(false), m_backrefs(0), m_has_recursions(false)
272+
: m_pdata(data), m_traits(*(data->m_ptraits)), m_last_state(0), m_repeater_id(0), m_has_backrefs(false), m_has_recursions(false)
271273
{
272274
m_pdata->m_data.clear();
273275
m_pdata->m_status = ::boost::regex_constants::error_ok;
@@ -763,7 +765,7 @@ void basic_regex_creator<charT, traits>::fixup_recursions(re_syntax_base* state)
763765
if(idx < 0)
764766
{
765767
idx = -idx-1;
766-
if(idx >= 10000)
768+
if(idx >= hash_value_mask)
767769
{
768770
idx = m_pdata->get_id(idx);
769771
if(idx <= 0)
@@ -795,7 +797,7 @@ void basic_regex_creator<charT, traits>::fixup_recursions(re_syntax_base* state)
795797
bool ok = false;
796798
re_syntax_base* p = base;
797799
std::ptrdiff_t idx = static_cast<re_jump*>(state)->alt.i;
798-
if(idx > 10000)
800+
if(idx >= hash_value_mask)
799801
{
800802
//
801803
// There may be more than one capture group with this hash, just do what Perl

include/boost/regex/v4/basic_regex_parser.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,8 @@ bool basic_regex_parser<charT, traits>::parse_open_paren()
545545
//
546546
// allow backrefs to this mark:
547547
//
548-
if((markid > 0) && (markid < sizeof(unsigned) * CHAR_BIT))
549-
this->m_backrefs |= 1u << (markid - 1);
548+
if(markid > 0)
549+
this->m_backrefs.set(markid);
550550

551551
return true;
552552
}
@@ -912,7 +912,7 @@ bool basic_regex_parser<charT, traits>::parse_extended_escape()
912912
}
913913
if(negative)
914914
i = 1 + m_mark_count - i;
915-
if(((i > 0) && (i < std::numeric_limits<unsigned>::digits) && (i - 1 < static_cast<boost::intmax_t>(sizeof(unsigned) * CHAR_BIT)) && (this->m_backrefs & (1u << (i-1)))) || ((i > 10000) && (this->m_pdata->get_id(i) > 0) && (this->m_pdata->get_id(i)-1 < static_cast<boost::intmax_t>(sizeof(unsigned) * CHAR_BIT)) && (this->m_backrefs & (1u << (this->m_pdata->get_id(i)-1)))))
915+
if(((i < hash_value_mask) && (i > 0) && (this->m_backrefs.test(i))) || ((i >= hash_value_mask) && (this->m_pdata->get_id(i) > 0) && (this->m_backrefs.test(this->m_pdata->get_id(i)))))
916916
{
917917
m_position = pc;
918918
re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
@@ -1944,7 +1944,7 @@ bool basic_regex_parser<charT, traits>::parse_backref()
19441944
charT c = unescape_character();
19451945
this->append_literal(c);
19461946
}
1947-
else if((i > 0) && (this->m_backrefs & (1u << (i-1))))
1947+
else if((i > 0) && (this->m_backrefs.test(i)))
19481948
{
19491949
m_position = pc;
19501950
re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
@@ -2718,8 +2718,7 @@ bool basic_regex_parser<charT, traits>::parse_perl_extension()
27182718
//
27192719
// allow backrefs to this mark:
27202720
//
2721-
if(markid < (int)(sizeof(unsigned) * CHAR_BIT))
2722-
this->m_backrefs |= 1u << (markid - 1);
2721+
this->m_backrefs.set(markid);
27232722
}
27242723
return true;
27252724
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
*
3+
* Copyright (c) 2020
4+
* John Maddock
5+
*
6+
* Use, modification and distribution are subject to the
7+
* Boost Software License, Version 1.0. (See accompanying file
8+
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9+
*
10+
*/
11+
12+
/*
13+
* LOCATION: see http://www.boost.org for most recent version.
14+
* FILE basic_regex_parser.cpp
15+
* VERSION see <boost/version.hpp>
16+
* DESCRIPTION: Declares template class basic_regex_parser.
17+
*/
18+
19+
#include <boost/regex/config.hpp>
20+
#include <set>
21+
22+
#ifndef BOOST_REGEX_V4_INDEXED_BIT_FLAG_HPP
23+
#define BOOST_REGEX_V4_INDEXED_BIT_FLAG_HPP
24+
25+
namespace boost{
26+
namespace BOOST_REGEX_DETAIL_NS{
27+
28+
class indexed_bit_flag
29+
{
30+
boost::uint64_t low_mask;
31+
std::set<std::size_t> mask_set;
32+
public:
33+
indexed_bit_flag() : low_mask(0) {}
34+
void set(std::size_t i)
35+
{
36+
if (i < std::numeric_limits<boost::uint64_t>::digits - 1)
37+
low_mask |= static_cast<boost::uint64_t>(1u) << i;
38+
else
39+
mask_set.insert(i);
40+
}
41+
bool test(std::size_t i)
42+
{
43+
if (i < std::numeric_limits<boost::uint64_t>::digits - 1)
44+
return low_mask & static_cast<boost::uint64_t>(1u) << i ? true : false;
45+
else
46+
return mask_set.find(i) != mask_set.end();
47+
}
48+
};
49+
50+
} // namespace BOOST_REGEX_DETAIL_NS
51+
} // namespace boost
52+
53+
54+
#endif

include/boost/regex/v4/perl_matcher_common.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ bool perl_matcher<BidiIterator, Allocator, traits>::match_backref()
609609
// or PCRE.
610610
//
611611
int index = static_cast<const re_brace*>(pstate)->index;
612-
if(index >= 10000)
612+
if(index >= hash_value_mask)
613613
{
614614
named_subexpressions::range_type r = re.get_data().equal_range(index);
615615
BOOST_ASSERT(r.first != r.second);
@@ -758,7 +758,7 @@ inline bool perl_matcher<BidiIterator, Allocator, traits>::match_assert_backref(
758758
{
759759
// Have we matched subexpression "index"?
760760
// Check if index is a hash value:
761-
if(index >= 10000)
761+
if(index >= hash_value_mask)
762762
{
763763
named_subexpressions::range_type r = re.get_data().equal_range(index);
764764
while(r.first != r.second)
@@ -782,7 +782,7 @@ inline bool perl_matcher<BidiIterator, Allocator, traits>::match_assert_backref(
782782
// Have we recursed into subexpression "index"?
783783
// If index == 0 then check for any recursion at all, otherwise for recursion to -index-1.
784784
int idx = -(index+1);
785-
if(idx >= 10000)
785+
if(idx >= hash_value_mask)
786786
{
787787
named_subexpressions::range_type r = re.get_data().equal_range(idx);
788788
int stack_index = recursion_stack.empty() ? -1 : recursion_stack.back().idx;

test/regress/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ int cpp_main(int /*argc*/, char * /*argv*/[])
139139

140140
int* get_array_data()
141141
{
142-
static boost::thread_specific_ptr<boost::array<int, 200> > tp;
142+
static boost::thread_specific_ptr<boost::array<int, 800> > tp;
143143

144144
if(tp.get() == 0)
145-
tp.reset(new boost::array<int, 200>);
145+
tp.reset(new boost::array<int, 800>);
146146

147147
return tp.get()->data();
148148
}
@@ -160,9 +160,9 @@ const int* make_array(int first, ...)
160160
#ifdef TEST_THREADS
161161
int* data = get_array_data();
162162
#else
163-
static int data[200];
163+
static int data[800];
164164
#endif
165-
std::fill_n(data, 200, -2);
165+
std::fill_n(data, 800, -2);
166166
va_list ap;
167167
va_start(ap, first);
168168
//

test/regress/test_backrefs.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,10 @@ void test_backrefs()
103103
TEST_REGEX_SEARCH("a(?'foo'(?'bar'(?'bb'(?'aa'b*))))c\\g{foo}d", perl, "abbcbbbd", match_default, make_array(-2, -2));
104104
TEST_REGEX_SEARCH("^(?'foo'.)\\g{foo}", perl, "abc", match_default, make_array(-2, -2));
105105
TEST_REGEX_SEARCH("a(?'foo'[bc])\\g{foo}d", perl, "abcdabbd", match_default, make_array(4, 8, 5, 6, -2, -2));
106+
107+
// Bug cases from https://github.com/boostorg/regex/issues/75
108+
TEST_REGEX_SEARCH("(?:(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)\\g{-1}|WORKING)", perl, "WORKING", match_default, make_array(0, 7, -2, -2));
109+
TEST_REGEX_SEARCH("(?:(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)(z)\\g{-1}|WORKING)", perl, "WORKING", match_default, make_array(0, 7, -2, -2));
110+
106111
}
107112

0 commit comments

Comments
 (0)