1
+ // (C) Copyright Marshall Clow 2018
2
+ // Use, modification and distribution are subject to the
3
+ // Boost Software License, Version 1.0. (See accompanying file
4
+ // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
+
6
+ #include < iterator> // for std::distance
7
+
8
+ #include < boost/algorithm/minmax_element.hpp>
9
+
10
+ // Fuzzing tests for:
11
+ //
12
+ // template <class ForwardIterator>
13
+ // std::pair<ForwardIterator,ForwardIterator>
14
+ // minmax_element(ForwardIterator first, ForwardIterator last);
15
+ //
16
+ // template <class ForwardIterator, class BinaryPredicate>
17
+ // std::pair<ForwardIterator,ForwardIterator>
18
+ // minmax_element(ForwardIterator first, ForwardIterator last,
19
+ // BinaryPredicate comp);
20
+
21
+
22
+ bool greater (uint8_t lhs, uint8_t rhs) { return lhs > rhs; }
23
+
24
+ extern " C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t sz) {
25
+ typedef std::pair<const uint8_t *, const uint8_t *> result_t ;
26
+ if (sz == 0 ) return 0 ; // we need at least one element
27
+
28
+ {
29
+ // Find the min and max
30
+ result_t result = boost::minmax_element (data, data + sz);
31
+
32
+ // The iterators have to be in the sequence
33
+ if (std::distance (data, result.first ) >= sz) return 1 ;
34
+ if (std::distance (data, result.second ) >= sz) return 1 ;
35
+
36
+ // the minimum element can't be bigger than the max element
37
+ uint8_t min_value = *result.first ;
38
+ uint8_t max_value = *result.second ;
39
+
40
+ if (max_value < min_value) return 2 ;
41
+
42
+ // None of the elements in the sequence can be less than the min, nor greater than the max
43
+ for (size_t i = 0 ; i < sz; ++i) {
44
+ if (data[i] < min_value) return 3 ;
45
+ if (max_value < data[i]) return 3 ;
46
+ }
47
+ }
48
+
49
+ {
50
+ // Find the min and max
51
+ result_t result = boost::minmax_element (data, data + sz, greater);
52
+
53
+ // The iterators have to be in the sequence
54
+ if (std::distance (data, result.first ) >= sz) return 1 ;
55
+ if (std::distance (data, result.second ) >= sz) return 1 ;
56
+
57
+ // the minimum element can't be bigger than the max element
58
+ uint8_t min_value = *result.first ;
59
+ uint8_t max_value = *result.second ;
60
+
61
+ if (greater (max_value, min_value)) return 2 ;
62
+
63
+ // None of the elements in the sequence can be less than the min, nor greater than the max
64
+ for (size_t i = 0 ; i < sz; ++i) {
65
+ if (greater (data[i], min_value)) return 3 ;
66
+ if (greater (max_value, data[i])) return 3 ;
67
+ }
68
+ }
69
+
70
+ return 0 ;
71
+ }
0 commit comments