xref: /aosp_15_r20/external/cronet/base/strings/pattern_fuzzer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stdint.h>
6 
7 #include <string>
8 #include <tuple>
9 
10 #include <fuzzer/FuzzedDataProvider.h>
11 
12 #include "base/strings/pattern.h"
13 #include "base/strings/utf_string_conversions.h"
14 
15 namespace {
16 
17 // Prevent huge inputs from hitting time limits.
18 constexpr size_t kMaxLength = 1000;
19 
20 }  // namespace
21 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)22 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
23   FuzzedDataProvider provider(data, size);
24   std::string string = provider.ConsumeRandomLengthString(kMaxLength);
25   std::string pattern = provider.ConsumeRandomLengthString(kMaxLength);
26 
27   std::ignore = base::MatchPattern(string, pattern);
28   // Test the wide-string version as well. Note that the Unicode conversion
29   // function skips errors (returning the best conversion possible), which is
30   // good enough for the fuzzer.
31   std::ignore =
32       base::MatchPattern(base::UTF8ToUTF16(string), base::UTF8ToUTF16(pattern));
33 
34   return 0;
35 }
36