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 <cstddef>
13 #include <cstdint>
14 #include <iterator>
15 #include <vector>
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 std::vector<std::uint8_t> working(data, data + size);
21 std::sort(working.begin(), working.end());
22 std::vector<std::uint8_t> results;
23 (void)std::unique_copy(working.begin(), working.end(),
24 std::back_inserter<std::vector<std::uint8_t>>(results));
25 std::vector<std::uint8_t>::iterator it; // scratch iterator
26
27 // Check the size of the unique'd sequence.
28 // it should only be zero if the input sequence was empty.
29 if (results.size() == 0)
30 return working.size() == 0 ? 0 : 1;
31
32 // 'results' is sorted
33 if (!std::is_sorted(results.begin(), results.end()))
34 return 2;
35
36 // All the elements in 'results' must be different
37 it = results.begin();
38 std::uint8_t prev_value = *it++;
39 for (; it != results.end(); ++it) {
40 if (*it == prev_value)
41 return 3;
42 prev_value = *it;
43 }
44
45 // Every element in 'results' must be in 'working'
46 for (auto v : results)
47 if (std::find(working.begin(), working.end(), v) == working.end())
48 return 4;
49
50 // Every element in 'working' must be in 'results'
51 for (auto v : working)
52 if (std::find(results.begin(), results.end(), v) == results.end())
53 return 5;
54
55 return 0;
56 }
57