[libvirt PATCH v2 07/10] virhostuptime: Fix rounding in uptime calculation

Tim Wiederhake twiederh at redhat.com
Mon Feb 1 12:42:04 UTC 2021


"f + 0.5" does not round correctly for values very close to
".5" for every integer multiple, e.g. "0.499999975".

Found by clang-tidy's "bugprone-incorrect-roundings" check.

Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
Reviewed-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/virhostuptime.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/util/virhostuptime.c b/src/util/virhostuptime.c
index 696a38fbb5..7508a5a9b6 100644
--- a/src/util/virhostuptime.c
+++ b/src/util/virhostuptime.c
@@ -32,6 +32,8 @@
 #include "virtime.h"
 #include "virthread.h"
 
+#include <math.h>
+
 #define VIR_FROM_THIS VIR_FROM_NONE
 
 VIR_LOG_INIT("util.virhostuptime");
@@ -76,7 +78,7 @@ virHostGetBootTimeProcfs(unsigned long long *btime)
         return -EINVAL;
     }
 
-    *btime = now / 1000 - up + 0.5;
+    *btime = llround(now / 1000 - up);
 
     return 0;
 }
-- 
2.26.2




More information about the libvir-list mailing list