Bitcoin Core  22.99.0
P2P Digital Currency
buffered_file.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(buffered_file)
18 {
19  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
20  FuzzedFileProvider fuzzed_file_provider = ConsumeFile(fuzzed_data_provider);
21  std::optional<CBufferedFile> opt_buffered_file;
22  FILE* fuzzed_file = fuzzed_file_provider.open();
23  try {
24  opt_buffered_file.emplace(fuzzed_file, fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeIntegral<int>());
25  } catch (const std::ios_base::failure&) {
26  if (fuzzed_file != nullptr) {
27  fclose(fuzzed_file);
28  }
29  }
30  if (opt_buffered_file && fuzzed_file != nullptr) {
31  bool setpos_fail = false;
32  while (fuzzed_data_provider.ConsumeBool()) {
33  CallOneOf(
34  fuzzed_data_provider,
35  [&] {
36  std::array<uint8_t, 4096> arr{};
37  try {
38  opt_buffered_file->read((char*)arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
39  } catch (const std::ios_base::failure&) {
40  }
41  },
42  [&] {
43  opt_buffered_file->SetLimit(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096));
44  },
45  [&] {
46  if (!opt_buffered_file->SetPos(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096))) {
47  setpos_fail = true;
48  }
49  },
50  [&] {
51  if (setpos_fail) {
52  // Calling FindByte(...) after a failed SetPos(...) call may result in an infinite loop.
53  return;
54  }
55  try {
56  opt_buffered_file->FindByte(fuzzed_data_provider.ConsumeIntegral<char>());
57  } catch (const std::ios_base::failure&) {
58  }
59  },
60  [&] {
61  ReadFromStream(fuzzed_data_provider, *opt_buffered_file);
62  });
63  }
64  opt_buffered_file->GetPos();
65  opt_buffered_file->GetType();
66  opt_buffered_file->GetVersion();
67  }
68 }
streams.h
util.h
FUZZ_TARGET
FUZZ_TARGET(buffered_file)
Definition: buffered_file.cpp:17
ReadFromStream
void ReadFromStream(CAddrMan &addr, CDataStream &ssPeers)
Only used by tests.
Definition: addrdb.cpp:179
ConsumeFile
FuzzedFileProvider ConsumeFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:301
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40
FuzzedDataProvider.h
FuzzedFileProvider::open
FILE * open()
Definition: util.cpp:395
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedFileProvider
Definition: util.h:280