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 741168 - [review] unrelated patches to avoid compiler warning with -Wstrict-overflow
[review] unrelated patches to avoid compiler warning with -Wstrict-overflow
Status: RESOLVED FIXED
Product: NetworkManager
Classification: Platform
Component: general
git master
Other Linux
: Normal normal
: ---
Assigned To: NetworkManager maintainer(s)
NetworkManager maintainer(s)
Depends on:
Blocks:
 
 
Reported: 2014-12-05 16:04 UTC by Thomas Haller
Modified: 2015-01-12 15:51 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
libnm: return 0 for empty address in hwaddr_binary_len() (2.12 KB, patch)
2014-12-05 16:05 UTC, Thomas Haller
committed Details | Review
dispatcher: refactor constructing environment variables from strv (6.11 KB, patch)
2014-12-05 16:05 UTC, Thomas Haller
committed Details | Review
wifi: avoid strict-overflow warning in nm_ap_utils_complete_connection() (1.35 KB, patch)
2014-12-05 16:05 UTC, Thomas Haller
rejected Details | Review

Description Thomas Haller 2014-12-05 16:04:52 UTC
I wanted to compile with gcc-4.8.3, "-O2 -Wstrict-overflow".

It almost works. Attached 3 patches...


Arguably, "wifi: avoid strict-overflow warning in nm_ap_utils_complete_c..."
is ugly. But I don't see another way.
Comment 1 Thomas Haller 2014-12-05 16:05:24 UTC
Created attachment 292197 [details] [review]
libnm: return 0 for empty address in hwaddr_binary_len()

Motivated by avoiding compiler warning with -O2 -Wstrict-overflow (gcc-4.8.3):

    make[4]: Entering directory `./NetworkManager/libnm-core'
      CC       nm-utils.lo
    ../libnm-core/nm-utils.c: In function 'nm_utils_hwaddr_valid':
    ../libnm-core/nm-utils.c:2725:14: error: assuming signed overflow does not occur when simplifying conditional to constant [-Werror=strict-overflow]
       if (length == 0 || length > NM_UTILS_HWADDR_LEN_MAX)
                  ^
    ../libnm-core/nm-utils.c: In function 'nm_utils_hwaddr_canonical':
    ../libnm-core/nm-utils.c:2755:14: error: assuming signed overflow does not occur when simplifying conditional to constant [-Werror=strict-overflow]
       if (length == 0 || length > NM_UTILS_HWADDR_LEN_MAX)
                  ^
Comment 2 Thomas Haller 2014-12-05 16:05:30 UTC
Created attachment 292198 [details] [review]
dispatcher: refactor constructing environment variables from strv

This also avoids a warning with -O2 -Wstrict-overflow (gcc-4.8.3):

    make[4]: Entering directory `./NetworkManager/callouts'
      CC       libtest_dispatcher_envp_la-nm-dispatcher-utils.lo
    nm-dispatcher-utils.c: In function 'construct_ip6_items':
    nm-dispatcher-utils.c:283:8: error: assuming signed overflow does not occur when simplifying conditional to constant [-Werror=strict-overflow]
         if (i != 0)
            ^
    nm-dispatcher-utils.c: In function 'construct_ip4_items':
    nm-dispatcher-utils.c:144:8: error: assuming signed overflow does not occur when simplifying conditional to constant [-Werror=strict-overflow]
         if (i != 0)
            ^
    nm-dispatcher-utils.c:168:8: error: assuming signed overflow does not occur when simplifying conditional to constant [-Werror=strict-overflow]
         if (i != 0)
            ^
Comment 3 Thomas Haller 2014-12-05 16:05:36 UTC
Created attachment 292199 [details] [review]
wifi: avoid strict-overflow warning in nm_ap_utils_complete_connection()

avoid a warning with -O2 -Wstrict-overflow (gcc-4.8.3):

    make[6]: Entering directory `./NetworkManager/src/devices/wifi/tests'
      CC       nm-wifi-ap-utils.o
      CCLD     test-wifi-ap-utils
    test-wifi-ap-utils.c: In function 'complete_connection.2533.constprop.19':
    ./../nm-wifi-ap-utils.c:727:5: error: assuming signed overflow does not occur when simplifying conditional to constant [-Werror=strict-overflow]
      if (adhoc) {
         ^
Comment 4 Thomas Haller 2014-12-05 16:06:21 UTC
Patches apply on master, 5bfb4c8c23e5b9d8020d6caff8c63830d2d8ec37
Comment 5 Dan Winship 2014-12-05 16:55:39 UTC
Comment on attachment 292199 [details] [review]
wifi: avoid strict-overflow warning in nm_ap_utils_complete_connection()

>-	gboolean adhoc = FALSE;
>+	guint adhoc = FALSE;

No. That is so wrong.

-Wstrict-overflow is pretty obnoxious, in that it doesn't tell you where the compiler thinks the bug is, it just tells you what later lines would be affected if there had been a bug earlier.

And changing ints to uints is a silly fix anyway, because it doesn't actually fix the problem that gcc thinks it sees. It just turns the signed overflow into an unsigned overflow, which gcc doesn't warn you about, because it has well-defined behavior according to the spec (despite the fact that overflowing is almost certainly wrong if it could actually happen in the code).

In this case, we have absolutely no clue what gcc is thinking, so silencing the warning in a way that doesn't address the underlying issue is wrong, because there may be an actual bug here.
Comment 6 Thomas Haller 2014-12-05 17:38:47 UTC
(In reply to comment #5)
> (From update of attachment 292199 [details] [review])
> >-	gboolean adhoc = FALSE;
> >+	guint adhoc = FALSE;
> 
> No. That is so wrong.
> 
> -Wstrict-overflow is pretty obnoxious, in that it doesn't tell you where the
> compiler thinks the bug is, it just tells you what later lines would be
> affected if there had been a bug earlier.
> 
> And changing ints to uints is a silly fix anyway, because it doesn't actually
> fix the problem that gcc thinks it sees. It just turns the signed overflow into
> an unsigned overflow, which gcc doesn't warn you about, because it has
> well-defined behavior according to the spec (despite the fact that overflowing
> is almost certainly wrong if it could actually happen in the code).
> 
> In this case, we have absolutely no clue what gcc is thinking, so silencing the
> warning in a way that doesn't address the underlying issue is wrong, because
> there may be an actual bug here.

Looks like the warning is incorrect, so there is no underlying issue to fix.
(Unless you can see it).

Either the code gets somehow restructured so that the warning goes away, or the code cannot compile with these flags.

I can not compile without -Wstrict-overflow, ... but that is silencing the warning too (and not only for this code place).

So you suggest that -Wstrict-overflow makes no sense in general and we just don't enable it?
Comment 7 Dan Winship 2014-12-06 15:47:27 UTC
(In reply to comment #6)
> Looks like the warning is incorrect, so there is no underlying issue to fix.
> (Unless you can see it).

I can't see any bug, but who knows what gcc's optimizer is doing to that code before getting to the point where it sees a potential overflow...

> So you suggest that -Wstrict-overflow makes no sense in general and we just
> don't enable it?

Yeah. The gcc docs even warn that "this warning can easily give a false positive". And given that as far as we can tell, it didn't actually find any real bugs in this run, it seems like it's more of a nuisance than a feature.

(You could add "-Wstrict-overflow -Wno-error=strict-overflow" to CFLAGS when doing your own builds if you want to see the warnings but not break the build...)
Comment 8 Dan Williams 2014-12-08 16:04:20 UTC
Review of attachment 292197 [details] [review]:

LGTM
Comment 9 Dan Williams 2014-12-08 16:09:41 UTC
Review of attachment 292198 [details] [review]:

Rest looks good to me.

::: callouts/nm-dispatcher-utils.c
@@ +54,3 @@
 }
 
+static GSList *_list_append_val_strv (GSList *items, char **values, const char *format, ...) __attribute__((__format__ (__printf__, 3, 4)));

G_GNUC_PRINTF(3,4) instead?  it's shorter and evals to the same thing.
Comment 10 Dan Williams 2014-12-08 16:15:16 UTC
(In reply to comment #3)
> Created an attachment (id=292199) [details] [review]
> wifi: avoid strict-overflow warning in nm_ap_utils_complete_connection()
> 
> avoid a warning with -O2 -Wstrict-overflow (gcc-4.8.3):
> 
>     make[6]: Entering directory `./NetworkManager/src/devices/wifi/tests'
>       CC       nm-wifi-ap-utils.o
>       CCLD     test-wifi-ap-utils
>     test-wifi-ap-utils.c: In function 'complete_connection.2533.constprop.19':
>     ./../nm-wifi-ap-utils.c:727:5: error: assuming signed overflow does not
> occur when simplifying conditional to constant [-Werror=strict-overflow]
>       if (adhoc) {
>          ^

Does it build correctly in wifi, but *not* in test?  Since src/devices/wifi/Makefile.am has "SUBDIRS= . test" this seems to imply that it does, unless you have parallel make running?
Comment 11 Thomas Haller 2014-12-08 18:04:54 UTC
Comment on attachment 292197 [details] [review]
libnm: return 0 for empty address in hwaddr_binary_len()

Patch attachment 292197 [details] [review] commited.
Comment 12 Thomas Haller 2014-12-08 18:17:39 UTC
Comment on attachment 292198 [details] [review]
dispatcher: refactor constructing environment variables from strv

Merged as http://cgit.freedesktop.org/NetworkManager/NetworkManager/commit/?id=37361a038f0dbc503943552d9bf4297451bea417

after changing G_GNUC_PRINTF
Comment 13 Thomas Haller 2014-12-08 18:34:57 UTC
(In reply to comment #10)
> (In reply to comment #3)
> > Created an attachment (id=292199) [details] [review] [details] [review]
> > wifi: avoid strict-overflow warning in nm_ap_utils_complete_connection()
> > 
> > avoid a warning with -O2 -Wstrict-overflow (gcc-4.8.3):
> > 
> >     make[6]: Entering directory `./NetworkManager/src/devices/wifi/tests'
> >       CC       nm-wifi-ap-utils.o
> >       CCLD     test-wifi-ap-utils
> >     test-wifi-ap-utils.c: In function 'complete_connection.2533.constprop.19':
> >     ./../nm-wifi-ap-utils.c:727:5: error: assuming signed overflow does not
> > occur when simplifying conditional to constant [-Werror=strict-overflow]
> >       if (adhoc) {
> >          ^
> 
> Does it build correctly in wifi, but *not* in test?  Since
> src/devices/wifi/Makefile.am has "SUBDIRS= . test" this seems to imply that it
> does, unless you have parallel make running?

I think it happens due to inlining of nm_ap_utils_complete_connection() into the test routine.