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: c++03, c++11
10// UNSUPPORTED: no-exceptions
11// UNSUPPORTED: no-localization
12
13#include <cstddef>
14#include <cstdint>
15#include <regex>
16#include <string>
17
18#include "fuzz.h"
19
20template <std::regex_constants::syntax_option_type Syntax>
21static int regex_test(const std::uint8_t *data, std::size_t size) {
22 if (size == 0)
23 return 0;
24
25 std::string s((const char *)data, size);
26 std::regex re;
27 try {
28 re.assign(s: s, flags: Syntax);
29 } catch (std::regex_error &) {
30 // the data represents an invalid regex, ignore this test case
31 return 0;
32 }
33
34 auto match = std::regex_match(s: s, re: re);
35 (void)match;
36 return 0; // always pretend we succeeded -- we're only looking for crashes
37}
38
39extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) {
40 return regex_test<std::regex_constants::awk>(data, size) ||
41 regex_test<std::regex_constants::basic>(data, size) ||
42 regex_test<std::regex_constants::ECMAScript>(data, size) ||
43 regex_test<std::regex_constants::egrep>(data, size) ||
44 regex_test<std::regex_constants::extended>(data, size) ||
45 regex_test<std::regex_constants::grep>(data, size);
46}
47

source code of libcxx/test/libcxx/fuzzing/regex.pass.cpp