blob: 7a44ba71ada7ac0c7db68bc67d075f29b1b0a51e [file] [log] [blame]
Howard Hinnant93da3b22010-07-27 19:53:10 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant93da3b22010-07-27 19:53:10 +00007//
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
Howard Hinnant93da3b22010-07-27 19:53:10 +000019#include <regex>
20#include <cassert>
21
Eric Fiselier1e051aa2015-08-28 05:46:17 +000022#include "test_macros.h"
Marshall Clow32227082013-01-05 03:21:01 +000023#include "test_iterators.h"
Howard Hinnant93da3b22010-07-27 19:53:10 +000024
Marshall Clow550dfe72015-08-24 15:57:09 +000025extern "C" void LLVMFuzzerTestOneInput(const char *data)
26{
27 size_t size = strlen(data);
28 if (size > 0)
29 {
30 try
31 {
32 std::regex::flag_type flag = std::regex_constants::grep;
33 std::string s((const char *)data, size);
34 std::regex re(s, flag);
35 std::regex_match(s, re);
36 }
37 catch (std::regex_error &ex) {}
38 }
39}
40
41
42void fuzz_tests() // patterns that the fuzzer has found
43{
Eric Fiselierde1495b2015-08-30 17:58:50 +000044// Raw string literals are a C++11 feature.
Eric Fiselier1e051aa2015-08-28 05:46:17 +000045#if TEST_STD_VER >= 11
Marshall Clow550dfe72015-08-24 15:57:09 +000046 LLVMFuzzerTestOneInput(R"XX(Õ)_%()()((\8'_%()_%()_%()_%(()_%()_%()_%(.t;)()¥f()_%()(.)_%;)()!¥f(((()()XX");
Eric Fiselier1e051aa2015-08-28 05:46:17 +000047#endif
Marshall Clow550dfe72015-08-24 15:57:09 +000048}
49
Howard Hinnant93da3b22010-07-27 19:53:10 +000050int main()
51{
52 {
53 std::cmatch m;
54 const char s[] = "tournament";
55 assert(std::regex_search(s, m, std::regex("tour\nto\ntournament",
56 std::regex_constants::grep)));
57 assert(m.size() == 1);
58 assert(!m.prefix().matched);
59 assert(m.prefix().first == s);
60 assert(m.prefix().second == m[0].first);
61 assert(!m.suffix().matched);
62 assert(m.suffix().first == m[0].second);
63 assert(m.suffix().second == s + std::char_traits<char>::length(s));
64 assert(m.length(0) == 10);
65 assert(m.position(0) == 0);
66 assert(m.str(0) == "tournament");
67 }
68 {
69 std::cmatch m;
70 const char s[] = "ment";
71 assert(std::regex_search(s, m, std::regex("tour\n\ntournament",
72 std::regex_constants::grep)));
73 assert(m.size() == 1);
74 assert(!m.prefix().matched);
75 assert(m.prefix().first == s);
76 assert(m.prefix().second == m[0].first);
77 assert(m.suffix().matched);
78 assert(m.suffix().first == m[0].second);
79 assert(m.suffix().second == s + std::char_traits<char>::length(s));
80 assert(m.length(0) == 0);
81 assert(m.position(0) == 0);
82 assert(m.str(0) == "");
83 }
Marshall Clow550dfe72015-08-24 15:57:09 +000084 fuzz_tests();
Howard Hinnant93da3b22010-07-27 19:53:10 +000085}