From 62a34cfd72b18dcffb89770c19460d0a91ef4974 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Fri, 24 Nov 2023 10:57:37 +0100 Subject: [PATCH] tcpreceiver: Better handling of "clock moved backward" events --- pdns/tcpreceiver.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pdns/tcpreceiver.cc b/pdns/tcpreceiver.cc index e76be90001..2ea87fbf7c 100644 --- a/pdns/tcpreceiver.cc +++ b/pdns/tcpreceiver.cc @@ -132,11 +132,13 @@ static int readnWithTimeout(int fd, void* buffer, unsigned int n, unsigned int i if (totalTimeout) { time_t now = time(nullptr); const auto elapsed = now - start; - if (elapsed > 0 && elapsed >= static_cast(remainingTotal)) { + if (elapsed >= static_cast(remainingTotal)) { throw NetworkError("Timeout while reading data"); } start = now; - remainingTotal -= elapsed; + if (elapsed > 0) { + remainingTotal -= elapsed; + } } } return n; @@ -197,10 +199,12 @@ static bool maxConnectionDurationReached(unsigned int maxConnectionDuration, tim { if (maxConnectionDuration) { time_t elapsed = time(nullptr) - start; - if (elapsed > 0 && elapsed >= maxConnectionDuration) { + if (elapsed >= maxConnectionDuration) { return true; } - remainingTime = static_cast(maxConnectionDuration - elapsed); + if (elapsed > 0) { + remainingTime = static_cast(maxConnectionDuration - elapsed); + } } return false; } -- 2.47.2