diff --git a/include/regex b/include/regex index d1afa54a8..27c7ecfa4 100644 --- a/include/regex +++ b/include/regex @@ -4481,7 +4481,7 @@ basic_regex<_CharT, _Traits>::__parse_character_escape(_ForwardIterator __first, ++__first; } #ifndef _LIBCPP_NO_EXCEPTIONS - else if (__str) + else throw regex_error(regex_constants::error_escape); #endif // _LIBCPP_NO_EXCEPTIONS break; diff --git a/test/re/re.regex/re.regex.construct/bad_escape.pass.cpp b/test/re/re.regex/re.regex.construct/bad_escape.pass.cpp new file mode 100644 index 000000000..75845a232 --- /dev/null +++ b/test/re/re.regex/re.regex.construct/bad_escape.pass.cpp @@ -0,0 +1,37 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +int main() +{ + // Correct: Exception thrown for invalid escape char in a character class + try { + std::regex char_class_escape("[\\a]"); + assert(false); + } catch (std::regex_error &ex) { + assert(ex.code() == std::regex_constants::error_escape); + } + + // Failure: No exception thrown for invalid escape char in this case. + try { + std::regex escape("\\a"); + assert(false); + } catch (std::regex_error &ex) { + assert(ex.code() == std::regex_constants::error_escape); + } +}