Skip to content

Commit 685c16f

Browse files
author
MarcoFalke
committed
Merge #21043: net: Avoid UBSan warning in ProcessMessage(...)
3ddbf22 util: Disallow negative mocktime (MarcoFalke) f5f2f97 net: Avoid UBSan warning in ProcessMessage(...) (practicalswift) Pull request description: Avoid UBSan warning in `ProcessMessage(...)`. Context: bitcoin/bitcoin#20380 (comment) (thanks Crypt-iQ!) ACKs for top commit: MarcoFalke: re-ACK 3ddbf22 only change is adding patch written by me ajtowns: ACK 3ddbf22 -- code review only Tree-SHA512: e8d7af0457ca86872b75a4e406c0a93aafd841c2962e244e147e748cc7ca118c56be0fdafe53765f4b291410030b2c3cc8f76f733b37a955d34fc885ab6037b9
2 parents e498aef + 3ddbf22 commit 685c16f

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

src/net_processing.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,6 +2489,9 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
24892489
bool fRelay = true;
24902490

24912491
vRecv >> nVersion >> nServiceInt >> nTime >> addrMe;
2492+
if (nTime < 0) {
2493+
nTime = 0;
2494+
}
24922495
nServices = ServiceFlags(nServiceInt);
24932496
if (!pfrom.IsInboundConn())
24942497
{

src/rpc/misc.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,13 @@ static RPCHelpMan signmessagewithprivkey()
365365
static RPCHelpMan setmocktime()
366366
{
367367
return RPCHelpMan{"setmocktime",
368-
"\nSet the local time to given timestamp (-regtest only)\n",
369-
{
370-
{"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, UNIX_EPOCH_TIME + "\n"
371-
" Pass 0 to go back to using the system time."},
372-
},
373-
RPCResult{RPCResult::Type::NONE, "", ""},
374-
RPCExamples{""},
368+
"\nSet the local time to given timestamp (-regtest only)\n",
369+
{
370+
{"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, UNIX_EPOCH_TIME + "\n"
371+
"Pass 0 to go back to using the system time."},
372+
},
373+
RPCResult{RPCResult::Type::NONE, "", ""},
374+
RPCExamples{""},
375375
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
376376
{
377377
if (!Params().IsMockableChain()) {
@@ -386,7 +386,10 @@ static RPCHelpMan setmocktime()
386386
LOCK(cs_main);
387387

388388
RPCTypeCheck(request.params, {UniValue::VNUM});
389-
int64_t time = request.params[0].get_int64();
389+
const int64_t time{request.params[0].get_int64()};
390+
if (time < 0) {
391+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Mocktime can not be negative: %s.", time));
392+
}
390393
SetMockTime(time);
391394
if (request.context.Has<NodeContext>()) {
392395
for (const auto& chain_client : request.context.Get<NodeContext>().chain_clients) {

src/util/time.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <util/time.h>
1111

12+
#include <util/check.h>
13+
1214
#include <atomic>
1315
#include <boost/date_time/posix_time/posix_time.hpp>
1416
#include <ctime>
@@ -18,7 +20,7 @@
1820

1921
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
2022

21-
static std::atomic<int64_t> nMockTime(0); //!< For unit testing
23+
static std::atomic<int64_t> nMockTime(0); //!< For testing
2224

2325
int64_t GetTime()
2426
{
@@ -46,6 +48,7 @@ template std::chrono::microseconds GetTime();
4648

4749
void SetMockTime(int64_t nMockTimeIn)
4850
{
51+
Assert(nMockTimeIn >= 0);
4952
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
5053
}
5154

test/functional/rpc_uptime.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import time
1111

1212
from test_framework.test_framework import BitcoinTestFramework
13+
from test_framework.util import assert_raises_rpc_error
1314

1415

1516
class UptimeTest(BitcoinTestFramework):
@@ -18,8 +19,12 @@ def set_test_params(self):
1819
self.setup_clean_chain = True
1920

2021
def run_test(self):
22+
self._test_negative_time()
2123
self._test_uptime()
2224

25+
def _test_negative_time(self):
26+
assert_raises_rpc_error(-8, "Mocktime can not be negative: -1.", self.nodes[0].setmocktime, -1)
27+
2328
def _test_uptime(self):
2429
wait_time = 10
2530
self.nodes[0].setmocktime(int(time.time() + wait_time))

0 commit comments

Comments
 (0)