fuzz coverage

Coverage Report

Created: 2025-10-29 15:27

/Users/eugenesiegel/btc/bitcoin/src/util/tokenpipe.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2021-present 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
#include <util/tokenpipe.h>
5
6
#include <bitcoin-build-config.h> // IWYU pragma: keep
7
8
#ifndef WIN32
9
10
#include <cerrno>
11
#include <fcntl.h>
12
#include <optional>
13
#include <unistd.h>
14
15
TokenPipeEnd TokenPipe::TakeReadEnd()
16
51.2k
{
17
51.2k
    TokenPipeEnd res(m_fds[0]);
18
51.2k
    m_fds[0] = -1;
19
51.2k
    return res;
20
51.2k
}
21
22
TokenPipeEnd TokenPipe::TakeWriteEnd()
23
51.2k
{
24
51.2k
    TokenPipeEnd res(m_fds[1]);
25
51.2k
    m_fds[1] = -1;
26
51.2k
    return res;
27
51.2k
}
28
29
205k
TokenPipeEnd::TokenPipeEnd(int fd) : m_fd(fd)
30
205k
{
31
205k
}
32
33
TokenPipeEnd::~TokenPipeEnd()
34
205k
{
35
205k
    Close();
36
205k
}
37
38
int TokenPipeEnd::TokenWrite(uint8_t token)
39
0
{
40
0
    while (true) {
41
0
        ssize_t result = write(m_fd, &token, 1);
42
0
        if (result < 0) {
43
            // Failure. It's possible that the write was interrupted by a signal,
44
            // in that case retry.
45
0
            if (errno != EINTR) {
46
0
                return TS_ERR;
47
0
            }
48
0
        } else if (result == 0) {
49
0
            return TS_EOS;
50
0
        } else { // ==1
51
0
            return 0;
52
0
        }
53
0
    }
54
0
}
55
56
int TokenPipeEnd::TokenRead()
57
0
{
58
0
    uint8_t token;
59
0
    while (true) {
60
0
        ssize_t result = read(m_fd, &token, 1);
61
0
        if (result < 0) {
62
            // Failure. Check if the read was interrupted by a signal,
63
            // in that case retry.
64
0
            if (errno != EINTR) {
65
0
                return TS_ERR;
66
0
            }
67
0
        } else if (result == 0) {
68
0
            return TS_EOS;
69
0
        } else { // ==1
70
0
            return token;
71
0
        }
72
0
    }
73
0
    return token;
74
0
}
75
76
void TokenPipeEnd::Close()
77
307k
{
78
307k
    if (m_fd != -1) 
close(m_fd)102k
;
79
307k
    m_fd = -1;
80
307k
}
81
82
std::optional<TokenPipe> TokenPipe::Make()
83
51.2k
{
84
51.2k
    int fds[2] = {-1, -1};
85
#if HAVE_O_CLOEXEC && HAVE_DECL_PIPE2
86
    if (pipe2(fds, O_CLOEXEC) != 0) {
87
        return std::nullopt;
88
    }
89
#else
90
51.2k
    if (pipe(fds) != 0) {
91
0
        return std::nullopt;
92
0
    }
93
51.2k
#endif
94
51.2k
    return TokenPipe(fds);
95
51.2k
}
96
97
TokenPipe::~TokenPipe()
98
102k
{
99
102k
    Close();
100
102k
}
101
102
void TokenPipe::Close()
103
102k
{
104
102k
    if (m_fds[0] != -1) 
close(m_fds[0])0
;
105
102k
    if (m_fds[1] != -1) 
close(m_fds[1])0
;
106
102k
    m_fds[0] = m_fds[1] = -1;
107
102k
}
108
109
#endif // WIN32