fuzz coverage

Coverage Report

Created: 2026-04-24 13:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/flatfile.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <flatfile.h>
7
8
#include <tinyformat.h>
9
#include <util/fs_helpers.h>
10
#include <util/log.h>
11
#include <util/overflow.h>
12
13
#include <stdexcept>
14
15
FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :
16
295k
    m_dir(std::move(dir)),
17
295k
    m_prefix(prefix),
18
295k
    m_chunk_size(chunk_size)
19
295k
{
20
295k
    if (chunk_size == 0) {
21
0
        throw std::invalid_argument("chunk_size must be positive");
22
0
    }
23
295k
}
24
25
std::string FlatFilePos::ToString() const
26
0
{
27
0
    return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);
Line
Count
Source
1172
0
#define strprintf tfm::format
28
0
}
29
30
fs::path FlatFileSeq::FileName(const FlatFilePos& pos) const
31
60.6M
{
32
60.6M
    return m_dir / fs::u8path(strprintf("%s%05u.dat", m_prefix, pos.nFile));
Line
Count
Source
1172
60.6M
#define strprintf tfm::format
33
60.6M
}
34
35
FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only) const
36
60.6M
{
37
60.6M
    if (pos.IsNull()) {
38
0
        return nullptr;
39
0
    }
40
60.6M
    fs::path path = FileName(pos);
41
60.6M
    fs::create_directories(path.parent_path());
42
60.6M
    FILE* file = fsbridge::fopen(path, read_only ? 
"rb"242k
:
"rb+"60.3M
);
43
60.6M
    if (!file && 
!read_only0
)
44
0
        file = fsbridge::fopen(path, "wb+");
45
60.6M
    if (!file) {
46
0
        LogError("Unable to open file %s", fs::PathToString(path));
Line
Count
Source
99
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
47
0
        return nullptr;
48
0
    }
49
60.6M
    if (pos.nPos && 
fseek(file, pos.nPos, 59.8M
SEEK_SET)59.8M
) {
50
0
        LogError("Unable to seek to position %u of %s", pos.nPos, fs::PathToString(path));
Line
Count
Source
99
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
51
0
        if (fclose(file) != 0) {
52
0
            LogError("Unable to close file %s", fs::PathToString(path));
Line
Count
Source
99
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
53
0
        }
54
0
        return nullptr;
55
0
    }
56
60.6M
    return file;
57
60.6M
}
58
59
size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) const
60
60.0M
{
61
60.0M
    out_of_space = false;
62
63
60.0M
    unsigned int n_old_chunks = CeilDiv(pos.nPos, m_chunk_size);
64
60.0M
    unsigned int n_new_chunks = CeilDiv(pos.nPos + add_size, m_chunk_size);
65
60.0M
    if (n_new_chunks > n_old_chunks) {
66
295k
        size_t old_size = pos.nPos;
67
295k
        size_t new_size = n_new_chunks * m_chunk_size;
68
295k
        size_t inc_size = new_size - old_size;
69
70
295k
        if (CheckDiskSpace(m_dir, inc_size)) {
71
295k
            FILE *file = Open(pos);
72
295k
            if (file) {
73
295k
                LogDebug(BCLog::VALIDATION, "Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);
Line
Count
Source
117
295k
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__)
Line
Count
Source
108
295k
    do {                                                               \
109
295k
        if (util::log::ShouldLog((category), (level))) {               \
110
0
            bool rate_limit{level >= BCLog::Level::Info};              \
111
0
            Assume(!rate_limit); /*Only called with the levels below*/ \
Line
Count
Source
125
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
112
0
            LogPrintLevel_(category, level, rate_limit, __VA_ARGS__);  \
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
113
0
        }                                                              \
114
295k
    } while (0)
74
295k
                AllocateFileRange(file, pos.nPos, inc_size);
75
295k
                if (fclose(file) != 0) {
76
0
                    LogError("Cannot close file %s%05u.dat after extending it with %u bytes", m_prefix, pos.nFile, new_size);
Line
Count
Source
99
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
77
0
                    return 0;
78
0
                }
79
295k
                return inc_size;
80
295k
            }
81
295k
        } else {
82
0
            out_of_space = true;
83
0
        }
84
295k
    }
85
59.7M
    return 0;
86
60.0M
}
87
88
bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize) const
89
0
{
90
0
    FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
91
0
    if (!file) {
92
0
        LogError("%s: failed to open file %d\n", __func__, pos.nFile);
Line
Count
Source
99
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
93
0
        return false;
94
0
    }
95
0
    if (finalize && !TruncateFile(file, pos.nPos)) {
96
0
        LogError("%s: failed to truncate file %d\n", __func__, pos.nFile);
Line
Count
Source
99
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
97
0
        if (fclose(file) != 0) {
98
0
            LogError("Failed to close file %d", pos.nFile);
Line
Count
Source
99
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
99
0
        }
100
0
        return false;
101
0
    }
102
0
    if (!FileCommit(file)) {
103
0
        LogError("%s: failed to commit file %d\n", __func__, pos.nFile);
Line
Count
Source
99
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
104
0
        if (fclose(file) != 0) {
105
0
            LogError("Failed to close file %d", pos.nFile);
Line
Count
Source
99
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
106
0
        }
107
0
        return false;
108
0
    }
109
0
    DirectoryCommit(m_dir);
110
111
0
    if (fclose(file) != 0) {
112
0
        LogError("Failed to close file %d after flush", pos.nFile);
Line
Count
Source
99
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
113
0
        return false;
114
0
    }
115
0
    return true;
116
0
}