From 21246e3314785180079699edcdd1b84b277067c8 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 llvm-svn: 185192 --- libcxx/include/regex | 2 +- .../re.regex.construct/bad_escape.pass.cpp | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 libcxx/test/re/re.regex/re.regex.construct/bad_escape.pass.cpp diff --git a/libcxx/include/regex b/libcxx/include/regex index d1afa54a894f..27c7ecfa4e51 100644 --- a/libcxx/include/regex +++ b/libcxx/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/libcxx/test/re/re.regex/re.regex.construct/bad_escape.pass.cpp b/libcxx/test/re/re.regex/re.regex.construct/bad_escape.pass.cpp new file mode 100644 index 000000000000..75845a232c40 --- /dev/null +++ b/libcxx/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); + } +} -- 2.34.1