Skip to content

Commit 54f580b

Browse files
committed
fix integer overflow when parsing Perl-extended named backrefs
1 parent 0b64ece commit 54f580b

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

include/boost/regex/v5/basic_regex_parser.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,20 @@ bool basic_regex_parser<charT, traits>::parse_extended_escape()
910910
pc = m_position;
911911
}
912912
if(negative)
913-
i = 1 + (static_cast<std::intmax_t>(m_mark_count) - i);
913+
{
914+
auto mark_count = static_cast<std::intmax_t>(m_mark_count);
915+
auto int_min = std::numeric_limits<std::intmax_t>::min();
916+
auto int_max = std::numeric_limits<std::intmax_t>::max();
917+
918+
if ((i < 0) && (mark_count < int_min - i)) { i = -1; }
919+
else if ((i > 0) && (mark_count > int_max - i )) { i = -1; }
920+
else
921+
{
922+
i = mark_count - i;
923+
if (i >= int_max - 1) { i = -1; }
924+
else { i += 1;}
925+
}
926+
}
914927
if(((i < hash_value_mask) && (i > 0)) || ((i >= hash_value_mask) && (this->m_pdata->get_id((int)i) > 0)))
915928
{
916929
m_position = pc;

test/Jamfile.v2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ compile test_windows_defs_4.cpp ;
137137
run issue153.cpp : : : "<toolset>msvc:<linkflags>-STACK:2097152" ;
138138
run issue227.cpp ;
139139
run issue232.cpp ;
140+
run issue245.cpp ;
140141
run lookbehind_recursion_stress_test.cpp ;
141142
run regex_replace_overflow.cpp ;
142143

test/issue245.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <boost/regex.hpp>
2+
3+
#include <vector>
4+
#include <string>
5+
6+
#include "test_macros.hpp"
7+
8+
9+
int main()
10+
{
11+
// invalid because \k-- is an unterminated token
12+
{
13+
char const strdata[] = "\\k--00000000000000000000000000000000000000000000000000000000009223372036854775807\xff\xff\xff\xff\xff\xff\xff\xef""99999999999999999999999999999999999]999999999999999\x90";
14+
std::string regex_string(strdata, strdata + sizeof(strdata) - 1);
15+
BOOST_TEST_THROWS((boost::regex(regex_string)), boost::regex_error);
16+
}
17+
{
18+
char const strdata[] = "\\k-00000000000000000000000000000000000000000000000000000000009223372036854775807\xff\xff\xff\xff\xff\xff\xff\xef""99999999999999999999999999999999999]999999999999999\x90";
19+
std::string regex_string(strdata, strdata + sizeof(strdata) - 1);
20+
BOOST_TEST_THROWS((boost::regex(regex_string)), boost::regex_error);
21+
22+
}
23+
{
24+
char const strdata[] = "\\k00000000000000000000000000000000000000000000000000000000009223372036854775807\xff\xff\xff\xff\xff\xff\xff\xef""99999999999999999999999999999999999]999999999999999\x90";
25+
std::string regex_string(strdata, strdata + sizeof(strdata) - 1);
26+
BOOST_TEST_THROWS((boost::regex(regex_string)), boost::regex_error);
27+
28+
}
29+
return boost::report_errors();
30+
}

0 commit comments

Comments
 (0)