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.
251}252}254[[nodiscard]] bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) noexcept;256/**257* Sets errno to a value selected from the given std::array `errnos`.258*/259template <typename T, size_t size>260void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider, const std::array<T, size>& errnos)261{262errno = fuzzed_data_provider.PickValueInArray(errnos);263}265/*266* Sets a fuzzed errno in the range [0, 133 (EHWPOISON)]. Can be used from functions emulating267* standard library functions that set errno, or in other contexts where the value of errno268* might be relevant for the execution path that will be taken.269*/270inline void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider) noexcept271{272errno = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 133);273}275/**276* Returns a byte vector of specified size regardless of the number of remaining bytes available277* from the fuzzer. Pads with zero value bytes if needed to achieve the specified size.278*/279template<typename B = uint8_t>280[[nodiscard]] inline std::vector<B> ConsumeFixedLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const size_t length) noexcept281{282static_assert(sizeof(B) == 1);283auto random_bytes = fuzzed_data_provider.ConsumeBytes<B>(length);284random_bytes.resize(length);285return random_bytes;286}288class FuzzedFileProvider289{290FuzzedDataProvider& m_fuzzed_data_provider;291int64_t m_offset = 0;293public:294FuzzedFileProvider(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}295{296}298FILE* open();300static ssize_t read(void* cookie, char* buf, size_t size);302static ssize_t write(void* cookie, const char* buf, size_t size);304static int seek(void* cookie, int64_t* offset, int whence);306static int close(void* cookie);307};309#define WRITE_TO_STREAM_CASE(type, consume) \310[&] { \311type o = consume; \312stream << o; \313}314template <typename Stream>315void WriteToStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept316{317while (fuzzed_data_provider.ConsumeBool()) {318try {319CallOneOf(320fuzzed_data_provider,321WRITE_TO_STREAM_CASE(bool, fuzzed_data_provider.ConsumeBool()),322WRITE_TO_STREAM_CASE(int8_t, fuzzed_data_provider.ConsumeIntegral<int8_t>()),323WRITE_TO_STREAM_CASE(uint8_t, fuzzed_data_provider.ConsumeIntegral<uint8_t>()),324WRITE_TO_STREAM_CASE(int16_t, fuzzed_data_provider.ConsumeIntegral<int16_t>()),325WRITE_TO_STREAM_CASE(uint16_t, fuzzed_data_provider.ConsumeIntegral<uint16_t>()),326WRITE_TO_STREAM_CASE(int32_t, fuzzed_data_provider.ConsumeIntegral<int32_t>()),327WRITE_TO_STREAM_CASE(uint32_t, fuzzed_data_provider.ConsumeIntegral<uint32_t>()),328WRITE_TO_STREAM_CASE(int64_t, fuzzed_data_provider.ConsumeIntegral<int64_t>()),329WRITE_TO_STREAM_CASE(uint64_t, fuzzed_data_provider.ConsumeIntegral<uint64_t>()),330WRITE_TO_STREAM_CASE(std::string, fuzzed_data_provider.ConsumeRandomLengthString(32)),331WRITE_TO_STREAM_CASE(std::vector<uint8_t>, ConsumeRandomLengthIntegralVector<uint8_t>(fuzzed_data_provider)));332} catch (const std::ios_base::failure&) {333break;334}335}336}338#define READ_FROM_STREAM_CASE(type) \339[&] { \340type o; \341stream >> o; \342}343template <typename Stream>344void ReadFromStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept345{346while (fuzzed_data_provider.ConsumeBool()) {347try {348CallOneOf(349fuzzed_data_provider,350READ_FROM_STREAM_CASE(bool),351READ_FROM_STREAM_CASE(int8_t),352READ_FROM_STREAM_CASE(uint8_t),353READ_FROM_STREAM_CASE(int16_t),354READ_FROM_STREAM_CASE(uint16_t),355READ_FROM_STREAM_CASE(int32_t),356READ_FROM_STREAM_CASE(uint32_t),357READ_FROM_STREAM_CASE(int64_t),358READ_FROM_STREAM_CASE(uint64_t),359READ_FROM_STREAM_CASE(std::string),360READ_FROM_STREAM_CASE(std::vector<uint8_t>));361} catch (const std::ios_base::failure&) {362break;363}364}365}367inline void FinalizeHeader(CBlockHeader& header, const ChainstateManager& chainman)368{369while (!CheckProofOfWork(header.GetHash(), header.nBits, chainman.GetParams().GetConsensus())) {370++(header.nNonce);371}372}374#endif // BITCOIN_TEST_FUZZ_UTIL_H
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.