From 918f2a80ababefb69a9e695767350f3912c7d127 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Fri, 28 Jun 2013 18:57:30 +0000 Subject: [PATCH] Bill Fisher: Fix for failing to throw an exception in regex when parsing an invalid escape sequence. This fixes http://llvm.org/bugs/show_bug.cgi?id=16023 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@185192 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/regex | 2 +- .../re.regex.construct/bad_escape.pass.cpp | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 test/re/re.regex/re.regex.construct/bad_escape.pass.cpp diff --git a/include/regex b/include/regex index d1afa54a..27c7ecfa 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 00000000..75845a23 --- /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); + } +}