diff mbox

[COMMITTED] Fix test-skeleton C99 designed initialization

Message ID 1472245120-18961-1-git-send-email-adhemerval.zanella@linaro.org
State New
Headers show

Commit Message

Adhemerval Zanella Netto Aug. 26, 2016, 8:58 p.m. UTC
ISO C forbids empty initializer braces (6.7.9 initializer-list must
contain at least one initializer). However GCC allows it, generating
a warning depending of the version.

With GCC 4.8 on ARM I noticed tst-initializers1.c fails to build with:

In file included from tst-initializers1.c:60:0:
../test-skeleton.c: In function 'delayed_exit_thread':
../test-skeleton.c:687:10: error: missing initializer for field 'tv_sec' of 'struct timespec' [-Werror=missing-field-initializers]
   struct timespec remaining = {}

While with GCC 5.1 the same warning is just spilled with -pedantic.
To be safe this patch just zero initialize the struct as expected.

Tested on armhf.

	* test-skeleton.c (delayed_exit_thread): Add initializer on struct
	timespec C99 designated initialization.
---
 test-skeleton.c | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

Comments

Paul Eggert Aug. 27, 2016, 7:42 p.m. UTC | #1
Adhemerval Zanella wrote:
> -  struct timespec remaining = {};
> +  struct timespec remaining = { 0 };

I suggest appending a comma; this style is often used in GNU apps as a cue to 
the reader that the remaining zeros are elided. Like this:

    struct timespec remaining = { 0, };
Florian Weimer Aug. 29, 2016, 9:21 a.m. UTC | #2
On 08/26/2016 10:58 PM, Adhemerval Zanella wrote:
> ISO C forbids empty initializer braces (6.7.9 initializer-list must
> contain at least one initializer). However GCC allows it, generating
> a warning depending of the version.
>
> With GCC 4.8 on ARM I noticed tst-initializers1.c fails to build with:
>
> In file included from tst-initializers1.c:60:0:
> ../test-skeleton.c: In function 'delayed_exit_thread':
> ../test-skeleton.c:687:10: error: missing initializer for field 'tv_sec' of 'struct timespec' [-Werror=missing-field-initializers]
>    struct timespec remaining = {}

Thanks for fixing this.

The root cause is that we compile tst-initializers1 with -Wall -W 
-Werror (presumably to catch initializer issues in <pthread.h>).  If we 
keep expanding test-skeleton.c, this will make it quite difficult to 
keep supporting many different compilers.  I guess this is another 
reason to switch to something like libtest, with separate compilation of 
these test helpers.

Florian
diff mbox

Patch

diff --git a/test-skeleton.c b/test-skeleton.c
index b24ce1d..913a335 100644
--- a/test-skeleton.c
+++ b/test-skeleton.c
@@ -684,7 +684,7 @@  delayed_exit_thread (void *seconds_as_ptr)
 {
   int seconds = (uintptr_t) seconds_as_ptr;
   struct timespec delay = { seconds, 0 };
-  struct timespec remaining = {};
+  struct timespec remaining = { 0 };
   if (nanosleep (&delay, &remaining) != 0)
     {
       printf ("error: nanosleep: %m\n");