Skip to content

Commit 39d597d

Browse files
committed
Merge bitcoin/bitcoin#21659: net: flag relevant Sock methods with [[nodiscard]]
e286cd0 net: flag relevant Sock methods with [[nodiscard]] (Vasil Dimov) Pull request description: Flag relevant Sock methods with `[[nodiscard]]` to avoid issues like the one fixed in bitcoin/bitcoin#21631. ACKs for top commit: practicalswift: cr ACK e286cd0: the only changes made are additions of `[[nodiscard]]` and `(void)` where appropriate laanwj: Code review ACK e286cd0 Tree-SHA512: addc361968d24912bb625b42f4db557791556bf0ffad818252a89a32d76ac22758ec70f8282dcfbfd77eebec20a8e6bb7557c8ed08d50a58de95378c34955973
2 parents 0878128 + e286cd0 commit 39d597d

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/test/fuzz/i2p.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ FUZZ_TARGET_INIT(i2p, initialize_i2p)
3737
if (sess.Listen(conn)) {
3838
if (sess.Accept(conn)) {
3939
try {
40-
conn.sock->RecvUntilTerminator('\n', 10ms, interrupt, i2p::sam::MAX_MSG_SIZE);
40+
(void)conn.sock->RecvUntilTerminator('\n', 10ms, interrupt, i2p::sam::MAX_MSG_SIZE);
4141
} catch (const std::runtime_error&) {
4242
}
4343
}

src/test/sock_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ BOOST_AUTO_TEST_CASE(wait)
139139
Sock sock0(s[0]);
140140
Sock sock1(s[1]);
141141

142-
std::thread waiter([&sock0]() { sock0.Wait(24h, Sock::RECV); });
142+
std::thread waiter([&sock0]() { (void)sock0.Wait(24h, Sock::RECV); });
143143

144144
BOOST_REQUIRE_EQUAL(sock1.Send("a", 1, 0), 1);
145145

@@ -162,7 +162,7 @@ BOOST_AUTO_TEST_CASE(recv_until_terminator_limit)
162162
// BOOST_CHECK_EXCEPTION() writes to some variables shared with the main thread which
163163
// creates a data race. So mimic it manually.
164164
try {
165-
sock_recv.RecvUntilTerminator('\n', timeout, interrupt, max_data);
165+
(void)sock_recv.RecvUntilTerminator('\n', timeout, interrupt, max_data);
166166
} catch (const std::runtime_error& e) {
167167
threw_as_expected = HasReason("too many bytes without a terminator")(e);
168168
}

src/util/sock.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void Sock::SendComplete(const std::string& data,
179179
// Wait for a short while (or the socket to become ready for sending) before retrying
180180
// if nothing was sent.
181181
const auto wait_time = std::min(deadline - now, std::chrono::milliseconds{MAX_WAIT_FOR_IO});
182-
Wait(wait_time, SEND);
182+
(void)Wait(wait_time, SEND);
183183
}
184184
}
185185

@@ -262,7 +262,7 @@ std::string Sock::RecvUntilTerminator(uint8_t terminator,
262262

263263
// Wait for a short while (or the socket to become ready for reading) before retrying.
264264
const auto wait_time = std::min(deadline - now, std::chrono::milliseconds{MAX_WAIT_FOR_IO});
265-
Wait(wait_time, RECV);
265+
(void)Wait(wait_time, RECV);
266266
}
267267
}
268268

src/util/sock.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Sock
6464
* Get the value of the contained socket.
6565
* @return socket or INVALID_SOCKET if empty
6666
*/
67-
virtual SOCKET Get() const;
67+
[[nodiscard]] virtual SOCKET Get() const;
6868

6969
/**
7070
* Get the value of the contained socket and drop ownership. It will not be closed by the
@@ -82,26 +82,29 @@ class Sock
8282
* send(2) wrapper. Equivalent to `send(this->Get(), data, len, flags);`. Code that uses this
8383
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
8484
*/
85-
virtual ssize_t Send(const void* data, size_t len, int flags) const;
85+
[[nodiscard]] virtual ssize_t Send(const void* data, size_t len, int flags) const;
8686

8787
/**
8888
* recv(2) wrapper. Equivalent to `recv(this->Get(), buf, len, flags);`. Code that uses this
8989
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
9090
*/
91-
virtual ssize_t Recv(void* buf, size_t len, int flags) const;
91+
[[nodiscard]] virtual ssize_t Recv(void* buf, size_t len, int flags) const;
9292

9393
/**
9494
* connect(2) wrapper. Equivalent to `connect(this->Get(), addr, addrlen)`. Code that uses this
9595
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
9696
*/
97-
virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
97+
[[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
9898

9999
/**
100100
* getsockopt(2) wrapper. Equivalent to
101101
* `getsockopt(this->Get(), level, opt_name, opt_val, opt_len)`. Code that uses this
102102
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
103103
*/
104-
virtual int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const;
104+
[[nodiscard]] virtual int GetSockOpt(int level,
105+
int opt_name,
106+
void* opt_val,
107+
socklen_t* opt_len) const;
105108

106109
using Event = uint8_t;
107110

@@ -124,9 +127,9 @@ class Sock
124127
* value of `true` and `occurred` being set to 0.
125128
* @return true on success and false otherwise
126129
*/
127-
virtual bool Wait(std::chrono::milliseconds timeout,
128-
Event requested,
129-
Event* occurred = nullptr) const;
130+
[[nodiscard]] virtual bool Wait(std::chrono::milliseconds timeout,
131+
Event requested,
132+
Event* occurred = nullptr) const;
130133

131134
/* Higher level, convenience, methods. These may throw. */
132135

@@ -154,17 +157,17 @@ class Sock
154157
* @throws std::runtime_error if the operation cannot be completed. In this case some bytes may
155158
* have been consumed from the socket.
156159
*/
157-
virtual std::string RecvUntilTerminator(uint8_t terminator,
158-
std::chrono::milliseconds timeout,
159-
CThreadInterrupt& interrupt,
160-
size_t max_data) const;
160+
[[nodiscard]] virtual std::string RecvUntilTerminator(uint8_t terminator,
161+
std::chrono::milliseconds timeout,
162+
CThreadInterrupt& interrupt,
163+
size_t max_data) const;
161164

162165
/**
163166
* Check if still connected.
164167
* @param[out] errmsg The error string, if the socket has been disconnected.
165168
* @return true if connected
166169
*/
167-
virtual bool IsConnected(std::string& errmsg) const;
170+
[[nodiscard]] virtual bool IsConnected(std::string& errmsg) const;
168171

169172
protected:
170173
/**

0 commit comments

Comments
 (0)