mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-13 07:23:21 +02:00
Replace hard-coded MiB byte conversions (e.g. `1024*1024`, `1<<20`, `1048576`) with the existing `_MiB` literal to improve readability and avoid repeating constants.
In the few spots where arithmetic involves signed values, the result is identical to the previous code assuming those quantities never turn negative.
Also switch to brace init on every declaration assigned from `_MiB`/`_GiB` literals so a future oversized value (e.g. `unsigned int x{4096_MiB}`) becomes a compile error through the C++11 narrowing check instead of silently truncating.
Extend unit tests to cover the 32-bit `size_t` overflow boundary and to assert equivalence for integer and floating-point conversions.
Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
Co-authored-by: w0xlt <94266259+w0xlt@users.noreply.github.com>
52 lines
1.7 KiB
C++
52 lines
1.7 KiB
C++
// Copyright (c) 2020-present The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <cuckoocache.h>
|
|
#include <script/sigcache.h>
|
|
#include <test/fuzz/FuzzedDataProvider.h>
|
|
#include <test/fuzz/fuzz.h>
|
|
#include <test/fuzz/util.h>
|
|
#include <test/util/setup_common.h>
|
|
#include <util/byte_units.h>
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
FuzzedDataProvider* fuzzed_data_provider_ptr = nullptr;
|
|
|
|
struct RandomHasher {
|
|
template <uint8_t>
|
|
uint32_t operator()(const bool& /* unused */) const
|
|
{
|
|
assert(fuzzed_data_provider_ptr != nullptr);
|
|
return fuzzed_data_provider_ptr->ConsumeIntegral<uint32_t>();
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
FUZZ_TARGET(cuckoocache)
|
|
{
|
|
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
|
fuzzed_data_provider_ptr = &fuzzed_data_provider;
|
|
CuckooCache::cache<int, RandomHasher> cuckoo_cache{};
|
|
if (fuzzed_data_provider.ConsumeBool()) {
|
|
const size_t megabytes = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 16);
|
|
cuckoo_cache.setup_bytes(megabytes * 1_MiB);
|
|
} else {
|
|
cuckoo_cache.setup(fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, 4096));
|
|
}
|
|
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
|
|
if (fuzzed_data_provider.ConsumeBool()) {
|
|
cuckoo_cache.insert(fuzzed_data_provider.ConsumeBool());
|
|
} else {
|
|
auto e = fuzzed_data_provider.ConsumeBool();
|
|
auto erase = fuzzed_data_provider.ConsumeBool();
|
|
cuckoo_cache.contains(e, erase);
|
|
}
|
|
}
|
|
fuzzed_data_provider_ptr = nullptr;
|
|
}
|