From ff7a0ea10d9607bed0cdc68cc02c59ee6367e8e2 Mon Sep 17 00:00:00 2001 From: dmiller Date: Mon, 25 Nov 2013 18:35:48 +0000 Subject: [PATCH] Avoid runtime undefinedness due to integer overflow Using TIMEVAL_MSEC_SUBTRACT can lead to integer overflow when the times are far apart (such as epoch (0) or uninitialized timeval and "now"). Instead, calculate the "deadline" and use TIMEVAL_AFTER to test. --- scan_engine.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/scan_engine.cc b/scan_engine.cc index b23b97e40..45d782f6a 100644 --- a/scan_engine.cc +++ b/scan_engine.cc @@ -1921,9 +1921,11 @@ int UltraScanInfo::removeCompletedHosts() { HostScanStats *hss = NULL; int hostsRemoved = 0; bool timedout = false; + struct timeval compare; /* We don't want to run this all of the time */ - if ((unsigned) TIMEVAL_MSEC_SUBTRACT(now, lastCompletedHostRemoval) > completedHostLifetime / 2) { + TIMEVAL_MSEC_ADD(compare, lastCompletedHostRemoval, completedHostLifetime / 2); + if ( TIMEVAL_AFTER(now, compare) ) { for (hostI = completedHosts.begin(); hostI != completedHosts.end(); hostI = nxt) { nxt = hostI; nxt++; @@ -1933,7 +1935,8 @@ int UltraScanInfo::removeCompletedHosts() { if (hss == gstats->pinghost) continue; - if ((unsigned) TIMEVAL_MSEC_SUBTRACT(now, hss->completiontime) > completedHostLifetime) { + TIMEVAL_MSEC_ADD(compare, hss->completiontime, completedHostLifetime); + if ( TIMEVAL_AFTER(now, compare) ) { completedHosts.erase(hostI); hostsRemoved++; }