xref: /llvm-project/libcxx/test/std/re/re.regex/re.regex.construct/bad_backref.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: libcpp-no-exceptions
10 // <regex>
11 
12 // template <class charT, class traits = regex_traits<charT>> class basic_regex;
13 
14 // template <class ST, class SA>
15 //    basic_regex(const basic_string<charT, ST, SA>& s);
16 
17 #include <regex>
18 #include <cassert>
19 #include "test_macros.h"
20 
21 static bool error_badbackref_thrown(const char *pat)
22 {
23     bool result = false;
24     try {
25         std::regex re(pat);
26     } catch (const std::regex_error &ex) {
27         result = (ex.code() == std::regex_constants::error_backref);
28     }
29     return result;
30 }
31 
32 int main(int, char**)
33 {
34     assert(error_badbackref_thrown("\\1abc"));      // no references
35     assert(error_badbackref_thrown("ab(c)\\2def")); // only one reference
36     assert(error_badbackref_thrown("\\800000000000000000000000000000")); // overflows
37 
38 //  this should NOT throw, because we only should look at the '1'
39 //  See https://bugs.llvm.org/show_bug.cgi?id=31387
40     {
41     const char *pat1 = "a(b)c\\1234";
42     std::regex re(pat1, pat1 + 7); // extra chars after the end.
43     }
44 
45   return 0;
46 }
47