After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 577231 - [PATCH] add G_VALUE_INIT for full GValue variable initialization
[PATCH] add G_VALUE_INIT for full GValue variable initialization
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: gobject
unspecified
Other All
: Normal enhancement
: ---
Assigned To: gtkdev
gtkdev
: 654793 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2009-03-29 20:50 UTC by Andrew Paprocki
Modified: 2011-08-13 22:19 UTC
See Also:
GNOME target: ---
GNOME version: Unversioned Enhancement


Attachments
patch for adding G_VALUE_INIT (488 bytes, patch)
2009-03-29 20:52 UTC, Andrew Paprocki
rejected Details | Review
Add G_VALUE_INIT (2.49 KB, patch)
2011-07-17 20:23 UTC, Marc-Andre Lureau
accepted-commit_now Details | Review

Description Andrew Paprocki 2009-03-29 20:50:58 UTC
When a GValue is declared on the stack, the large code base I work
with usually does not properly initialize the struct. (e.g. "GValue v
= {0,};") This causes gcc to print warnings when 'gcc -Wall -Wextra'
is used due to -Wmissing-braces and -Wmissing-field-initializers.

I checked gvalue.h and I don't see a #define in there containing the
proper initializer list for a GValue to prevent gcc warnings. Rather
than duplicating "GValue v = {0, {{0}}};" everywhere, I'd like the
following to be added:

#define G_VALUE_INIT {0,{{0}}}

That way, code everywhere could simple use "GValue v = G_VALUE_INIT;"
and be sure that warnings will not be generated and that the
initializer list will always be kept up to date in the unlikely case
that it ever changes.

These are the warnings (or errors with -Werror) that print out if this
is not done correctly:

// if {0, } is used
error: missing initializer for member '_GValue::data'
// if {0, 0} is used
error: missing braces around initializer for '_GValue::<anonymous union> [2]'
// if {0, {0}} is used
error: missing braces around initializer for '_GValue::<anonymous union>'
Comment 1 Andrew Paprocki 2009-03-29 20:52:23 UTC
Created attachment 131646 [details] [review]
patch for adding G_VALUE_INIT
Comment 2 Marc-Andre Lureau 2011-07-17 20:22:45 UTC
*** Bug 654793 has been marked as a duplicate of this bug. ***
Comment 3 Marc-Andre Lureau 2011-07-17 20:23:42 UTC
Created attachment 192145 [details] [review]
Add G_VALUE_INIT

The implementation of GValue is not public or documented.  When
allocated on the stack, initializing a GValue is usually done as
documented with:

GValue value = { 0, };

There is lot code around (including WebKit) that added all the missing
fields, resulting in this ugly and non-obvious:

GValue value = { 0, { { 0 } } };

However, this doesn't play nice with -Wmissing-field-initializers for
example. Instead, we could provide a macro doing the right job.

I propose instead this:

GValue value = G_VALUE_INIT;

Similar to other _INIT macros in GLib.

https://bugzilla.gnome.org/show_bug.cgi?id=654793
Comment 4 Marc-Andre Lureau 2011-07-17 20:25:13 UTC
Review of attachment 131646 [details] [review]:

It's missing update to gobject doc section. Mine is more complete, marking this one as obsolete
Comment 5 Matthias Clasen 2011-07-18 23:46:16 UTC
Review of attachment 192145 [details] [review]:

{ 0, } is perfectly fine initializer.
People who feel that they have to blow that up to t { 0, { { 0 } } } only have 
themselves to blame.

Anyway, no objection to adding G_VALUE_INIT

::: gobject/gvalue.h
@@ +172,3 @@
+ *  </programlisting>
+ * </informalexample>
+ *

Should use the |[ ]| shorthand for informal examples here.