qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: Stefan Hajnoczi <stefanha@gmail.com>
Cc: Kevin Wolf <kwolf@redhat.com>, Stefan Weil <sw@weilnetz.de>,
	Anthony Liguori <aliguori@us.ibm.com>,
	qemu-devel@nongnu.org, Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [Qemu-devel] [PATCH 1.1] virtio: Fix compiler warning for non Linux hosts
Date: Mon, 28 May 2012 13:48:11 +0100	[thread overview]
Message-ID: <20120528124811.GM19909@redhat.com> (raw)
In-Reply-To: <CAJSP0QURUf90n=o6+MjMRUA0ijUwWmwx-YW2MkbHMG2ats3nFw@mail.gmail.com>

On Mon, May 28, 2012 at 01:39:58PM +0100, Stefan Hajnoczi wrote:
> On Wed, May 23, 2012 at 5:03 PM, Stefan Weil <sw@weilnetz.de> wrote:
> > Am 23.05.2012 17:32, schrieb Kevin Wolf:
> >
> >> Am 23.05.2012 17:29, schrieb Stefan Weil:
> >>>
> >>> Am 23.05.2012 10:09, schrieb Stefan Hajnoczi:
> >>>>
> >>>> On Tue, May 22, 2012 at 10:23 PM, Stefan Weil<sw@weilnetz.de>   wrote:
> >>>>>
> >>>>> The local variables ret, i are only used if __linux__ is defined.
> >>>>>
> >>>>> Signed-off-by: Stefan Weil<sw@weilnetz.de>
> >>>>> ---
> >>>>>   hw/virtio-blk.c |    4 +++-
> >>>>>   1 file changed, 3 insertions(+), 1 deletion(-)
> >>>>
> >>>> The #ifdef __linux__ further down in the function declares the local
> >>>> hdr variable.  We could move ret and i down into the #ifdef instead of
> >>>> adding a new one.
> >>>
> >>> I noticed that, but declaring variables anywhere is C++, not C code.
> >>
> >> It's called C99.
> >>
> >> Kevin
> >>
> >
> > Maybe, but I already had patches rejected because of that style.
> > Did this policy change? I'd appreciate that!
> 
> Agreed, people have been asked to declare variables at the beginning
> of the scope.  I don't understand why, C99 allows them to be declared
> anywhere and it usually makes the code more readable IMO (you don't
> have to jump to the definition to check a variable's type).
>
> What's the problem with C99-style variable declarations anywhere in a function?

Not an issue in this particular patch, but declaration at point of
first use can cause you problems if the code uses 'goto' alot (eg
for return cleanup).

eg

void somefunc(void) {
   ...some code...

   if (...something bad...)
     goto cleanup;

   ... some code...

   char *bar = NULL;
   bar = strdup("Hello World");

   ...some code...

 cleanup:
   free(bar);
}

In that 'cleanup' label 'bar' will potentially be uninitialized,
because it is in scope, but the line where initialization takes
place may never been reached.

So if you do decide to make use of C99 style decl at first use,
then be sure to turn on the GCC warnings to detect these variable
initialization problems.

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

  reply	other threads:[~2012-05-28 12:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-22 21:23 [Qemu-devel] [PATCH 1.1] virtio: Fix compiler warning for non Linux hosts Stefan Weil
2012-05-23  8:09 ` Stefan Hajnoczi
2012-05-23 15:29   ` Stefan Weil
2012-05-23 15:32     ` Kevin Wolf
2012-05-23 16:03       ` Stefan Weil
2012-05-28 12:39         ` Stefan Hajnoczi
2012-05-28 12:48           ` Daniel P. Berrange [this message]
2012-05-28 12:52           ` Peter Maydell
2012-05-28 14:14   ` Andreas Färber

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=20120528124811.GM19909@redhat.com \
    --to=berrange@redhat.com \
    --cc=aliguori@us.ibm.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    --cc=sw@weilnetz.de \
    /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).