Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix overflow bug when attempting to access match results #239

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/boost/regex/v5/match_results.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ class match_results
{
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))
{
Expand Down
2 changes: 2 additions & 0 deletions include/boost/regex/v5/syntax_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef BOOST_REGEX_SYNTAX_TYPE_HPP
#define BOOST_REGEX_SYNTAX_TYPE_HPP

#include <boost/regex/config.hpp>

namespace boost{
namespace regex_constants{

Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,5 @@ run issue153.cpp : : : "<toolset>msvc:<linkflags>-STACK:2097152" ;
run issue227.cpp ;
run issue232.cpp ;
run lookbehind_recursion_stress_test.cpp ;
run regex_replace_overflow.cpp ;

29 changes: 29 additions & 0 deletions test/regex_replace_overflow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <boost/regex.hpp>

#include <iostream>
#include <iterator>
#include <sstream>
#include <string>

#include <boost/core/lightweight_test.hpp>

int main() {
std::string format_string = "$2$2147483647";
boost::regex e2("(<)|(>)|(&)|\\r");

std::string in =
"#include <iostream>"
""
"int main() { std::cout << \"Hello, world!\\n\"; }";

std::ostringstream t( std::ios::out | std::ios::binary );
std::ostream_iterator<char, char> 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();
}
Loading