From 568bd0222fd14e5cd0f7b158dd020d29e372227e Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Thu, 23 Jul 2015 18:27:51 +0000 Subject: [PATCH] Detect and throw on a class of bad regexes that we mistakenly accepted before. Thanks to Trevor Smigiel for the report git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@243030 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/regex | 8 ++++ .../re.regex.construct/bad_escape.pass.cpp | 2 +- .../re.regex.construct/bad_repeat.pass.cpp | 42 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/std/re/re.regex/re.regex.construct/bad_repeat.pass.cpp diff --git a/include/regex b/include/regex index 6ac5e1da..698278c6 100644 --- a/include/regex +++ b/include/regex @@ -4305,6 +4305,14 @@ basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first, } } break; + case '*': + case '+': + case '?': + case '{': +#ifndef _LIBCPP_NO_EXCEPTIONS + throw regex_error(regex_constants::error_badrepeat); +#endif + break; default: __first = __parse_pattern_character(__first, __last); break; diff --git a/test/std/re/re.regex/re.regex.construct/bad_escape.pass.cpp b/test/std/re/re.regex/re.regex.construct/bad_escape.pass.cpp index 94555274..ddf2607f 100644 --- a/test/std/re/re.regex/re.regex.construct/bad_escape.pass.cpp +++ b/test/std/re/re.regex/re.regex.construct/bad_escape.pass.cpp @@ -22,7 +22,7 @@ static bool error_escape_thrown(const char *pat) bool result = false; try { std::regex re(pat); - } catch (std::regex_error &ex) { + } catch (const std::regex_error &ex) { result = (ex.code() == std::regex_constants::error_escape); } return result; diff --git a/test/std/re/re.regex/re.regex.construct/bad_repeat.pass.cpp b/test/std/re/re.regex/re.regex.construct/bad_repeat.pass.cpp new file mode 100644 index 00000000..bc70ec13 --- /dev/null +++ b/test/std/re/re.regex/re.regex.construct/bad_repeat.pass.cpp @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template > class basic_regex; + +// template +// basic_regex(const basic_string& s); + +#include +#include + +static bool error_badrepeat_thrown(const char *pat) +{ + bool result = false; + try { + std::regex re(pat); + } catch (const std::regex_error &ex) { + result = (ex.code() == std::regex_constants::error_badrepeat); + } + return result; +} + +int main() +{ + assert(error_badrepeat_thrown("?a")); + assert(error_badrepeat_thrown("*a")); + assert(error_badrepeat_thrown("+a")); + assert(error_badrepeat_thrown("{a")); + + assert(error_badrepeat_thrown("?(a+)")); + assert(error_badrepeat_thrown("*(a+)")); + assert(error_badrepeat_thrown("+(a+)")); + assert(error_badrepeat_thrown("{(a+)")); +}