All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ian Campbell <Ian.Campbell@citrix.com>
To: Thanos Makatos <thanos.makatos@citrix.com>
Cc: "xen-devel@lists.xensource.com" <xen-devel@lists.xensource.com>
Subject: Re: [PATCH 2 of 7 v2] blktap3/tapback: Introduces functionality required to access XenStore
Date: Fri, 25 Jan 2013 14:13:05 +0000	[thread overview]
Message-ID: <1359123185.10051.71.camel@zakaz.uk.xensource.com> (raw)
In-Reply-To: <4B45B535F7F6BE4CB1C044ED5115CDDE0153A6E31CD8@LONPMAILBOX01.citrite.net>

On Fri, 2013-01-25 at 14:03 +0000, Thanos Makatos wrote:

> > > + */
> > > +static char *
> > > +vmprintf(const char * const fmt, va_list ap)
> > > +{
> > > +    char *s = NULL;
> > 
> > No need to init s.
> 
> Regarding initialising variables, isn't it a good programming practice
> to initialise variables even if they'll be shortly assigned a value?
> Won't the compiler easily optimise such cases?

It should but that's not why we avoid them.

>  Or does it make it too cumbersome to read?

The problem with "unnecessary" initialisations is that the prevent the
compiler's checks for use of uninitialised variables from functioning as
intended and catching bugs.

This is obviously a very simple contrived case but e.g. if you start
with:
	int x = 0, y = 0;

	x = get_a_value();
	y = another_value();

	use_some_values(x, y)
and someone adds a new case:
	int x = 0, y = 0;

	if (some_condition)
		x = get_a_value();
	else
		y = get_an_alternative_value();
	y = another_value();

	use_some_values(x, y);

But they've accidentally used the wrong variable in the new assignment!
So x is actually uninitialised, although it has been set arbitrarily to
0. This is the sort of thing which is easily missed (especially in more
complex example with multiple code paths etc)

If you just had "int x, y;" this would have resulted in "Variable x used
without being initialised" from the compiler.

This doesn't apply in all cases, and obviously it something of a matter
of taste/judgement, but there we go ;-)

> > However xenstore values generally can contain nulls and are not
> > necessarily NULL terminated (see docs/misc/xenstore.txt), but perhaps
> > the block protocol tightens this restriction such that you can rely on
> > NULL termination (at least in practice)?
> 
> Hm.. I saw this in tools/xenstore/xenstore.h and I though xs_read does end the string with a NULL:
> 
> /* Get the value of a single file, nul terminated.
>  * Returns a malloced value: call free() on it after use.
>  * len indicates length in bytes, not including terminator.
>  */
> void *xs_read(struct xs_handle *h, xs_transaction_t t,
> 	      const char *path, unsigned int *len);
> 
> Am I missing something?

I'm not sure, this isn't consistent with the wire protocol doc (at least
AIUI), but perhaps the library is just hiding some complexity for you.
This is one for Ian J I think.

Ian.

  reply	other threads:[~2013-01-25 14:13 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-04 12:14 [PATCH 0 of 7 v2] blktap3: Introduce the tapback daemon (most of blkback in user-space) Thanos Makatos
2013-01-04 12:14 ` [PATCH 1 of 7 v2] blktap3/tapback: Introduce core defines and structure definitions Thanos Makatos
2013-01-18 15:28   ` Ian Campbell
2013-01-25 13:25     ` Thanos Makatos
2013-01-04 12:14 ` [PATCH 2 of 7 v2] blktap3/tapback: Introduces functionality required to access XenStore Thanos Makatos
2013-01-18 16:08   ` Ian Campbell
2013-01-25 14:03     ` Thanos Makatos
2013-01-25 14:13       ` Ian Campbell [this message]
2013-01-04 12:14 ` [PATCH 3 of 7 v2] blktap3/tapback: Logging for the tapback daemon and libxenio Thanos Makatos
2013-01-04 12:14 ` [PATCH 4 of 7 v2] blktap3/tapback: Introduce back-end XenStore path handler Thanos Makatos
2013-01-18 17:07   ` Ian Campbell
2013-01-25 14:31     ` Thanos Makatos
2013-01-25 14:38       ` Ian Campbell
2013-01-04 12:14 ` [PATCH 5 of 7 v2] blktap3/tapback: Introduce front-end " Thanos Makatos
2013-01-18 17:17   ` Ian Campbell
2013-01-25 15:09     ` Thanos Makatos
2013-01-25 16:15       ` Ian Campbell
2013-01-04 12:14 ` [PATCH 6 of 7 v2] blktap3/tapback: Introduce the tapback daemon Thanos Makatos
2013-01-04 12:14 ` [PATCH 7 of 7 v2] blktap3/tapback: Introduce tapback daemon Makefile Thanos Makatos
2013-01-18 17:21   ` Ian Campbell

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=1359123185.10051.71.camel@zakaz.uk.xensource.com \
    --to=ian.campbell@citrix.com \
    --cc=thanos.makatos@citrix.com \
    --cc=xen-devel@lists.xensource.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.