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 745723 - -Wunused-but-set-variable work-around no longer sufficient
-Wunused-but-set-variable work-around no longer sufficient
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: build
2.42.x
Other Mac OS
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2015-03-06 08:07 UTC by Daniel Macks
Modified: 2017-09-11 21:24 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Fix some -Wself-assign warnings (6.37 KB, patch)
2015-03-20 03:16 UTC, Daniel Macks
committed Details | Review
Fix more -Wself-assign warnings (2.28 KB, patch)
2015-04-23 05:29 UTC, Daniel Macks
committed Details | Review

Description Daniel Macks 2015-03-06 08:07:40 UTC
Commit 0729260141bb585943ad1c6efa8ab7ee9058b0aa avoided a bunch of warnings triggered when a variable is set but not used (-Wunused-but-set-variable). The trick was to assign the variable to itself. Clang now knows that's a hack that hides the actual situation and warns specifically about it (-Wself-assign). So the unused-variable arms race continues. For example, glib-2.42.2 on OS X 10.8:

  CC       httpd.o
httpd.c:75:11: warning: explicitly assigning a variable of type 'char *' to itself [-Wself-assign]
  version = version; /* To avoid -Wunused-but-set-variable */
  ~~~~~~~ ^ ~~~~~~~
1 warning generated.

That one in particular looks like it's a valid case of a useless variable:

  version = NULL;
  tmp = strchr (escaped, ' ');
  if (tmp != NULL)
    {
      *tmp = 0;
      version = tmp + 1;
    }
  version = version; /* To avoid -Wunused-but-set-variable */

version is a local variable in the function and has no other use. Ripping it out entirely silences the warning (and does not lead to any others).
Comment 1 Dan Winship 2015-03-06 14:15:01 UTC
Or we could actually check the version; if tmp is NULL, then send_error(out, 400, "Bad Request"), and if version doesn't start with "HTTP/1.", then send_error(out, 505, "HTTP Version Not Supported");
Comment 2 Daniel Macks 2015-03-07 21:48:35 UTC
Also quite a reasonable approach. Should I file a separate bug for each of this type of warning (at least some look like simple removal would work, but no idea if some should remain as placeholders for future code, etc.), or keep all here in this one?
Comment 3 Dan Winship 2015-03-10 02:44:05 UTC
as long as none of them gets too big or controversial I think it's fine to do multiple related patches here
Comment 4 Daniel Macks 2015-03-16 06:26:04 UTC
Next one is in gthreadedresolver.c g_resolver_records_from_res_query(), in the "Read answers" loop:

 while (count-- && p < end)
    {
      p += dn_expand (answer, end, p, namebuf, sizeof (namebuf));
      GETSHORT (type, p);
      GETSHORT (qclass, p);
      GETLONG  (ttl, p);
      ttl = ttl; /* To avoid -Wunused-but-set-variable */
      GETSHORT (rdlength, p);


ttl is never used. Data at p is parsed sequentially, so the ttl value needs to be extracted, but then the loop here doesn't use it. GETLONG is NS_GET32, which reads a few bytes from p to create ttl and then increments p to the next chunk of data. I don't know if there is anything useful for ttl here, but if we just want to ignore this value, just increment p with something like (based on NS_GET32 around line 317), which is also comparable to what my /usr/include/nameser.h has):

#define NS_SKIP32(cp) do { \
	(cp) += NS_INT32SZ; \
} while (/*CONSTCOND*/0)
Comment 5 Daniel Macks 2015-03-16 06:54:43 UTC
Next one is in gdbusmessage.c parse_value_from_blob(), where is_leaf is set to various boolean values depending on the passed blob type. This variable is apparently only a status used for debugging...the only place its value is used is around line 1879:

#ifdef DEBUG_SERIALIZER
  [do things with is_leaf]
#else
  is_leaf = is_leaf;  // <-- triggers warning
#endif

Probably all uses of is_leaf should be completely protected by that token.
Comment 6 Daniel Macks 2015-03-16 07:06:11 UTC
Next one is gdusauth.c _g_dbus_auth_run_server(), where there are two instances (depending on G_OS_UNIX) of:

      byte = g_data_input_stream_read_byte (dis, cancellable, &local_error);
      byte = byte; /* To avoid -Wunused-but-set-variable */

There is a comment near there:

  /* first read the NUL-byte (TODO: read credentials if using a unix domain socket) */

so the code is just reading it into byte and then ignoring it. Should it check that it's actually NUL? Or should it just read it in a void context rather than assigning it to a dummy variable?
Comment 7 Daniel Macks 2015-03-16 07:16:17 UTC
A trivial one in gdbusauthmechanismsha1.c keyring_generate_entry() at line 749:

line_when = g_ascii_strtoll (tokens[1], &endp, 10);
[do some things]
line_when = line_when; /* To avoid -Wunused-but-set-variable */
[do some more things]
if (line_when > now)

so it actually *is* used after being set. Previously was probably just awaiting someone to implement the handler for its value. Simply removing the line_when=line_when line solves it.

There are two others in this .c, not as obvious as this one.
Comment 8 Dan Winship 2015-03-16 18:51:24 UTC
Want to wrap this all up in a patch?

(In reply to Daniel Macks from comment #4)
>       GETLONG  (ttl, p);
>       ttl = ttl; /* To avoid -Wunused-but-set-variable */

probably "p += 4" is sufficient

(In reply to Daniel Macks from comment #5)
> Probably all uses of is_leaf should be completely protected by that token.

yes

(In reply to Daniel Macks from comment #6)
> so the code is just reading it into byte and then ignoring it. Should it
> check that it's actually NUL? Or should it just read it in a void context
> rather than assigning it to a dummy variable?

The !G_IS_UNIX_CONNECTION branch doesn't check for NUL, so this branch shouldn't either. (The D-Bus Specification says that "If the first byte received from the client is not a nul byte, the server may disconnect that client". "May", not "must", so just ignoring it is allowed.)

>   /* first read the NUL-byte (TODO: read credentials if using a unix domain socket) */

That TODO is already done...

(In reply to Daniel Macks from comment #7)
> so it actually *is* used after being set. Previously was probably just
> awaiting someone to implement the handler for its value. Simply removing the
> line_when=line_when line solves it.

Yes, sounds right
Comment 9 Daniel Macks 2015-03-19 05:35:44 UTC
(In reply to Dan Winship from comment #8)
> (In reply to Daniel Macks from comment #6)
> > so the code is just reading it into byte and then ignoring it. Should it
> > check that it's actually NUL? Or should it just read it in a void context
> > rather than assigning it to a dummy variable?
> 
> The !G_IS_UNIX_CONNECTION branch doesn't check for NUL, so this branch
> shouldn't either. (The D-Bus Specification says that "If the first byte
> received from the client is not a nul byte, the server may disconnect that
> client". "May", not "must", so just ignoring it is allowed.)

So it definitely needs to be ignored. Is just calling (void)g_data_input_stream_read_byte(...) okay here?
Comment 10 Dan Winship 2015-03-19 17:25:48 UTC
sure
Comment 11 Daniel Macks 2015-03-20 03:16:49 UTC
Created attachment 299905 [details] [review]
Fix some -Wself-assign warnings

Includes all specific cases and solutions discussed so far. There are still others yet un-analyzed.
Comment 12 Daniel Macks 2015-04-23 05:29:57 UTC
Created attachment 302197 [details] [review]
Fix more -Wself-assign warnings

Resolves the two other instances in gdbusauthmechanismsha1.c (followup from Comment #7).
Comment 13 Philip Withnall 2017-09-11 21:16:24 UTC
Review of attachment 299905 [details] [review]:

Looks good to me.
Comment 14 Philip Withnall 2017-09-11 21:16:29 UTC
Review of attachment 302197 [details] [review]:

Looks good.
Comment 15 Philip Withnall 2017-09-11 21:24:35 UTC
Rebased (trivial conflicts to resolve) and pushed.