Branch data Line data Source code
1 : : // Copyright (c) 2020-2022 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 <span.h>
6 : : #include <streams.h>
7 : : #include <test/fuzz/FuzzedDataProvider.h>
8 : : #include <test/fuzz/fuzz.h>
9 : : #include <test/fuzz/util.h>
10 : :
11 : : #include <array>
12 : : #include <cstddef>
13 : : #include <cstdio>
14 : : #include <iostream>
15 : : #include <vector>
16 : :
17 [ + - + - ]: 434 : FUZZ_TARGET(autofile)
18 : : {
19 : 431 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
20 : 431 : FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
21 [ + - + - ]: 862 : AutoFile auto_file{
22 : 431 : fuzzed_file_provider.open(),
23 : 431 : ConsumeRandomLengthByteVector<std::byte>(fuzzed_data_provider),
24 : : };
25 [ + - + + : 13991 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
+ + ]
26 : : {
27 [ + - ]: 13560 : CallOneOf(
28 : : fuzzed_data_provider,
29 : 14299 : [&] {
30 : 739 : std::array<std::byte, 4096> arr{};
31 : : try {
32 [ + + ]: 739 : auto_file.read({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
33 [ + - ]: 739 : } catch (const std::ios_base::failure&) {
34 : 556 : }
35 : 1295 : },
36 : 15612 : [&] {
37 : 2052 : const std::array<std::byte, 4096> arr{};
38 : : try {
39 [ + + ]: 2052 : auto_file.write({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
40 [ + - ]: 2052 : } catch (const std::ios_base::failure&) {
41 : 871 : }
42 : 2923 : },
43 : 14491 : [&] {
44 : : try {
45 [ + + ]: 931 : auto_file.ignore(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
46 [ + - ]: 931 : } catch (const std::ios_base::failure&) {
47 : 548 : }
48 : 1479 : },
49 : 14077 : [&] {
50 : 517 : auto_file.fclose();
51 : 517 : },
52 : 19225 : [&] {
53 : 5665 : ReadFromStream(fuzzed_data_provider, auto_file);
54 : 5665 : },
55 : 17216 : [&] {
56 : 3656 : WriteToStream(fuzzed_data_provider, auto_file);
57 : 3656 : });
58 : 13560 : }
59 [ + - ]: 431 : (void)auto_file.Get();
60 [ + - ]: 431 : (void)auto_file.IsNull();
61 [ + - + + ]: 431 : if (fuzzed_data_provider.ConsumeBool()) {
62 [ + - ]: 45 : FILE* f = auto_file.release();
63 [ + + ]: 45 : if (f != nullptr) {
64 [ + - ]: 29 : fclose(f);
65 : 29 : }
66 : 45 : }
67 : 431 : }
|