From: Zbigniew Jędrzejewski-Szmek Date: Sat, 9 May 2020 07:09:11 +0000 (+0200) Subject: shared/ethtool-util: hush gcc warnings about array bounds X-Git-Tag: v246-rc1~390^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F15762%2Fhead;p=thirdparty%2Fsystemd.git shared/ethtool-util: hush gcc warnings about array bounds [127/1355] Compiling C object 'src/shared/5afaae1@@systemd-shared-245@sta/ethtool-util.c.o' ../src/shared/ethtool-util.c: In function ‘ethtool_get_permanent_macaddr’: ../src/shared/ethtool-util.c:260:60: warning: array subscript 5 is outside the bounds of an interior zero-length array ‘__u8[0]’ {aka ‘unsigned char[]’} [-Wzero-length-bounds] 260 | ret->ether_addr_octet[i] = epaddr.addr.data[i]; | ~~~~~~~~~~~~~~~~^~~ In file included from ../src/shared/ethtool-util.c:5: ../src/shared/linux/ethtool.h:704:7: note: while referencing ‘data’ 704 | __u8 data[0]; | ^~~~ ../src/shared/ethtool-util.c: In function ‘ethtool_set_features’: ../src/shared/ethtool-util.c:488:31: warning: array subscript 0 is outside the bounds of an interior zero-length array ‘__u32[0]’ {aka ‘unsigned int[]’} [-Wzero-length-bounds] 488 | len = buffer.info.data[0]; | ~~~~~~~~~~~~~~~~^~~ In file included from ../src/shared/ethtool-util.c:5: ../src/shared/linux/ethtool.h:631:8: note: while referencing ‘data’ 631 | __u32 data[0]; | ^~~~ The kernel should not define the length of the array, but it does. We can't fix that, so let's use a cast to avoid the warning. For https://github.com/systemd/systemd/issues/6119#issuecomment-626073743. v2: - use #pragma instead of a cast. It seems the cast only works in some cases, and gcc is "smart" enough to see beyond the cast. Unfortunately clang does not support this warning, so we need to do a config check whether to try to suppress. --- diff --git a/meson.build b/meson.build index 4c997ab6f7e..580d5126fae 100644 --- a/meson.build +++ b/meson.build @@ -411,6 +411,9 @@ add_project_arguments(cc.get_supported_arguments(basic_disabled_warnings), langu add_project_arguments(cc.get_supported_arguments(possible_cc_flags), language : 'c') add_project_link_arguments(cc.get_supported_link_arguments(possible_link_flags), language : 'c') +have = cc.has_argument('-Wzero-length-bounds') +conf.set10('HAVE_ZERO_LENGTH_BOUNDS', have) + if cc.compiles(''' #include #include diff --git a/src/shared/ethtool-util.c b/src/shared/ethtool-util.c index 0cde87f5ac3..25bc79eeea1 100644 --- a/src/shared/ethtool-util.c +++ b/src/shared/ethtool-util.c @@ -256,8 +256,13 @@ int ethtool_get_permanent_macaddr(int *ethtool_fd, const char *ifname, struct et if (epaddr.addr.size != 6) return -EOPNOTSUPP; +#pragma GCC diagnostic push +#if HAVE_ZERO_LENGTH_BOUNDS +# pragma GCC diagnostic ignored "-Wzero-length-bounds" +#endif for (size_t i = 0; i < epaddr.addr.size; i++) ret->ether_addr_octet[i] = epaddr.addr.data[i]; +#pragma GCC diagnostic pop return 0; } @@ -485,7 +490,12 @@ static int get_stringset(int ethtool_fd, struct ifreq *ifr, int stringset_id, st if (!buffer.info.sset_mask) return -EINVAL; +#pragma GCC diagnostic push +#if HAVE_ZERO_LENGTH_BOUNDS +# pragma GCC diagnostic ignored "-Wzero-length-bounds" +#endif len = buffer.info.data[0]; +#pragma GCC diagnostic pop strings = malloc0(sizeof(struct ethtool_gstrings) + len * ETH_GSTRING_LEN); if (!strings)