From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48462) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dwEf6-0003BQ-Um for qemu-devel@nongnu.org; Sun, 24 Sep 2017 17:42:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dwEf2-0004ls-Rn for qemu-devel@nongnu.org; Sun, 24 Sep 2017 17:42:04 -0400 From: Michael Tokarev Date: Mon, 25 Sep 2017 00:22:43 +0300 Message-Id: <7ac5511eea4f8ee511cd78e47a92436b0bdb3627.1506288070.git.mjt@msgid.tls.msk.ru> In-Reply-To: References: In-Reply-To: References: Subject: [Qemu-devel] [PULL 25/31] osdep: Fix ROUND_UP(64-bit, 32-bit) List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Eric Blake , qemu-trivial@nongnu.org, qemu-stable@nongnu.org, Michael Tokarev From: Eric Blake 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: qemu-trivial@nongnu.org CC: qemu-stable@nongnu.org Signed-off-by: Eric Blake Reviewed-by: Laszlo Ersek Reviewed-by: Richard Henderson Signed-off-by: Michael Tokarev --- 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