Bitcoin Core  22.99.0
P2P Digital Currency
autofile.cpp
Go to the documentation of this file.
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 <streams.h>
7 #include <test/fuzz/fuzz.h>
8 #include <test/fuzz/util.h>
9 
10 #include <array>
11 #include <cstdint>
12 #include <iostream>
13 #include <optional>
14 #include <string>
15 #include <vector>
16 
17 FUZZ_TARGET(autofile)
18 {
19  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
20  FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
21  CAutoFile auto_file = fuzzed_auto_file_provider.open();
22  while (fuzzed_data_provider.ConsumeBool()) {
23  CallOneOf(
24  fuzzed_data_provider,
25  [&] {
26  std::array<uint8_t, 4096> arr{};
27  try {
28  auto_file.read((char*)arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
29  } catch (const std::ios_base::failure&) {
30  }
31  },
32  [&] {
33  const std::array<uint8_t, 4096> arr{};
34  try {
35  auto_file.write((const char*)arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
36  } catch (const std::ios_base::failure&) {
37  }
38  },
39  [&] {
40  try {
41  auto_file.ignore(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
42  } catch (const std::ios_base::failure&) {
43  }
44  },
45  [&] {
46  auto_file.fclose();
47  },
48  [&] {
49  ReadFromStream(fuzzed_data_provider, auto_file);
50  },
51  [&] {
52  WriteToStream(fuzzed_data_provider, auto_file);
53  });
54  }
55  (void)auto_file.Get();
56  (void)auto_file.GetType();
57  (void)auto_file.GetVersion();
58  (void)auto_file.IsNull();
59  if (fuzzed_data_provider.ConsumeBool()) {
60  FILE* f = auto_file.release();
61  if (f != nullptr) {
62  fclose(f);
63  }
64  }
65 }
CAutoFile::ignore
void ignore(size_t nSize)
Definition: streams.h:625
streams.h
CAutoFile::Get
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:605
util.h
CAutoFile::read
void read(char *pch, size_t nSize)
Definition: streams.h:617
FUZZ_TARGET
FUZZ_TARGET(autofile)
Definition: autofile.cpp:17
ReadFromStream
void ReadFromStream(CAddrMan &addr, CDataStream &ssPeers)
Only used by tests.
Definition: addrdb.cpp:179
CAutoFile::GetVersion
int GetVersion() const
Definition: streams.h:615
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:564
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40
FuzzedDataProvider.h
FuzzedAutoFileProvider
Definition: util.h:306
CAutoFile::IsNull
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:609
CAutoFile::fclose
void fclose()
Definition: streams.h:587
CAutoFile::GetType
int GetType() const
Definition: streams.h:614
ConsumeAutoFile
FuzzedAutoFileProvider ConsumeAutoFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:322
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
WriteToStream
void WriteToStream(FuzzedDataProvider &fuzzed_data_provider, Stream &stream) noexcept
Definition: util.h:333
CAutoFile::write
void write(const char *pch, size_t nSize)
Definition: streams.h:638
CAutoFile::release
FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:599
FuzzedAutoFileProvider::open
CAutoFile open()
Definition: util.h:316