xref: /llvm-project/libcxx/test/libcxx/fuzzing/search.pass.cpp (revision b4bd194378851c2f421477d4147019d10f2420ac)
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 
11 #include <algorithm>
12 #include <cassert>
13 #include <cstddef>
14 #include <cstdint>
15 #include <limits>
16 
17 #include "fuzz.h"
18 
LLVMFuzzerTestOneInput(const std::uint8_t * data,std::size_t size)19 extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) {
20     if (size < 2)
21         return 0;
22 
23     const std::size_t pat_size = data[0] * (size - 1) / std::numeric_limits<uint8_t>::max();
24     assert(pat_size <= size - 1);
25     const std::uint8_t *pat_begin = data + 1;
26     const std::uint8_t *pat_end   = pat_begin + pat_size;
27     const std::uint8_t *data_end  = data + size;
28     assert(pat_end <= data_end);
29 
30     auto it = std::search(pat_end, data_end, pat_begin, pat_end);
31     if (it != data_end) // not found
32         if (!std::equal(pat_begin, pat_end, it))
33             return 1;
34     return 0;
35 }
36