Branch data Line data Source code
1 : : // Copyright (c) 2020-2021 The Bitcoin Core developers 2 : : // Distributed under the MIT software license, see the accompanying 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : : 5 : : #include <test/fuzz/FuzzedDataProvider.h> 6 : : #include <test/fuzz/fuzz.h> 7 : : #include <test/fuzz/util.h> 8 : : #include <timedata.h> 9 : : 10 : : #include <cstdint> 11 : : #include <string> 12 : : #include <vector> 13 : : 14 [ - + ]: 495 : FUZZ_TARGET(timedata) 15 : : { 16 : 149 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); 17 : 149 : const unsigned int max_size = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 1000); 18 : : // A max_size of 0 implies no limit, so cap the max number of insertions to avoid timeouts 19 : 149 : auto max_to_insert = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 4000); 20 : : // Divide by 2 to avoid signed integer overflow in .median() 21 : 149 : const int64_t initial_value = fuzzed_data_provider.ConsumeIntegral<int64_t>() / 2; 22 : 149 : CMedianFilter<int64_t> median_filter{max_size, initial_value}; 23 [ + - + + : 20739 : while (fuzzed_data_provider.remaining_bytes() > 0 && --max_to_insert >= 0) { + + ] 24 [ + - ]: 20590 : (void)median_filter.median(); 25 [ + - + - : 20763 : assert(median_filter.size() > 0); - + ] 26 [ + - + - : 20590 : assert(static_cast<size_t>(median_filter.size()) == median_filter.sorted().size()); - + ] 27 [ + - + + : 20590 : assert(static_cast<unsigned int>(median_filter.size()) <= max_size || max_size == 0); - + ] 28 : : // Divide by 2 to avoid signed integer overflow in .median() 29 [ + - + - ]: 20590 : median_filter.input(fuzzed_data_provider.ConsumeIntegral<int64_t>() / 2); 30 : : } 31 : 149 : }