xref: /aosp_15_r20/external/cronet/third_party/libc++/src/test/libcxx/fuzzing/regex.pass.cpp (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
20 template <std::regex_constants::syntax_option_type Syntax>
regex_test(const std::uint8_t * data,std::size_t size)21 static 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, 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, re);
35     (void)match;
36     return 0; // always pretend we succeeded -- we're only looking for crashes
37 }
38 
LLVMFuzzerTestOneInput(const std::uint8_t * data,std::size_t size)39 extern "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