Skip to content

Commit d574d1e

Browse files
committed
Fuzzing targets for minmax
1 parent 3af7aca commit d574d1e

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

minmax/fuzzing/minmax.fuzz.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 <boost/algorithm/minmax.hpp>
7+
8+
// Fuzzing tests for:
9+
//
10+
// template <class T>
11+
// tuple<T const&, T const&>
12+
// minmax(const T& a, const T& b);
13+
//
14+
// template <class T, class BinaryPredicate>
15+
// tuple<T const&, T const&>
16+
// minmax(const T& a, const T& b, BinaryPredicate comp);
17+
18+
19+
bool greater(uint8_t lhs, uint8_t rhs) { return lhs > rhs; }
20+
21+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t sz) {
22+
typedef boost::tuple<uint8_t const&, uint8_t const&> result_t;
23+
if (sz < 2) return 0; // we only need two elements
24+
25+
{
26+
result_t result = boost::minmax(data[0], data[1]);
27+
uint8_t const& first = result.get<0>();
28+
uint8_t const& second = result.get<1>();
29+
30+
// first must be <= second
31+
if (second < first) return 1;
32+
33+
// The references returned must be data[0] and data[1]
34+
if (&first != data && &first != data + 1) return 2;
35+
if (&second != data && &second != data + 1) return 2;
36+
}
37+
38+
{
39+
result_t result = boost::minmax(data[0], data[1], greater);
40+
uint8_t const& first = result.get<0>();
41+
uint8_t const& second = result.get<1>();
42+
43+
// first must be <= second
44+
if (greater(second, first)) return 1;
45+
46+
// The references returned must be data[0] and data[1]
47+
if (&first != data && &first != data + 1) return 2;
48+
if (&second != data && &second != data + 1) return 2;
49+
}
50+
51+
return 0;
52+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)