Received: from eggs.gnu.org ([2001:4830:134:3::10]:45961) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bN0UO-0000n1-3L for qemu-devel@nongnu.org; Tue, 12 Jul 2016 12:24:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bN0UI-0003lN-2r for qemu-devel@nongnu.org; Tue, 12 Jul 2016 12:24:51 -0400 Received: from mail-lf0-x242.google.com ([2a00:1450:4010:c07::242]:36026) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bN0UH-0003lJ-R3 for qemu-devel@nongnu.org; Tue, 12 Jul 2016 12:24:46 -0400 Received: by mail-lf0-x242.google.com with SMTP id 33so1471339lfw.3 for ; Tue, 12 Jul 2016 09:24:45 -0700 (PDT) Sender: Paolo Bonzini References: <1468306113-847-1-git-send-email-famz@redhat.com> <578512BB.3040003@redhat.com> From: Paolo Bonzini Message-ID: <9832869c-5e80-6636-8618-e4251f649726@redhat.com> Date: Tue, 12 Jul 2016 18:24:40 +0200 MIME-Version: 1.0 In-Reply-To: <578512BB.3040003@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH] util: Fix MIN_NON_ZERO List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake , Fam Zheng , qemu-devel@nongnu.org Cc: Miroslav Rezanina , Peter Lieven , Stefan Hajnoczi , Max Reitz On 12/07/2016 17:54, Eric Blake wrote: > On 07/12/2016 12:48 AM, Fam Zheng wrote: >> MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it. > > Huh? > > Old expansion, in various stages: > > (((0) != 0 && (0) < (1)) ? (0) : (1)) > ((0 && 1) ? 0 : 1) > (0 ? 0 : 1) > 1 > > Maybe you meant MIN_NON_ZERO(1, 0), which evaluates to: > > (((1) != 0 && (1) < (0)) ? (1) : (0)) > ((1 && 0) ? 1 : 0) > (0 ? 1 : 0) > 0 > > in which case, you are correct that there is a bug. Commit message fixed, patch queued. Paolo >> >> Reported-by: Miroslav Rezanina >> Signed-off-by: Fam Zheng >> --- >> include/qemu/osdep.h | 3 ++- >> 1 file changed, 2 insertions(+), 1 deletion(-) >> >> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h >> index e63da28..e4c6ae6 100644 >> --- a/include/qemu/osdep.h >> +++ b/include/qemu/osdep.h >> @@ -151,7 +151,8 @@ extern int daemon(int, int); >> /* Minimum function that returns zero only iff both values are zero. >> * Intended for use with unsigned values only. */ >> #ifndef MIN_NON_ZERO >> -#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b)) >> +#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \ >> + ((b) == 0 ? (a) : (MIN(a, b)))) > > Another way might be: > > (!(a) == !(b) ? MIN(a, b) : (a) + (b)) > > but you have to put more mental thought into that. I'm just fine with > your version. > > With the commit message fixed, > Reviewed-by: Eric Blake >