qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Jamie Lokier <jamie@shareable.org>
To: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] Remove typedef for bool from eepro100.c
Date: Thu, 27 Aug 2009 15:59:34 +0100	[thread overview]
Message-ID: <20090827145934.GD31453@shareable.org> (raw)
In-Reply-To: <20090827144214.GA3803@1und1.de>

Reimar Döffinger wrote:
> On Thu, Aug 27, 2009 at 04:00:17PM +0200, Laurent Desnogues wrote:
> > On Thu, Aug 27, 2009 at 2:45 PM, Reimar
> > Döffinger<Reimar.Doeffinger@gmx.de> wrote:
> > > 1) stdbool bool is probably 4 bytes, not just 1 like char
> > 
> > It's one byte on my gcc 4.4.0.
> > 
> > > I suggest to just get rid of bool in this file, it is only used in 5
> > > places, i.e. change
> > >>        bool bit_el = ((command & 0x8000) != 0);
> > > to
> > >>        int bit_el = command & 0x8000;
> > 
> > This is dangerous if you start using bit_el in integer expressions
> > by accident (for instance using & or |).
> 
> Programming errors are dangerous in general. I don't see much of a
> point of cluttering the code with not really effective ways to hide
> their effects (unless you wanted to suggest using "bool bit_el =
> command & 0x8000;", I see that bool is already used in some places
> in qemu and performance doesn't matter here so it indeed shouldn't
> be a problem).

I've seen the "int flag = (x & FLAG); if (flag & otherflag)" bug
enough times to consider it one of those subtle things that
programmers don't notice easily.

A variation is "long flag = (x & FLAG); ... function(flag)" where
"function" takes an int argument and FLAG doesn't fit.  Or just "int
flag = (x & FLAG)".

My approach is to use "!= 0" if I think the variable really is
boolean, and only keep the original masked bit pattern if it's clear
in context that the variable is supposed to contain a bit pattern.

-- Jamie

  reply	other threads:[~2009-08-27 14:59 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-27 12:31 [Qemu-devel] [PATCH] Remove typedef for bool from eepro100.c Amit Shah
2009-08-27 12:45 ` Reimar Döffinger
2009-08-27 14:00   ` Laurent Desnogues
2009-08-27 14:42     ` Reimar Döffinger
2009-08-27 14:59       ` Jamie Lokier [this message]
2009-08-27 16:25   ` Stefan Weil
2009-08-27 16:43     ` Reimar Döffinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090827145934.GD31453@shareable.org \
    --to=jamie@shareable.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).