Skip to content

fix integer overflow when parsing Perl-extended named backrefs #246

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

Merged
merged 1 commit into from
Mar 4, 2025
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
5 changes: 5 additions & 0 deletions include/boost/regex/v5/basic_regex_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,11 @@ bool basic_regex_parser<charT, traits>::parse_extended_escape()
}
const charT* pc = m_position;
std::intmax_t i = this->m_traits.toi(pc, m_end, 10);
if(i < 0 && !syn_end)
{
fail(regex_constants::error_backref, m_position - m_base);
return false;
}
if((i < 0) && syn_end)
{
// Check for a named capture, get the leftmost one if there is more than one:
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ run issue153.cpp : : : "<toolset>msvc:<linkflags>-STACK:2097152" ;
run issue227.cpp ;
run issue232.cpp ;
run issue244.cpp ;
run issue245.cpp ;
run lookbehind_recursion_stress_test.cpp ;
run regex_replace_overflow.cpp ;

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

#include <vector>
#include <string>

#include "test_macros.hpp"


int main()
{
// invalid because \k-- is an unterminated token
{
char const strdata[] = "\\k--00000000000000000000000000000000000000000000000000000000009223372036854775807\xff\xff\xff\xff\xff\xff\xff\xef""99999999999999999999999999999999999]999999999999999\x90";
std::string regex_string(strdata, strdata + sizeof(strdata) - 1);
BOOST_TEST_THROWS((boost::regex(regex_string)), boost::regex_error);
}
{
char const strdata[] = "\\k-00000000000000000000000000000000000000000000000000000000009223372036854775807\xff\xff\xff\xff\xff\xff\xff\xef""99999999999999999999999999999999999]999999999999999\x90";
std::string regex_string(strdata, strdata + sizeof(strdata) - 1);
BOOST_TEST_THROWS((boost::regex(regex_string)), boost::regex_error);
}
{
char const strdata[] = "\\k00000000000000000000000000000000000000000000000000000000009223372036854775807\xff\xff\xff\xff\xff\xff\xff\xef""99999999999999999999999999999999999]999999999999999\x90";
std::string regex_string(strdata, strdata + sizeof(strdata) - 1);
BOOST_TEST_THROWS((boost::regex(regex_string)), boost::regex_error);
}
{
char const strdata[] = "a(b*)c\\k{--1}d";
std::string regex_string(strdata, strdata + sizeof(strdata) - 1);
BOOST_TEST_THROWS((boost::regex(regex_string)), boost::regex_error);
}
{
char const strdata[] = "a(b*)c\\k-{-1}d";
std::string regex_string(strdata, strdata + sizeof(strdata) - 1);
BOOST_TEST_THROWS((boost::regex(regex_string)), boost::regex_error);
}
{
char const strdata[] = "\\k{--00000000000000000000000000000000000000000000000000000000009223372036854775807}\xff\xff\xff\xff\xff\xff\xff\xef""99999999999999999999999999999999999]999999999999999\x90";
std::string regex_string(strdata, strdata + sizeof(strdata) - 1);
BOOST_TEST_THROWS((boost::regex(regex_string)), boost::regex_error);
}
{
char const strdata[] = "\\k{-00000000000000000000000000000000000000000000000000000000009223372036854775807}\xff\xff\xff\xff\xff\xff\xff\xef""99999999999999999999999999999999999]999999999999999\x90";
std::string regex_string(strdata, strdata + sizeof(strdata) - 1);
BOOST_TEST_THROWS((boost::regex(regex_string)), boost::regex_error);
}
{
char const strdata[] = "\\k{00000000000000000000000000000000000000000000000000000000009223372036854775807}\xff\xff\xff\xff\xff\xff\xff\xef""99999999999999999999999999999999999]999999999999999\x90";
std::string regex_string(strdata, strdata + sizeof(strdata) - 1);
BOOST_TEST_THROWS((boost::regex(regex_string)), boost::regex_error);
}

return boost::report_errors();
}
Loading