Skip to content

Commit 31f98f2

Browse files
authored
Merge pull request #239 from cmazakas/feature/overflow-fix
fix overflow bug when attempting to access match results
2 parents 7af2aad + 093e135 commit 31f98f2

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

include/boost/regex/v5/match_results.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ class match_results
227227
{
228228
if(m_is_singular && m_subs.empty())
229229
raise_logic_error();
230+
231+
if (sub >= INT_MAX - 2 )
232+
return m_null;
233+
230234
sub += 2;
231235
if(sub < (int)m_subs.size() && (sub >= 0))
232236
{

include/boost/regex/v5/syntax_type.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef BOOST_REGEX_SYNTAX_TYPE_HPP
2020
#define BOOST_REGEX_SYNTAX_TYPE_HPP
2121

22+
#include <boost/regex/config.hpp>
23+
2224
namespace boost{
2325
namespace regex_constants{
2426

test/Jamfile.v2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,5 @@ run issue153.cpp : : : "<toolset>msvc:<linkflags>-STACK:2097152" ;
138138
run issue227.cpp ;
139139
run issue232.cpp ;
140140
run lookbehind_recursion_stress_test.cpp ;
141+
run regex_replace_overflow.cpp ;
141142

test/regex_replace_overflow.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <boost/regex.hpp>
2+
3+
#include <iostream>
4+
#include <iterator>
5+
#include <sstream>
6+
#include <string>
7+
8+
#include <boost/core/lightweight_test.hpp>
9+
10+
int main() {
11+
std::string format_string = "$2$2147483647";
12+
boost::regex e2("(<)|(>)|(&)|\\r");
13+
14+
std::string in =
15+
"#include <iostream>"
16+
""
17+
"int main() { std::cout << \"Hello, world!\\n\"; }";
18+
19+
std::ostringstream t( std::ios::out | std::ios::binary );
20+
std::ostream_iterator<char, char> oi( t );
21+
22+
boost::regex_replace(oi, in.begin(), in.end(), e2, format_string,
23+
boost::match_default | boost::format_all);
24+
25+
std::string s(t.str());
26+
27+
BOOST_TEST(!s.empty());
28+
return boost::report_errors();
29+
}

0 commit comments

Comments
 (0)