From f5f2f9716885e7548809e77f46b493c896a019bf Mon Sep 17 00:00:00 2001 From: practicalswift Date: Sun, 31 Jan 2021 21:03:05 +0000 Subject: net: Avoid UBSan warning in ProcessMessage(...) --- src/net_processing.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src') diff --git a/src/net_processing.cpp b/src/net_processing.cpp index b68453759a..8936b8109d 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -2489,6 +2489,9 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, bool fRelay = true; vRecv >> nVersion >> nServiceInt >> nTime >> addrMe; + if (nTime < 0) { + nTime = 0; + } nServices = ServiceFlags(nServiceInt); if (!pfrom.IsInboundConn()) { -- cgit v1.2.3 From 3ddbf22ed179a2db733af4b521bec5d2b13ebf4b Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Mon, 1 Feb 2021 14:57:34 +0000 Subject: util: Disallow negative mocktime Signed-off-by: practicalswift --- src/rpc/misc.cpp | 19 +++++++++++-------- src/util/time.cpp | 5 ++++- 2 files changed, 15 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index b75a7b8d26..38a0bddddb 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -365,13 +365,13 @@ static RPCHelpMan signmessagewithprivkey() static RPCHelpMan setmocktime() { return RPCHelpMan{"setmocktime", - "\nSet the local time to given timestamp (-regtest only)\n", - { - {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, UNIX_EPOCH_TIME + "\n" - " Pass 0 to go back to using the system time."}, - }, - RPCResult{RPCResult::Type::NONE, "", ""}, - RPCExamples{""}, + "\nSet the local time to given timestamp (-regtest only)\n", + { + {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, UNIX_EPOCH_TIME + "\n" + "Pass 0 to go back to using the system time."}, + }, + RPCResult{RPCResult::Type::NONE, "", ""}, + RPCExamples{""}, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { if (!Params().IsMockableChain()) { @@ -386,7 +386,10 @@ static RPCHelpMan setmocktime() LOCK(cs_main); RPCTypeCheck(request.params, {UniValue::VNUM}); - int64_t time = request.params[0].get_int64(); + const int64_t time{request.params[0].get_int64()}; + if (time < 0) { + throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Mocktime can not be negative: %s.", time)); + } SetMockTime(time); if (request.context.Has()) { for (const auto& chain_client : request.context.Get().chain_clients) { diff --git a/src/util/time.cpp b/src/util/time.cpp index e96972fe12..d130e4e4d4 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -9,6 +9,8 @@ #include +#include + #include #include #include @@ -18,7 +20,7 @@ void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); } -static std::atomic nMockTime(0); //!< For unit testing +static std::atomic nMockTime(0); //!< For testing int64_t GetTime() { @@ -46,6 +48,7 @@ template std::chrono::microseconds GetTime(); void SetMockTime(int64_t nMockTimeIn) { + Assert(nMockTimeIn >= 0); nMockTime.store(nMockTimeIn, std::memory_order_relaxed); } -- cgit v1.2.3