/src/llvm-project/libcxx/test/libcxx/fuzzing/search.pass.cpp
Line | Count | Source |
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 | | |
19 | 150 | extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) { |
20 | 150 | if (size < 2) |
21 | 1 | return 0; |
22 | | |
23 | 149 | const std::size_t pat_size = data[0] * (size - 1) / std::numeric_limits<uint8_t>::max(); |
24 | 149 | assert(pat_size <= size - 1); |
25 | 149 | const std::uint8_t *pat_begin = data + 1; |
26 | 149 | const std::uint8_t *pat_end = pat_begin + pat_size; |
27 | 149 | const std::uint8_t *data_end = data + size; |
28 | 149 | assert(pat_end <= data_end); |
29 | | |
30 | 149 | auto it = std::search(pat_end, data_end, pat_begin, pat_end); |
31 | 149 | if (it != data_end) // not found |
32 | 27 | if (!std::equal(pat_begin, pat_end, it)) |
33 | 0 | return 1; |
34 | 149 | return 0; |
35 | 149 | } |