• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <regex>
11 
12 // template <class BidirectionalIterator, class Allocator, class charT, class traits>
13 //     bool
14 //     regex_search(BidirectionalIterator first, BidirectionalIterator last,
15 //                  match_results<BidirectionalIterator, Allocator>& m,
16 //                  const basic_regex<charT, traits>& e,
17 //                  regex_constants::match_flag_type flags = regex_constants::match_default);
18 
19 #include <regex>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "test_iterators.h"
24 
LLVMFuzzerTestOneInput(const char * data)25 extern "C" void LLVMFuzzerTestOneInput(const char *data)
26 {
27 #ifndef TEST_HAS_NO_EXCEPTIONS
28     size_t size = strlen(data);
29     if (size > 0)
30     {
31         try
32         {
33             std::regex::flag_type flag = std::regex_constants::grep;
34             std::string s((const char *)data, size);
35             std::regex re(s, flag);
36             TEST_IGNORE_NODISCARD std::regex_match(s, re);
37         }
38         catch (std::regex_error &) {}
39     }
40 #else
41     ((void)data);
42 #endif
43 }
44 
45 
fuzz_tests()46 void fuzz_tests()  // patterns that the fuzzer has found
47 {
48 // Raw string literals are a C++11 feature.
49 #if TEST_STD_VER >= 11
50     LLVMFuzzerTestOneInput(R"XX(Õ)_%()()((\8'_%()_%()_%()_%(()_%()_%()_%(.t;)()¥f()_%()(.)_%;)()!¥f(((()()XX");
51 #endif
52 }
53 
main()54 int main()
55 {
56     {
57         std::cmatch m;
58         const char s[] = "tournament";
59         assert(std::regex_search(s, m, std::regex("tour\nto\ntournament",
60                 std::regex_constants::grep)));
61         assert(m.size() == 1);
62         assert(!m.prefix().matched);
63         assert(m.prefix().first == s);
64         assert(m.prefix().second == m[0].first);
65         assert(!m.suffix().matched);
66         assert(m.suffix().first == m[0].second);
67         assert(m.suffix().second == s + std::char_traits<char>::length(s));
68         assert(m.length(0) == 10);
69         assert(m.position(0) == 0);
70         assert(m.str(0) == "tournament");
71     }
72     {
73         std::cmatch m;
74         const char s[] = "ment";
75         assert(std::regex_search(s, m, std::regex("tour\n\ntournament",
76                 std::regex_constants::grep)));
77         assert(m.size() == 1);
78         assert(!m.prefix().matched);
79         assert(m.prefix().first == s);
80         assert(m.prefix().second == m[0].first);
81         assert(m.suffix().matched);
82         assert(m.suffix().first == m[0].second);
83         assert(m.suffix().second == s + std::char_traits<char>::length(s));
84         assert(m.length(0) == 0);
85         assert(m.position(0) == 0);
86         assert(m.str(0) == "");
87     }
88     fuzz_tests();
89 }
90