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 [ + - ]: 856 : FUZZ_TARGET(autofile)
18 : : {
19 : 410 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
20 : 410 : FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
21 : 410 : AutoFile auto_file{
22 : : fuzzed_file_provider.open(),
23 : 410 : ConsumeRandomLengthByteVector<std::byte>(fuzzed_data_provider),
24 [ + - ]: 410 : };
25 [ + + + + ]: 10697 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
26 : : {
27 [ + - ]: 10287 : CallOneOf(
28 : : fuzzed_data_provider,
29 : 690 : [&] {
30 : 690 : std::array<std::byte, 4096> arr{};
31 : 690 : try {
32 [ + + ]: 690 : auto_file.read({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
33 [ - + ]: 374 : } catch (const std::ios_base::failure&) {
34 : 374 : }
35 : 690 : },
36 : 1379 : [&] {
37 : 1379 : const std::array<std::byte, 4096> arr{};
38 : 1379 : try {
39 [ + + ]: 1379 : auto_file.write({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
40 [ - + ]: 589 : } catch (const std::ios_base::failure&) {
41 : 589 : }
42 : 1379 : },
43 : 1110 : [&] {
44 : 1110 : try {
45 [ + + ]: 1110 : auto_file.ignore(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
46 [ - + ]: 613 : } catch (const std::ios_base::failure&) {
47 : 613 : }
48 : 1110 : },
49 : 435 : [&] {
50 : 435 : (void)auto_file.fclose();
51 : 435 : },
52 : 3929 : [&] {
53 : 3929 : ReadFromStream(fuzzed_data_provider, auto_file);
54 : 3929 : },
55 : 2744 : [&] {
56 : 2744 : WriteToStream(fuzzed_data_provider, auto_file);
57 : 2744 : });
58 : : }
59 : 410 : (void)auto_file.IsNull();
60 [ + + ]: 410 : if (fuzzed_data_provider.ConsumeBool()) {
61 [ + + ]: 54 : FILE* f = auto_file.release();
62 [ + + ]: 54 : if (f != nullptr) {
63 [ + - ]: 33 : fclose(f);
64 : : }
65 : : } else {
66 [ + - ]: 356 : (void)auto_file.fclose();
67 : : }
68 : 410 : }
|