Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2020-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <span.h>6#include <streams.h>7#include <test/fuzz/fuzz.h>8#include <test/fuzz/FuzzedDataProvider.h>9#include <test/fuzz/util.h>10#include <util/obfuscation.h>12#include <array>13#include <cstddef>14#include <cstdint>15#include <iostream>16#include <optional>17#include <vector>19FUZZ_TARGET(buffered_file)20{21FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};22FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};23std::optional<BufferedFile> opt_buffered_file;24const auto key_bytes{ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, Obfuscation::KEY_SIZE)};25AutoFile fuzzed_file{26fuzzed_file_provider.open(),27Obfuscation{std::span{key_bytes}.first<Obfuscation::KEY_SIZE>()},28};29try {30auto n_buf_size = fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096);31auto n_rewind_in = fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096);32opt_buffered_file.emplace(fuzzed_file, n_buf_size, n_rewind_in);33} catch (const std::ios_base::failure&) {34}35if (opt_buffered_file && !fuzzed_file.IsNull()) {36bool setpos_fail = false;37LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 100) {38CallOneOf(39fuzzed_data_provider,40[&] {41std::array<std::byte, 4096> arr{};42try {43opt_buffered_file->read({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});44} catch (const std::ios_base::failure&) {45}46},47[&] {48opt_buffered_file->SetLimit(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096));49},50[&] {51if (!opt_buffered_file->SetPos(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096))) {52setpos_fail = true;53}54},55[&] {56if (setpos_fail) {57// Calling FindByte(...) after a failed SetPos(...) call may result in an infinite loop.58return;59}60try {61opt_buffered_file->FindByte(std::byte(fuzzed_data_provider.ConsumeIntegral<uint8_t>()));62} catch (const std::ios_base::failure&) {63}64},65[&] {66ReadFromStream(fuzzed_data_provider, *opt_buffered_file);67});68}69opt_buffered_file->GetPos();70}71}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.