diff --git a/include/boost/regex/v5/match_results.hpp b/include/boost/regex/v5/match_results.hpp index b10585ca..7e872105 100644 --- a/include/boost/regex/v5/match_results.hpp +++ b/include/boost/regex/v5/match_results.hpp @@ -227,6 +227,10 @@ public: { if(m_is_singular && m_subs.empty()) raise_logic_error(); + + if (sub >= INT_MAX - 2 ) + return m_null; + sub += 2; if(sub < (int)m_subs.size() && (sub >= 0)) { diff --git a/include/boost/regex/v5/syntax_type.hpp b/include/boost/regex/v5/syntax_type.hpp index af66ad73..7824d2d3 100644 --- a/include/boost/regex/v5/syntax_type.hpp +++ b/include/boost/regex/v5/syntax_type.hpp @@ -19,6 +19,8 @@ #ifndef BOOST_REGEX_SYNTAX_TYPE_HPP #define BOOST_REGEX_SYNTAX_TYPE_HPP +#include + namespace boost{ namespace regex_constants{ diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 19f675fe..3732c999 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -138,4 +138,5 @@ run issue153.cpp : : : "msvc:-STACK:2097152" ; run issue227.cpp ; run issue232.cpp ; run lookbehind_recursion_stress_test.cpp ; +run regex_replace_overflow.cpp ; diff --git a/test/regex_replace_overflow.cpp b/test/regex_replace_overflow.cpp new file mode 100644 index 00000000..300b8d1b --- /dev/null +++ b/test/regex_replace_overflow.cpp @@ -0,0 +1,29 @@ +#include + +#include +#include +#include +#include + +#include + +int main() { + std::string format_string = "$2$2147483647"; + boost::regex e2("(<)|(>)|(&)|\\r"); + + std::string in = + "#include " + "" + "int main() { std::cout << \"Hello, world!\\n\"; }"; + + std::ostringstream t( std::ios::out | std::ios::binary ); + std::ostream_iterator oi( t ); + + boost::regex_replace(oi, in.begin(), in.end(), e2, format_string, + boost::match_default | boost::format_all); + + std::string s(t.str()); + + BOOST_TEST(!s.empty()); + return boost::report_errors(); +}