From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=54525 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ou72q-0002QJ-EZ for qemu-devel@nongnu.org; Fri, 10 Sep 2010 13:05:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1Ou72m-0001jj-00 for qemu-devel@nongnu.org; Fri, 10 Sep 2010 13:05:48 -0400 Received: from mail-yx0-f173.google.com ([209.85.213.173]:55797) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Ou72l-0001je-Sm for qemu-devel@nongnu.org; Fri, 10 Sep 2010 13:05:43 -0400 Received: by yxs7 with SMTP id 7so1413315yxs.4 for ; Fri, 10 Sep 2010 10:05:43 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <1284127451-22092-2-git-send-email-Jes.Sorensen@redhat.com> References: <1284127451-22092-1-git-send-email-Jes.Sorensen@redhat.com> <1284127451-22092-2-git-send-email-Jes.Sorensen@redhat.com> From: Blue Swirl Date: Fri, 10 Sep 2010 17:05:22 +0000 Message-ID: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] Re: [PATCH 1/1] Avoid compiler error when building block/blkdebug.c with -Wtype-limits List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jes.Sorensen@redhat.com Cc: qemu-devel@nongnu.org On Fri, Sep 10, 2010 at 2:04 PM, wrote: > From: Jes Sorensen > > Signed-off-by: Jes Sorensen > --- > =C2=A0block/blkdebug.c | =C2=A0 =C2=A07 ++++++- > =C2=A01 files changed, 6 insertions(+), 1 deletions(-) > > diff --git a/block/blkdebug.c b/block/blkdebug.c > index 2a63df9..17d796d 100644 > --- a/block/blkdebug.c > +++ b/block/blkdebug.c > @@ -439,7 +439,12 @@ static void blkdebug_debug_event(BlockDriverState *b= s, BlkDebugEvent event) > =C2=A0 =C2=A0 struct BlkdebugRule *rule; > =C2=A0 =C2=A0 BlkdebugVars old_vars =3D s->vars; > > - =C2=A0 =C2=A0if (event < 0 || event >=3D BLKDBG_EVENT_MAX) { > + =C2=A0 =C2=A0/* > + =C2=A0 =C2=A0 * enum is not guaranteed to be signed on all archs, so ca= st to > + =C2=A0 =C2=A0 * int before the comparison against zero to avoid compile= r > + =C2=A0 =C2=A0 * warning when building with -Wtype-limits > + =C2=A0 =C2=A0 */ > + =C2=A0 =C2=A0if ((int)event < 0 || event >=3D BLKDBG_EVENT_MAX) { I changed 'if' to 'assert' in my version because the check could only fail due to an internal error: http://lists.nongnu.org/archive/html/qemu-devel/2010-09/msg00239.html There's also Michael's version with a cast to unsigned int. It's a bit simpler, the generated code is the same: $ cat check.c int f(int x) { if (x < 0 || x > 1000) return 1; return 0; } int g(int x) { if ((unsigned)x > 1000) return 1; return 0; } $ gcc -O -S check.c $ head -20 check.s .file "check.c" .text .globl f .type f, @function f: .LFB2: cmpl $1000, %edi seta %al movzbl %al, %eax ret .LFE2: .size f, .-f .globl g .type g, @function g: .LFB3: cmpl $1000, %edi seta %al movzbl %al, %eax ret