gio-test: fix leak & maybe-uninitialized warning

GCC 8 on F29 complains:

../tests/gio-test.c: In function ‘main’:
../tests/gio-test.c:375:7: warning: ‘id’ may be used uninitialized in this function [-Wmaybe-uninitialized]
       g_free (id);
       ^~~~~~~~~~~

In the normal case, when run without arguments, 'id' will be assigned
exactly once, so all is fine. If run with argument '0', 'id' will never
be assigned, so the warning is legit; but in that case the test program
will never exit. If run with any argument greater than 1, 'id' will be
assigned more than once but only the last incarnation will be freed.

Tweak the scope of the variable to match its use, and arrange for it to
be freed when its watch is destroyed.
This commit is contained in:
Will Thompson 2018-11-26 15:36:09 +00:00
parent 4326d5e27d
commit b45d911cc6
No known key found for this signature in database
GPG Key ID: 3422DC0D7AD482A7

View File

@ -266,7 +266,6 @@ main (int argc,
GIOChannel *my_read_channel;
gchar *cmdline;
guint *id;
int i;
#ifdef G_OS_WIN32
GTimeVal start, end;
@ -316,6 +315,7 @@ main (int argc,
for (i = 0; i < nkiddies; i++)
{
int pipe_to_sub[2], pipe_from_sub[2];
guint *id;
if (pipe (pipe_to_sub) == -1 ||
pipe (pipe_from_sub) == -1)
@ -328,10 +328,11 @@ main (int argc,
id = g_new (guint, 1);
*id =
g_io_add_watch (my_read_channel,
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
recv_message,
id);
g_io_add_watch_full (my_read_channel,
G_PRIORITY_DEFAULT,
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
recv_message,
id, g_free);
nrunning++;
@ -372,7 +373,6 @@ main (int argc,
g_main_loop_unref (main_loop);
g_free (seqtab);
g_free (id);
}
else if (argc == 3)
{