From eaa3a4e396a36b29897ef7862fe0d3f79aebd728 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 23 Jan 2024 10:17:21 +0100 Subject: [PATCH 1/6] libnm: avoid "-Walloc-size" warning in nm_team_link_watcher_new_ethtool() gcc-14.0.1-0.2.fc40 warns: CC src/libnm-core-impl/libnm_core_impl_la-nm-setting-team.lo ../src/libnm-core-impl/nm-setting-team.c: In function nm_team_link_watcher_new_ethtool: ../src/libnm-core-impl/nm-setting-team.c:127:13: error: allocation of insufficient size 16 for type NMTeamLinkWatcher with size 48 [-Werror=alloc-size] 127 | watcher = g_malloc(nm_offsetofend(NMTeamLinkWatcher, ethtool)); | ^ (cherry picked from commit 5715feebe728b25cb36a1a40808c0386794db9f5) --- src/libnm-core-impl/nm-setting-team.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libnm-core-impl/nm-setting-team.c b/src/libnm-core-impl/nm-setting-team.c index 2195757fcf..191ed9aef6 100644 --- a/src/libnm-core-impl/nm-setting-team.c +++ b/src/libnm-core-impl/nm-setting-team.c @@ -123,6 +123,7 @@ nm_team_link_watcher_new_ethtool(int delay_up, int delay_down, GError **error) } NM_PRAGMA_WARNING_DISABLE("-Warray-bounds") + NM_PRAGMA_WARNING_DISABLE("-Walloc-size") watcher = g_malloc(nm_offsetofend(NMTeamLinkWatcher, ethtool)); @@ -131,6 +132,7 @@ nm_team_link_watcher_new_ethtool(int delay_up, int delay_down, GError **error) watcher->ethtool.delay_up = delay_up; watcher->ethtool.delay_down = delay_down; + NM_PRAGMA_WARNING_REENABLE NM_PRAGMA_WARNING_REENABLE return watcher; From 157c2ffeee60bcd0e541c0b3870013302c74d079 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 23 Jan 2024 10:27:13 +0100 Subject: [PATCH 2/6] libnm: use flexible array member in NMUtilsStrStrDictKey Otherwise, gcc-14.0.1-0.2.fc40 warns: ../src/libnm-core-impl/nm-utils.c: In function _nm_utils_strstrdictkey_create: ../src/libnm-core-impl/nm-utils.c:5076:16: error: allocation of insufficient size '1' for type 'NMUtilsStrStrDictKey' {aka 'struct _NMUtilsStrStrDictKey'} with size '2' [-Werror=alloc-size] 5076 | return g_malloc0(1); | ^~~~~~~~~~~~ (cherry picked from commit 63ab0d926d7d328454fe10019e3261e1303121d7) --- src/libnm-core-impl/nm-utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libnm-core-impl/nm-utils.c b/src/libnm-core-impl/nm-utils.c index 88df329148..761f74bdf1 100644 --- a/src/libnm-core-impl/nm-utils.c +++ b/src/libnm-core-impl/nm-utils.c @@ -5008,7 +5008,7 @@ nm_utils_bond_mode_string_to_int(const char *mode) struct _NMUtilsStrStrDictKey { char type; - char data[1]; + char data[]; }; guint From 9fd5a78eb9ec8a6d6b3e842e5748b6f2446fc698 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 23 Jan 2024 11:02:34 +0100 Subject: [PATCH 3/6] std-aux: add NM_STRINGIFY() macro (cherry picked from commit 025d6898fed452d9de07d09086f836dfb144319a) --- src/libnm-std-aux/nm-std-aux.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libnm-std-aux/nm-std-aux.h b/src/libnm-std-aux/nm-std-aux.h index 3b39a01940..1e29418300 100644 --- a/src/libnm-std-aux/nm-std-aux.h +++ b/src/libnm-std-aux/nm-std-aux.h @@ -83,6 +83,11 @@ /*****************************************************************************/ +#define NM_STRINGIFY_ARG(contents) #contents +#define NM_STRINGIFY(macro_or_string) NM_STRINGIFY_ARG(macro_or_string) + +/*****************************************************************************/ + #ifndef _NM_CC_SUPPORT_AUTO_TYPE #if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) #define _NM_CC_SUPPORT_AUTO_TYPE 1 From a5f75625e27838cc5ee4fb5c25f3a3145a7e5ead Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 23 Jan 2024 10:52:29 +0100 Subject: [PATCH 4/6] std-aux: move NM_PRAGMA_WARNING_DISABLE() macro from libnm-glib-aux to libnm-std-aux Will be used by nm_assert(). (cherry picked from commit c0338526f37dcee327f7e919acee0c48158bd34c) --- src/libnm-glib-aux/nm-macros-internal.h | 42 ------------------------- src/libnm-std-aux/nm-std-aux.h | 42 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/libnm-glib-aux/nm-macros-internal.h b/src/libnm-glib-aux/nm-macros-internal.h index a9253ed4fa..1c8c85e668 100644 --- a/src/libnm-glib-aux/nm-macros-internal.h +++ b/src/libnm-glib-aux/nm-macros-internal.h @@ -195,48 +195,6 @@ _nm_auto_freev(gpointer ptr) /*****************************************************************************/ -#if defined(__GNUC__) -#define _NM_PRAGMA_WARNING_DO(warning) G_STRINGIFY(GCC diagnostic ignored warning) -#elif defined(__clang__) -#define _NM_PRAGMA_WARNING_DO(warning) G_STRINGIFY(clang diagnostic ignored warning) -#endif - -/* you can only suppress a specific warning that the compiler - * understands. Otherwise you will get another compiler warning - * about invalid pragma option. - * It's not that bad however, because gcc and clang often have the - * same name for the same warning. */ - -#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) -#define NM_PRAGMA_DIAGNOSTICS_PUSH _Pragma("GCC diagnostic push") -#define NM_PRAGMA_WARNING_DISABLE(warning) \ - NM_PRAGMA_DIAGNOSTICS_PUSH _Pragma(_NM_PRAGMA_WARNING_DO(warning)) -#define NM_PRAGMA_WARNING_REENABLE _Pragma("GCC diagnostic pop") -#elif defined(__clang__) -#define NM_PRAGMA_DIAGNOSTICS_PUSH _Pragma("clang diagnostic push") -#define NM_PRAGMA_WARNING_DISABLE(warning) \ - NM_PRAGMA_DIAGNOSTICS_PUSH _Pragma(_NM_PRAGMA_WARNING_DO("-Wunknown-warning-option")) \ - _Pragma(_NM_PRAGMA_WARNING_DO(warning)) -#define NM_PRAGMA_WARNING_REENABLE _Pragma("clang diagnostic pop") -#else -#define NM_PRAGMA_DIAGNOSTICS_PUSH -#define NM_PRAGMA_WARNING_DISABLE(warning) -#define NM_PRAGMA_WARNING_REENABLE -#endif - -/*****************************************************************************/ - -/* Seems gcc-12 has a tendency for false-positive -Wdangling-pointer warnings with - * g_error()'s `for(;;);`. See https://bugzilla.redhat.com/show_bug.cgi?id=2056613 . - * Work around, but only for the affected gcc 12.0.1. */ -#if defined(__GNUC__) && __GNUC__ == 12 && __GNUC_MINOR__ == 0 && __GNUC_PATCHLEVEL__ <= 1 -#define NM_PRAGMA_WARNING_DISABLE_DANGLING_POINTER NM_PRAGMA_WARNING_DISABLE("-Wdangling-pointer") -#else -#define NM_PRAGMA_WARNING_DISABLE_DANGLING_POINTER NM_PRAGMA_DIAGNOSTICS_PUSH -#endif - -/*****************************************************************************/ - /** * NM_G_ERROR_MSG: * @error: (nullable): the #GError instance diff --git a/src/libnm-std-aux/nm-std-aux.h b/src/libnm-std-aux/nm-std-aux.h index 1e29418300..a1e89680b6 100644 --- a/src/libnm-std-aux/nm-std-aux.h +++ b/src/libnm-std-aux/nm-std-aux.h @@ -214,6 +214,48 @@ typedef uint64_t _nm_bitwise nm_be64_t; /*****************************************************************************/ +#if defined(__GNUC__) +#define _NM_PRAGMA_WARNING_DO(warning) NM_STRINGIFY(GCC diagnostic ignored warning) +#elif defined(__clang__) +#define _NM_PRAGMA_WARNING_DO(warning) NM_STRINGIFY(clang diagnostic ignored warning) +#endif + +/* you can only suppress a specific warning that the compiler + * understands. Otherwise you will get another compiler warning + * about invalid pragma option. + * It's not that bad however, because gcc and clang often have the + * same name for the same warning. */ + +#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) +#define NM_PRAGMA_DIAGNOSTICS_PUSH _Pragma("GCC diagnostic push") +#define NM_PRAGMA_WARNING_DISABLE(warning) \ + NM_PRAGMA_DIAGNOSTICS_PUSH _Pragma(_NM_PRAGMA_WARNING_DO(warning)) +#define NM_PRAGMA_WARNING_REENABLE _Pragma("GCC diagnostic pop") +#elif defined(__clang__) +#define NM_PRAGMA_DIAGNOSTICS_PUSH _Pragma("clang diagnostic push") +#define NM_PRAGMA_WARNING_DISABLE(warning) \ + NM_PRAGMA_DIAGNOSTICS_PUSH _Pragma(_NM_PRAGMA_WARNING_DO("-Wunknown-warning-option")) \ + _Pragma(_NM_PRAGMA_WARNING_DO(warning)) +#define NM_PRAGMA_WARNING_REENABLE _Pragma("clang diagnostic pop") +#else +#define NM_PRAGMA_DIAGNOSTICS_PUSH +#define NM_PRAGMA_WARNING_DISABLE(warning) +#define NM_PRAGMA_WARNING_REENABLE +#endif + +/*****************************************************************************/ + +/* Seems gcc-12 has a tendency for false-positive -Wdangling-pointer warnings with + * g_error()'s `for(;;);`. See https://bugzilla.redhat.com/show_bug.cgi?id=2056613 . + * Work around, but only for the affected gcc 12.0.1. */ +#if defined(__GNUC__) && __GNUC__ == 12 && __GNUC_MINOR__ == 0 && __GNUC_PATCHLEVEL__ <= 1 +#define NM_PRAGMA_WARNING_DISABLE_DANGLING_POINTER NM_PRAGMA_WARNING_DISABLE("-Wdangling-pointer") +#else +#define NM_PRAGMA_WARNING_DISABLE_DANGLING_POINTER NM_PRAGMA_DIAGNOSTICS_PUSH +#endif + +/*****************************************************************************/ + /* glib/C provides the following kind of assertions: * - assert() -- disable with NDEBUG * - g_return_if_fail() -- disable with G_DISABLE_CHECKS From 3fb0f9f8a764d850d580687cb32b87bf5237f2d7 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 23 Jan 2024 10:56:54 +0100 Subject: [PATCH 5/6] std-aux: suppress "-Wnonnull-compare" warning in nm_assert() When we use a "static" array declarator to a function, we understand and tell the compiler that the argument must not be NULL. But now gcc-14.0.1-0.2.fc40 starts warning about NULL checks for such arguments. static void foo(char args[static 10]) { nm_assert(args); sprintf(args, "hi"); } Granted, the compiler is right, and we know that this condition is not supposed to be violated. A logical thing would be just to drop the assertion. Instead, suppress "-Wnonnull-compare" warnings inside a nm_assert(). An nm_assert() is more than a run time check, it's an additional self-documenting code of the invariants. It's fine to assert for something that is true. Actually, all the conditions that we assert against, hold. The compiler telling us that the condition that we assert against is valid, is not useful. (cherry picked from commit 62c1745f627bb109e45c64ae510f5029df051da4) --- src/libnm-std-aux/nm-std-aux.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libnm-std-aux/nm-std-aux.h b/src/libnm-std-aux/nm-std-aux.h index a1e89680b6..2575be4b69 100644 --- a/src/libnm-std-aux/nm-std-aux.h +++ b/src/libnm-std-aux/nm-std-aux.h @@ -288,6 +288,8 @@ typedef uint64_t _nm_bitwise nm_be64_t; #define nm_assert(cond) \ ({ \ + NM_PRAGMA_WARNING_DISABLE("-Wnonnull-compare"); \ + \ /* nm_assert() must do *nothing* of effect, except evaluating * @cond (0 or 1 times). * @@ -305,6 +307,9 @@ typedef uint64_t _nm_bitwise nm_be64_t; } else { \ _nm_assert_fail(#cond); \ } \ + \ + NM_PRAGMA_WARNING_REENABLE; \ + \ 1; \ }) From aeaba8a2a123cce0f02ddc028eed23eda7082de9 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 23 Jan 2024 10:13:25 +0100 Subject: [PATCH 6/6] libnm: avoid "-Wnonnull-compare warnings with "static" array declarator With a static array, we indicate that the argument must not be NULL. Gcc-14.0.1-0.2.fc40 now warns against that: CC src/libnm-base/libnm_base_la-nm-base.lo In file included from ../src/libnm-std-aux/nm-default-std.h:102, from ../src/libnm-glib-aux/nm-default-glib.h:11, from ../src/libnm-glib-aux/nm-default-glib-i18n-lib.h:13, from ../src/libnm-base/nm-base.c:3: ../src/libnm-base/nm-base.c: In function 'nm_net_devname_infiniband': ../src/libnm-std-aux/nm-std-aux.h:191:12: error: 'nonnull' argument 'name' compared to NULL [-Werror=nonnull-compare] 191 | if (expr) \ | ^ ../src/libnm-std-aux/nm-std-aux.h:202:27: note: in expansion of macro '_NM_BOOLEAN_EXPR_IMPL' 202 | _NM_BOOLEAN_EXPR_IMPL(NM_UNIQ, expr)) | ^~~~~~~~~~~~~~~~~~~~~ ../src/libnm-glib-aux/nm-macros-internal.h:1693:31: note: in expansion of macro 'NM_BOOLEAN_EXPR' 1693 | #define _G_BOOLEAN_EXPR(expr) NM_BOOLEAN_EXPR(expr) | ^~~~~~~~~~~~~~~ /usr/include/glib-2.0/glib/gmacros.h:1244:43: note: in expansion of macro '_G_BOOLEAN_EXPR' 1244 | #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1)) | ^~~~~~~~~~~~~~~ /usr/include/glib-2.0/glib/gmessages.h:656:9: note: in expansion of macro 'G_LIKELY' 656 | if (G_LIKELY (expr)) \ | ^~~~~~~~ ../src/libnm-base/nm-base.c:57:5: note: in expansion of macro 'g_return_val_if_fail' 57 | g_return_val_if_fail(name, NULL); | ^~~~~~~~~~~~~~~~~~~~ ../src/libnm-core-impl/nm-setting-wireguard.c: In function '_nm_wireguard_peer_set_public_key_bin': ../src/libnm-core-impl/nm-setting-wireguard.c:316:8: error: 'nonnull' argument 'public_key' compared to NULL [-Werror=nonnull-compare] 316 | if (!public_key) | ^ Convert these checks to an nm_assert() to suppress the warning. (cherry picked from commit 7a031eef5d91adb2c34dd63ce4f503be730ad8c7) --- src/libnm-base/nm-base.c | 3 ++- src/libnm-core-impl/nm-setting-wireguard.c | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libnm-base/nm-base.c b/src/libnm-base/nm-base.c index 6e164908af..54d89e1f31 100644 --- a/src/libnm-base/nm-base.c +++ b/src/libnm-base/nm-base.c @@ -54,7 +54,8 @@ nm_dhcp_iaid_from_hexstr(const char *str, guint32 *out_value) const char * nm_net_devname_infiniband(char name[static NM_IFNAMSIZ], const char *parent_name, int p_key) { - g_return_val_if_fail(name, NULL); + nm_assert(name); + g_return_val_if_fail(parent_name && parent_name[0], NULL); g_return_val_if_fail(strlen(parent_name) < NM_IFNAMSIZ, NULL); diff --git a/src/libnm-core-impl/nm-setting-wireguard.c b/src/libnm-core-impl/nm-setting-wireguard.c index 1e7ed05b1d..c313d22c28 100644 --- a/src/libnm-core-impl/nm-setting-wireguard.c +++ b/src/libnm-core-impl/nm-setting-wireguard.c @@ -311,10 +311,9 @@ _nm_wireguard_peer_set_public_key_bin(NMWireGuardPeer *self, { g_return_if_fail(NM_IS_WIREGUARD_PEER(self, FALSE)); - nm_clear_g_free(&self->public_key); + nm_assert(public_key); - if (!public_key) - return; + nm_clear_g_free(&self->public_key); self->public_key = g_base64_encode(public_key, NM_WIREGUARD_PUBLIC_KEY_LEN); self->public_key_valid = TRUE;