[Spice-devel] [PATCH spice] Add missing struct field initializers

Daniel P. Berrange berrange at redhat.com
Wed Apr 25 05:31:12 PDT 2012


On Wed, Apr 25, 2012 at 01:46:20PM +0200, Hans de Goede wrote:
> Hi,
> 
> $subject: Why? The C standard guarantees that if any
> member of a struct (or array) gets initialized all non
> specifically initialized members will get initalized
> to a value of 0.

While you are correct that they get initialized to zero if
omitted, this does not mean your code is going to be bug
free by relying on those semantics.

For example consider code

  struct demo {
     int foo;
  };

and somewhere else miles away in the code, an initializer:

 struct demo v = { 1 };

So 'foo' get the value '1'

Now someone comes along and changes the struct to

  struct demo {
     int bar;
     int foo;
  };

Now 'bar gets the value 1, but 'foo' gets silently defaulted
to '0'. The initializer is still syntactically correct, but
it is clearly not semantically correct.

The use of -Wmissing-field-initializers protects us against
such problems.

Alternatively, we could switch to use named initalizers in
these cases instead, which avoids problems when people add
new struct members.

eg

  struct demo v { .foo = 1 };


> On 04/25/2012 12:00 PM, Daniel P. Berrange wrote:
> >From: "Daniel P. Berrange"<berrange at redhat.com>
> >
> >Signed-off-by: Daniel P. Berrange<berrange at redhat.com>
> >---
> >  client/x11/platform.cpp |   10 +++++-----
> >  1 files changed, 5 insertions(+), 5 deletions(-)
> >
> >diff --git a/client/x11/platform.cpp b/client/x11/platform.cpp
> >index f35d537..0669b69 100644
> >--- a/client/x11/platform.cpp
> >+++ b/client/x11/platform.cpp
> >@@ -128,12 +128,12 @@ struct clipboard_format_info {
> >
> >  static struct clipboard_format_info clipboard_formats[] = {
> >      { VD_AGENT_CLIPBOARD_UTF8_TEXT, { "UTF8_STRING",
> >-      "text/plain;charset=UTF-8", "text/plain;charset=utf-8", NULL }, },
> >-    { VD_AGENT_CLIPBOARD_IMAGE_PNG, { "image/png", NULL }, },
> >+      "text/plain;charset=UTF-8", "text/plain;charset=utf-8", NULL }, { 0 }, 0},
> >+    { VD_AGENT_CLIPBOARD_IMAGE_PNG, { "image/png", NULL }, { 0 }, 0},
> >      { VD_AGENT_CLIPBOARD_IMAGE_BMP, { "image/bmp", "image/x-bmp",
> >-      "image/x-MS-bmp", "image/x-win-bitmap", NULL }, },
> >-    { VD_AGENT_CLIPBOARD_IMAGE_TIFF, { "image/tiff", NULL }, },
> >-    { VD_AGENT_CLIPBOARD_IMAGE_JPG, { "image/jpeg", NULL }, },
> >+      "image/x-MS-bmp", "image/x-win-bitmap", NULL }, { 0 }, 0},
> >+    { VD_AGENT_CLIPBOARD_IMAGE_TIFF, { "image/tiff", NULL }, { 0 }, 0},
> >+    { VD_AGENT_CLIPBOARD_IMAGE_JPG, { "image/jpeg", NULL }, { 0 }, 0},
> >  };

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 :|


More information about the Spice-devel mailing list