blob: 29300a818119b95c52f0b1a5055bf35c936f5338 [file] [log] [blame]
Howard Hinnant93da3b22010-07-27 19:53:10 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Howard Hinnant93da3b22010-07-27 19:53:10 +00006//
7//===----------------------------------------------------------------------===//
8
9// <regex>
10
11// template <class BidirectionalIterator, class Allocator, class charT, class traits>
12// bool
13// regex_search(BidirectionalIterator first, BidirectionalIterator last,
14// match_results<BidirectionalIterator, Allocator>& m,
15// const basic_regex<charT, traits>& e,
16// regex_constants::match_flag_type flags = regex_constants::match_default);
17
Howard Hinnant93da3b22010-07-27 19:53:10 +000018#include <regex>
19#include <cassert>
20
Eric Fiselier1e051aa2015-08-28 05:46:17 +000021#include "test_macros.h"
Marshall Clow32227082013-01-05 03:21:01 +000022#include "test_iterators.h"
Howard Hinnant93da3b22010-07-27 19:53:10 +000023
Marshall Clow550dfe72015-08-24 15:57:09 +000024extern "C" void LLVMFuzzerTestOneInput(const char *data)
25{
Asiri Rathnayake08eb2142016-10-06 11:15:41 +000026#ifndef TEST_HAS_NO_EXCEPTIONS
Marshall Clow550dfe72015-08-24 15:57:09 +000027 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);
Billy Robert O'Neal IIIba40b052017-11-21 21:37:26 +000035 TEST_IGNORE_NODISCARD std::regex_match(s, re);
Eric Fiselierd04c6852016-06-01 21:35:39 +000036 }
37 catch (std::regex_error &) {}
38 }
Eric Fiselierfd838222016-12-23 23:37:52 +000039#else
40 ((void)data);
Asiri Rathnayake08eb2142016-10-06 11:15:41 +000041#endif
Marshall Clow550dfe72015-08-24 15:57:09 +000042}
43
44
45void fuzz_tests() // patterns that the fuzzer has found
46{
Eric Fiselierde1495b2015-08-30 17:58:50 +000047// Raw string literals are a C++11 feature.
Eric Fiselier1e051aa2015-08-28 05:46:17 +000048#if TEST_STD_VER >= 11
Marshall Clow550dfe72015-08-24 15:57:09 +000049 LLVMFuzzerTestOneInput(R"XX(Õ)_%()()((\8'_%()_%()_%()_%(()_%()_%()_%(.t;)()¥f()_%()(.)_%;)()!¥f(((()()XX");
Eric Fiselier1e051aa2015-08-28 05:46:17 +000050#endif
Marshall Clow550dfe72015-08-24 15:57:09 +000051}
52
JF Bastien2df59c52019-02-04 20:31:13 +000053int main(int, char**)
Howard Hinnant93da3b22010-07-27 19:53:10 +000054{
55 {
56 std::cmatch m;
57 const char s[] = "tournament";
58 assert(std::regex_search(s, m, std::regex("tour\nto\ntournament",
59 std::regex_constants::grep)));
60 assert(m.size() == 1);
61 assert(!m.prefix().matched);
62 assert(m.prefix().first == s);
63 assert(m.prefix().second == m[0].first);
64 assert(!m.suffix().matched);
65 assert(m.suffix().first == m[0].second);
66 assert(m.suffix().second == s + std::char_traits<char>::length(s));
67 assert(m.length(0) == 10);
68 assert(m.position(0) == 0);
69 assert(m.str(0) == "tournament");
70 }
71 {
72 std::cmatch m;
73 const char s[] = "ment";
74 assert(std::regex_search(s, m, std::regex("tour\n\ntournament",
75 std::regex_constants::grep)));
76 assert(m.size() == 1);
77 assert(!m.prefix().matched);
78 assert(m.prefix().first == s);
79 assert(m.prefix().second == m[0].first);
80 assert(m.suffix().matched);
81 assert(m.suffix().first == m[0].second);
82 assert(m.suffix().second == s + std::char_traits<char>::length(s));
83 assert(m.length(0) == 0);
84 assert(m.position(0) == 0);
85 assert(m.str(0) == "");
86 }
Marshall Clow550dfe72015-08-24 15:57:09 +000087 fuzz_tests();
JF Bastien2df59c52019-02-04 20:31:13 +000088
89 return 0;
Howard Hinnant93da3b22010-07-27 19:53:10 +000090}