From efa1d0e173f6a383f0481ea6dd85629f68a51285 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Wed, 3 Apr 2024 16:51:54 +0200 Subject: [PATCH] libnm-core: avoid compiler warnings in team settings GCC 14 with LTO complains with: In function 'nm_team_link_watcher_new_ethtool', inlined from 'nm_team_link_watcher_new_ethtool' at src/libnm-core-impl/nm-setting-team.c:106:1: src/libnm-core-impl/nm-setting-team.c:130:33: error: array subscript 'struct NMTeamLinkWatcher[0]' is partly outside array bounds of 'unsigned char[16]' [-Werror=array-bounds=] 130 | watcher->ref_count = 1; | ^ src/libnm-core-impl/nm-setting-team.c:128:15: note: object of size 16 allocated by 'g_malloc' 128 | watcher = g_malloc(nm_offsetofend(NMTeamLinkWatcher, ethtool)); | ^ even if the warning is disabled via pragma directives in that code. This looks like the following GCC bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80922 saying We do not track warning options (and thus optimize pragmas / attributes) across LTO because they are not saved in the function specific optimization flag section. We use a (NMTeamLinkWatcher *) to point to a memory area that is shorter than the struct, because depending on the watcher type we need to store different parameters; in this way we can save few bytes of memory for some watcher types. However, this often breaks when upgrading the compiler; instead just allocate the full struct. (cherry picked from commit d369f5519244b4454b30376d36ed22b6777d7ee5) --- src/libnm-core-impl/nm-setting-team.c | 8 +------- src/libnm-core-impl/nm-team-utils.c | 10 +--------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/src/libnm-core-impl/nm-setting-team.c b/src/libnm-core-impl/nm-setting-team.c index 191ed9aef6..08364af88f 100644 --- a/src/libnm-core-impl/nm-setting-team.c +++ b/src/libnm-core-impl/nm-setting-team.c @@ -122,19 +122,13 @@ nm_team_link_watcher_new_ethtool(int delay_up, int delay_down, GError **error) return NULL; } - NM_PRAGMA_WARNING_DISABLE("-Warray-bounds") - NM_PRAGMA_WARNING_DISABLE("-Walloc-size") - - watcher = g_malloc(nm_offsetofend(NMTeamLinkWatcher, ethtool)); + watcher = g_malloc(sizeof(NMTeamLinkWatcher)); watcher->ref_count = 1; watcher->type = LINK_WATCHER_ETHTOOL; watcher->ethtool.delay_up = delay_up; watcher->ethtool.delay_down = delay_down; - NM_PRAGMA_WARNING_REENABLE - NM_PRAGMA_WARNING_REENABLE - return watcher; } diff --git a/src/libnm-core-impl/nm-team-utils.c b/src/libnm-core-impl/nm-team-utils.c index 6f2f5dd298..21562163d2 100644 --- a/src/libnm-core-impl/nm-team-utils.c +++ b/src/libnm-core-impl/nm-team-utils.c @@ -2809,16 +2809,8 @@ NMTeamSetting * nm_team_setting_new(gboolean is_port, const char *js_str) { NMTeamSetting *self; - gsize l; - G_STATIC_ASSERT_EXPR(sizeof(*self) == sizeof(self->_data_priv)); - G_STATIC_ASSERT_EXPR( - sizeof(*self) - == NM_MAX(nm_offsetofend(NMTeamSetting, d.master), nm_offsetofend(NMTeamSetting, d.port))); - - l = is_port ? nm_offsetofend(NMTeamSetting, d.port) : nm_offsetofend(NMTeamSetting, d.master); - - self = g_malloc0(l); + self = g_malloc0(sizeof(NMTeamSetting)); self->_data_priv.is_port = is_port; self->_data_priv.strict_validated = TRUE;