91 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <regex>10 11// template <class BidirectionalIterator, class Allocator, class charT, class traits>12// bool13// 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 18#include <regex>19#include <cassert>20 21#include "test_macros.h"22#include "test_iterators.h"23 24extern "C" void LLVMFuzzerTestOneInput(const char *data)25{26#ifndef TEST_HAS_NO_EXCEPTIONS27 std::size_t size = strlen(data);28 if (size > 0)29 {30 try31 {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 TEST_IGNORE_NODISCARD std::regex_match(s, re);36 }37 catch (std::regex_error &) {}38 }39#else40 ((void)data);41#endif42}43 44 45void fuzz_tests() // patterns that the fuzzer has found46{47// Raw string literals are a C++11 feature.48#if TEST_STD_VER >= 1149 LLVMFuzzerTestOneInput(R"XX(Õ)_%()()((\8'_%()_%()_%()_%(()_%()_%()_%(.t;)()¥f()_%()(.)_%;)()!¥f(((()()XX");50#endif51}52 53int main(int, char**)54{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 }87 fuzz_tests();88 89 return 0;90}91