[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-trivial] [PULL 25/31] osdep: Fix ROUND_UP(64-bit, 32-bit)
From: |
Michael Tokarev |
Subject: |
[Qemu-trivial] [PULL 25/31] osdep: Fix ROUND_UP(64-bit, 32-bit) |
Date: |
Mon, 25 Sep 2017 00:22:43 +0300 |
From: Eric Blake <address@hidden>
When using bit-wise operations that exploit the power-of-two
nature of the second argument of ROUND_UP(), we still need to
ensure that the mask is as wide as the first argument (done
by using a ternary to force proper arithmetic promotion).
Unpatched, ROUND_UP(2ULL*1024*1024*1024*1024, 512U) produces 0,
instead of the intended 2TiB, because negation of an unsigned
32-bit quantity followed by widening to 64-bits does not
sign-extend the mask.
Broken since its introduction in commit 292c8e50 (v1.5.0).
Callers that passed the same width type to both macro parameters,
or that had other code to ensure the first parameter's maximum
runtime value did not exceed the second parameter's width, are
unaffected, but I did not audit to see which (if any) existing
clients of the macro could trigger incorrect behavior (I found
the bug while adding a new use of the macro).
While preparing the patch, checkpatch complained about poor
spacing, so I also fixed that here and in the nearby DIV_ROUND_UP.
CC: address@hidden
CC: address@hidden
Signed-off-by: Eric Blake <address@hidden>
Reviewed-by: Laszlo Ersek <address@hidden>
Reviewed-by: Richard Henderson <address@hidden>
Signed-off-by: Michael Tokarev <address@hidden>
---
include/qemu/osdep.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index 72b75bf044..9dd318a7dd 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -205,13 +205,13 @@ extern int daemon(int, int);
/* Round number up to multiple. Requires that d be a power of 2 (see
* QEMU_ALIGN_UP for a safer but slower version on arbitrary
- * numbers) */
+ * numbers); works even if d is a smaller type than n. */
#ifndef ROUND_UP
-#define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
+#define ROUND_UP(n, d) (((n) + (d) - 1) & -(0 ? (n) : (d)))
#endif
#ifndef DIV_ROUND_UP
-#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
+#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
#endif
/*
--
2.11.0
- [Qemu-trivial] [PULL 26/31] hw/display/virtio-gpu: Put the virtio-gpu-device into the display category, (continued)
- [Qemu-trivial] [PULL 26/31] hw/display/virtio-gpu: Put the virtio-gpu-device into the display category, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 10/31] MAINTAINERS: add missing SSI entries, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 21/31] remove trailing whitespace from qemu-options.hx, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 12/31] MAINTAINERS: add missing AIO entry, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 17/31] filter-mirror: segfault when specifying non existent device, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 16/31] MAINTAINERS: update docs/interop/ entries, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 14/31] MAINTAINERS: add missing Cryptography entry, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 11/31] MAINTAINERS: add missing entries for throttling infra, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 28/31] dma/i82374: avoid double creation of i82374 device, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 03/31] MAINTAINERS: add missing ARM entries, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 25/31] osdep: Fix ROUND_UP(64-bit, 32-bit),
Michael Tokarev <=
- [Qemu-trivial] [PULL 24/31] target/xtensa: Use the pre-defined MEMTXATTRS_UNSPECIFIED macro, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 13/31] MAINTAINERS: add missing entry for Generic Loader, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 05/31] MAINTAINERS: add missing entry for vhost, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 08/31] MAINTAINERS: add missing qcow2 entry, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 15/31] MAINTAINERS: update docs/devel/ entries, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 18/31] util/qemu-thread-posix.c: Replace OS ifdefs with CONFIG_HAVE_SEM_TIMEDWAIT, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 23/31] trivial: Add missing "-m" parameter in docs/memory-hotplug.txt, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 30/31] Drop gld linker usage on SunOS, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 20/31] hw/display/xenfb.c: Add trace_xenfb_key_event, Michael Tokarev, 2017/09/24
- [Qemu-trivial] [PULL 22/31] chardev/baum: fix baum that releases brlapi twice, Michael Tokarev, 2017/09/24