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 d369f55192)
This commit is contained in:
Beniamino Galvani 2024-04-03 16:51:54 +02:00 committed by Fernando Fernandez Mancera
parent 78d5ba4d4f
commit efa1d0e173
2 changed files with 2 additions and 16 deletions

View file

@ -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;
}

View file

@ -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;