Bill Fisher: Fix for failing to throw an exception in regex when parsing an invalid...
authorHoward Hinnant <hhinnant@apple.com>
Fri, 28 Jun 2013 18:57:30 +0000 (18:57 +0000)
committerHoward Hinnant <hhinnant@apple.com>
Fri, 28 Jun 2013 18:57:30 +0000 (18:57 +0000)
llvm-svn: 185192

libcxx/include/regex
libcxx/test/re/re.regex/re.regex.construct/bad_escape.pass.cpp [new file with mode: 0644]

index d1afa54a894f9b2e8e4ffb4ece06db972c56edd7..27c7ecfa4e518502f219c84d37d8bdae2cd87f17 100644 (file)
@@ -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 (file)
index 0000000..75845a2
--- /dev/null
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <regex>
+
+// template <class charT, class traits = regex_traits<charT>> class basic_regex;
+
+// template <class ST, class SA>
+//    basic_regex(const basic_string<charT, ST, SA>& s);
+
+#include <regex>
+#include <cassert>
+
+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);
+    }
+}