]> git.proxmox.com Git - mirror_qemu.git/commitdiff
hw/timer/hpet.c: Avoid signed integer overflow which results in bugs on OSX
authorPeter Maydell <peter.maydell@linaro.org>
Mon, 9 Nov 2015 14:56:31 +0000 (14:56 +0000)
committerPeter Maydell <peter.maydell@linaro.org>
Mon, 9 Nov 2015 15:48:21 +0000 (15:48 +0000)
Signed integer overflow in C is undefined behaviour, and the compiler
is at liberty to assume it can never happen and optimize accordingly.
In particular, the subtractions in hpet_time_after() and hpet_time_after64()
were causing OSX clang to optimize the code such that it was prone to
hangs and complaints about the main loop stalling (presumably because
we were spending all our time trying to service very high frequency
HPET timer callbacks). The clang sanitizer confirms the UB:

hw/timer/hpet.c:119:26: runtime error: signed integer overflow: -2146967296 - 2147003978 cannot be represented in type 'int'

Fix this by doing the subtraction as an unsigned operation and then
converting to signed for the comparison.

Reported-by: Aaron Elkins <threcius@yahoo.com>
Cc: qemu-stable@nongnu.org
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Message-id: 1447080991-24995-1-git-send-email-peter.maydell@linaro.org

hw/timer/hpet.c

index 3037bef72e4ea79739ca0f609fc9a692c5990889..7f0391c7860ab18a5dc8a5adf81dd2b01b6c0332 100644 (file)
@@ -116,12 +116,12 @@ static uint32_t timer_enabled(HPETTimer *t)
 
 static uint32_t hpet_time_after(uint64_t a, uint64_t b)
 {
-    return ((int32_t)(b) - (int32_t)(a) < 0);
+    return ((int32_t)(b - a) < 0);
 }
 
 static uint32_t hpet_time_after64(uint64_t a, uint64_t b)
 {
-    return ((int64_t)(b) - (int64_t)(a) < 0);
+    return ((int64_t)(b - a) < 0);
 }
 
 static uint64_t ticks_to_ns(uint64_t value)