From 42ba464380808b392ac9245389b417357c91b253 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 16 Jun 2021 12:38:52 +0200 Subject: [PATCH 1/5] glib-aux: cleanup nm_utils_named_value_clear_with_g_free() implementation --- src/libnm-glib-aux/nm-shared-utils.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/libnm-glib-aux/nm-shared-utils.c b/src/libnm-glib-aux/nm-shared-utils.c index ff1b705006..936d900618 100644 --- a/src/libnm-glib-aux/nm-shared-utils.c +++ b/src/libnm-glib-aux/nm-shared-utils.c @@ -3244,11 +3244,8 @@ void nm_utils_named_value_clear_with_g_free(NMUtilsNamedValue *val) { if (val) { - gs_free gpointer x_name = NULL; - gs_free gpointer x_value = NULL; - - x_name = (gpointer) g_steal_pointer(&val->name); - x_value = g_steal_pointer(&val->value_ptr); + nm_clear_g_free(&val->name_mutable); + nm_clear_g_free(&val->value_ptr); } } From a6c9f2518ec16b6e5343d36d57b32a048f65575b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 16 Jun 2021 09:12:02 +0200 Subject: [PATCH 2/5] config: add nm_config_data_get_device_config_int64() helper --- src/core/nm-config-data.c | 21 +++++++++++++++++++++ src/core/nm-config-data.h | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/src/core/nm-config-data.c b/src/core/nm-config-data.c index f31c900e27..fa1cc49a78 100644 --- a/src/core/nm-config-data.c +++ b/src/core/nm-config-data.c @@ -1508,6 +1508,27 @@ nm_config_data_get_device_config_boolean(const NMConfigData *self, return nm_config_parse_boolean(value, val_invalid); } +gint64 +nm_config_data_get_device_config_int64(const NMConfigData *self, + const char * property, + NMDevice * device, + int base, + gint64 min, + gint64 max, + gint64 val_no_match, + gint64 val_invalid) +{ + gs_free char *value = NULL; + gboolean has_match; + + value = nm_config_data_get_device_config(self, property, device, &has_match); + if (!has_match) { + errno = ENOENT; + return val_no_match; + } + return _nm_utils_ascii_str_to_int64(value, base, min, max, val_invalid); +} + char * nm_config_data_get_connection_default(const NMConfigData *self, const char * property, diff --git a/src/core/nm-config-data.h b/src/core/nm-config-data.h index d1b2c3744a..0d133b8c57 100644 --- a/src/core/nm-config-data.h +++ b/src/core/nm-config-data.h @@ -234,6 +234,14 @@ gboolean nm_config_data_get_device_config_boolean(const NMConfigData *self, NMDevice * device, int val_no_match, int val_invalid); +gint64 nm_config_data_get_device_config_int64(const NMConfigData *self, + const char * property, + NMDevice * device, + int base, + gint64 min, + gint64 max, + gint64 val_no_match, + gint64 val_invalid); char ** nm_config_data_get_groups(const NMConfigData *self); char ** nm_config_data_get_keys(const NMConfigData *self, const char *group); From b929caa95cfc6f2bcf0dfd22a0743abdbcd9838c Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 16 Jun 2021 09:17:41 +0200 Subject: [PATCH 3/5] core: use nm_config_data_get_device_config_int64() for getting integer setting --- src/core/devices/nm-device.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c index 269418df40..f43646845a 100644 --- a/src/core/devices/nm-device.c +++ b/src/core/devices/nm-device.c @@ -5874,16 +5874,19 @@ sriov_op_queue(NMDevice * self, static void device_init_static_sriov_num_vfs(NMDevice *self) { - NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE(self); - gs_free char * value = NULL; - int num_vfs; + NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE(self); if (priv->ifindex > 0 && nm_device_has_capability(self, NM_DEVICE_CAP_SRIOV)) { - value = nm_config_data_get_device_config(NM_CONFIG_GET_DATA, - NM_CONFIG_KEYFILE_KEY_DEVICE_SRIOV_NUM_VFS, - self, - NULL); - num_vfs = _nm_utils_ascii_str_to_int64(value, 10, 0, G_MAXINT32, -1); + int num_vfs; + + num_vfs = nm_config_data_get_device_config_int64(NM_CONFIG_GET_DATA, + NM_CONFIG_KEYFILE_KEY_DEVICE_SRIOV_NUM_VFS, + self, + 10, + 0, + G_MAXINT32, + -1, + -1); if (num_vfs >= 0) sriov_op_queue(self, num_vfs, NM_OPTION_BOOL_DEFAULT, NULL, NULL); } @@ -14043,13 +14046,14 @@ nm_device_is_up(NMDevice *self) static gint64 _get_carrier_wait_ms(NMDevice *self) { - gs_free char *value = NULL; - - value = nm_config_data_get_device_config(NM_CONFIG_GET_DATA, - NM_CONFIG_KEYFILE_KEY_DEVICE_CARRIER_WAIT_TIMEOUT, - self, - NULL); - return _nm_utils_ascii_str_to_int64(value, 10, 0, G_MAXINT32, CARRIER_WAIT_TIME_MS); + return nm_config_data_get_device_config_int64(NM_CONFIG_GET_DATA, + NM_CONFIG_KEYFILE_KEY_DEVICE_CARRIER_WAIT_TIMEOUT, + self, + 10, + 0, + G_MAXINT32, + CARRIER_WAIT_TIME_MS, + CARRIER_WAIT_TIME_MS); } gboolean From 2f9ab1d5280bb21b21d358a6595e96617a57d777 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 16 Jun 2021 09:50:39 +0200 Subject: [PATCH 4/5] config: add lookup index for _match_section_infos_lookup() Previously, we would call g_key_file_get_string(), which requires two hash lookups (one for the group and one for the key). We can do better. Especially since NMConfigData is immutable, it's simple to build a lookup index of the values we have and then do binary search. Note that we call nm_config_data_get_connection_default() and similar API *a lot*, so this is measurable. --- src/core/nm-config-data.c | 111 +++++++++++++++++++++++++++++++++----- 1 file changed, 97 insertions(+), 14 deletions(-) diff --git a/src/core/nm-config-data.c b/src/core/nm-config-data.c index fa1cc49a78..f6667e926d 100644 --- a/src/core/nm-config-data.c +++ b/src/core/nm-config-data.c @@ -26,6 +26,8 @@ typedef struct { gboolean has; GSList * spec; } match_device; + gsize lookup_len; + const NMUtilsNamedValue *lookup_idx; } MatchSectionInfo; struct _NMGlobalDnsDomain { @@ -116,6 +118,11 @@ G_DEFINE_TYPE(NMConfigData, nm_config_data, G_TYPE_OBJECT) /*****************************************************************************/ +static const char * +_match_section_info_get_str(const MatchSectionInfo *m, GKeyFile *keyfile, const char *property); + +/*****************************************************************************/ + const char * nm_config_data_get_config_main_file(const NMConfigData *self) { @@ -1395,13 +1402,13 @@ _match_section_infos_lookup(const MatchSectionInfo *match_section_infos, const char *match_dhcp_plugin; if (!match_section_infos) - return NULL; + goto out; match_dhcp_plugin = nm_dhcp_manager_get_config(nm_dhcp_manager_get()); for (; match_section_infos->group_name; match_section_infos++) { - char * value = NULL; - gboolean match; + const char *value; + gboolean match; /* FIXME: Here we use g_key_file_get_string(). This should be in sync with what keyfile-reader * does. @@ -1410,7 +1417,7 @@ _match_section_infos_lookup(const MatchSectionInfo *match_section_infos, * string_to_value(keyfile_to_string(keyfile)) in one. Optimally, keyfile library would * expose both functions, and we would return here keyfile_to_string(keyfile). * The caller then could convert the string to the proper value via string_to_value(value). */ - value = g_key_file_get_string(keyfile, match_section_infos->group_name, property, NULL); + value = _match_section_info_get_str(match_section_infos, keyfile, property); if (!value && !match_section_infos->stop_match) continue; @@ -1429,11 +1436,13 @@ _match_section_infos_lookup(const MatchSectionInfo *match_section_infos, match = TRUE; if (match) { - *out_value = value; + *out_value = g_strdup(value); return match_section_infos; } - g_free(value); } + +out: + *out_value = NULL; return NULL; } @@ -1580,9 +1589,35 @@ nm_config_data_get_connection_default_int64(const NMConfigData *self, return _nm_utils_ascii_str_to_int64(value, 10, min, max, fallback); } -static void -_get_connection_info_init(MatchSectionInfo *connection_info, GKeyFile *keyfile, char *group) +static const char * +_match_section_info_get_str(const MatchSectionInfo *m, GKeyFile *keyfile, const char *property) { + gssize idx; + const char *value; + + idx = nm_utils_named_value_list_find(m->lookup_idx, m->lookup_len, property, TRUE); + value = idx >= 0 ? m->lookup_idx[idx].value_str : NULL; + +#if NM_MORE_ASSERTS > 10 + { + gs_free char *value2 = g_key_file_get_string(keyfile, m->group_name, property, NULL); + + nm_assert(nm_streq0(value2, value)); + } +#endif + + return value; +} + +static void +_match_section_info_init(MatchSectionInfo *connection_info, GKeyFile *keyfile, char *group) +{ + char ** keys = NULL; + gsize n_keys; + gsize i; + gsize j; + NMUtilsNamedValue *vals; + /* pass ownership of @group on... */ connection_info->group_name = group; @@ -1593,18 +1628,66 @@ _get_connection_info_init(MatchSectionInfo *connection_info, GKeyFile *keyfile, &connection_info->match_device.has); connection_info->stop_match = nm_config_keyfile_get_boolean(keyfile, group, NM_CONFIG_KEYFILE_KEY_STOP_MATCH, FALSE); + + keys = g_key_file_get_keys(keyfile, group, &n_keys, NULL); + nm_utils_strv_sort(keys, n_keys); + + vals = g_new(NMUtilsNamedValue, n_keys); + + for (i = 0, j = 0; i < n_keys; i++) { + gs_free char *key = g_steal_pointer(&keys[i]); + char * value; + + if (NM_IN_STRSET(key, NM_CONFIG_KEYFILE_KEY_STOP_MATCH, NM_CONFIG_KEYFILE_KEY_MATCH_DEVICE)) + continue; + + if (j > 0 && nm_streq(vals[j - 1].name, key)) + continue; + + value = g_key_file_get_string(keyfile, group, key, NULL); + if (!value) + continue; + + vals[j++] = (NMUtilsNamedValue){ + .name = g_steal_pointer(&key), + .value_str = value, + }; + } + + g_free(keys); + + if (n_keys != j) { + gs_free NMUtilsNamedValue *vals2 = vals; + + /* since this buffer will be kept around for a long time, + * get rid of the excess allocation. */ + vals = nm_memdup(vals2, sizeof(NMUtilsNamedValue) * j); + n_keys = j; + } + + if (n_keys == 0) + nm_clear_g_free(&vals); + + connection_info->lookup_idx = vals; + connection_info->lookup_len = n_keys; } static void _match_section_infos_free(MatchSectionInfo *match_section_infos) { - guint i; + MatchSectionInfo *m; + gsize i; if (!match_section_infos) return; - for (i = 0; match_section_infos[i].group_name; i++) { - g_free(match_section_infos[i].group_name); - g_slist_free_full(match_section_infos[i].match_device.spec, g_free); + for (m = match_section_infos; m->group_name; m++) { + g_free(m->group_name); + g_slist_free_full(m->match_device.spec, g_free); + for (i = 0; i < m->lookup_len; i++) { + g_free(m->lookup_idx[i].name_mutable); + g_free(m->lookup_idx[i].value_str_mutable); + } + g_free((gpointer) m->lookup_idx); } g_free(match_section_infos); } @@ -1649,11 +1732,11 @@ _match_section_infos_construct(GKeyFile *keyfile, const char *prefix) match_section_infos = g_new0(MatchSectionInfo, ngroups + 1 + (connection_tag ? 1 : 0)); for (i = 0; i < ngroups; i++) { /* pass ownership of @group on... */ - _get_connection_info_init(&match_section_infos[i], keyfile, groups[ngroups - i - 1]); + _match_section_info_init(&match_section_infos[i], keyfile, groups[ngroups - i - 1]); } if (connection_tag) { /* pass ownership of @connection_tag on... */ - _get_connection_info_init(&match_section_infos[i], keyfile, connection_tag); + _match_section_info_init(&match_section_infos[i], keyfile, connection_tag); } g_free(groups); From 9452d6946527eb4ae36f3c405d153e1fb4de7fbe Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 16 Jun 2021 09:50:46 +0200 Subject: [PATCH 5/5] config: avoid cloning string during nm_config_data_get_connection_default() et al. NMConfigData is immutable and with the previous commit are the strings already cached internally. There is no need to clone it. Of course, the callers must not assume that the string stays alive after a config reload (SIGHUP), where the NMConfigData might change. So they are not always alive, but long enough for all callers to avoid cloning the string. --- src/core/devices/nm-device.c | 135 +++++++++--------------- src/core/devices/wifi/nm-device-wifi.c | 4 +- src/core/devices/wifi/nm-wifi-factory.c | 5 +- src/core/nm-config-data.c | 32 +++--- src/core/nm-config-data.h | 24 ++--- src/core/tests/config/test-config.c | 84 +++++++-------- 6 files changed, 120 insertions(+), 164 deletions(-) diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c index f43646845a..1c70ecb328 100644 --- a/src/core/devices/nm-device.c +++ b/src/core/devices/nm-device.c @@ -820,8 +820,7 @@ _prop_get_connection_stable_id(NMDevice * self, * Especially with ${RANDOM} stable-id we want to generate *one* configuration * for each activation. */ if (G_UNLIKELY(!priv->current_stable_id)) { - gs_free char * default_id = NULL; - gs_free char * generated = NULL; + gs_free char * generated = NULL; NMUtilsStableType stable_type; NMSettingConnection *s_con; gboolean hwaddr_is_fake; @@ -834,11 +833,10 @@ _prop_get_connection_stable_id(NMDevice * self, stable_id = nm_setting_connection_get_stable_id(s_con); if (!stable_id) { - default_id = + stable_id = nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, NM_CON_DEFAULT("connection.stable-id"), self); - stable_id = default_id; } uuid = nm_connection_get_uuid(connection); @@ -897,7 +895,6 @@ _prop_get_ipv6_dhcp_duid(NMDevice * self, { NMSettingIPConfig *s_ip6; const char * duid; - gs_free char * duid_default = NULL; const char * duid_error; GBytes * duid_out; gboolean duid_enforce = TRUE; @@ -910,10 +907,9 @@ _prop_get_ipv6_dhcp_duid(NMDevice * self, duid = nm_setting_ip6_config_get_dhcp_duid(NM_SETTING_IP6_CONFIG(s_ip6)); if (!duid) { - duid_default = nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, - NM_CON_DEFAULT("ipv6.dhcp-duid"), - self); - duid = duid_default; + duid = nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, + NM_CON_DEFAULT("ipv6.dhcp-duid"), + self); if (!duid) duid = "lease"; } @@ -1390,7 +1386,6 @@ _prop_get_ipvx_dhcp_iaid(NMDevice * self, const int IS_IPv4 = NM_IS_IPv4(addr_family); NMSettingIPConfig *s_ip; const char * iaid_str; - gs_free char * iaid_str_free = NULL; guint32 iaid; const char * iface; const char * fail_reason; @@ -1399,11 +1394,10 @@ _prop_get_ipvx_dhcp_iaid(NMDevice * self, s_ip = nm_connection_get_setting_ip_config(connection, addr_family); iaid_str = nm_setting_ip_config_get_dhcp_iaid(s_ip); if (!iaid_str) { - iaid_str_free = nm_config_data_get_connection_default( - NM_CONFIG_GET_DATA, - IS_IPv4 ? NM_CON_DEFAULT("ipv4.dhcp-iaid") : NM_CON_DEFAULT("ipv6.dhcp-iaid"), - self); - iaid_str = iaid_str_free; + iaid_str = nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, + IS_IPv4 ? NM_CON_DEFAULT("ipv4.dhcp-iaid") + : NM_CON_DEFAULT("ipv6.dhcp-iaid"), + self); if (!iaid_str) { iaid_str = NM_IAID_IFNAME; is_explicit = FALSE; @@ -1560,12 +1554,10 @@ _prop_get_ipvx_dhcp_hostname_flags(NMDevice *self, int addr_family) } static const char * -_prop_get_connection_mud_url(NMDevice *self, NMSettingConnection *s_con, char **out_mud_url) +_prop_get_connection_mud_url(NMDevice *self, NMSettingConnection *s_con) { - const char * mud_url; - gs_free char *s = NULL; - - nm_assert(out_mud_url && !*out_mud_url); + const char *mud_url; + const char *s; mud_url = nm_setting_connection_get_mud_url(s_con); @@ -1582,7 +1574,7 @@ _prop_get_connection_mud_url(NMDevice *self, NMSettingConnection *s_con, char ** if (nm_streq(s, NM_CONNECTION_MUD_URL_NONE)) return NULL; if (nm_sd_http_url_is_valid_https(s)) - return (*out_mud_url = g_steal_pointer(&s)); + return s; } return NULL; @@ -1593,7 +1585,6 @@ _prop_get_ipv4_dhcp_client_id(NMDevice *self, NMConnection *connection, GBytes * { NMSettingIPConfig *s_ip4; const char * client_id; - gs_free char * client_id_default = NULL; guint8 * client_id_buf; const char * fail_reason; guint8 hwaddr_bin_buf[_NM_UTILS_HWADDR_LEN_MAX]; @@ -1607,13 +1598,12 @@ _prop_get_ipv4_dhcp_client_id(NMDevice *self, NMConnection *connection, GBytes * client_id = nm_setting_ip4_config_get_dhcp_client_id(NM_SETTING_IP4_CONFIG(s_ip4)); if (!client_id) { - client_id_default = - nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, - NM_CON_DEFAULT("ipv4.dhcp-client-id"), - self); - if (client_id_default && client_id_default[0]) { + client_id = nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, + NM_CON_DEFAULT("ipv4.dhcp-client-id"), + self); + if (client_id && !client_id[0]) { /* a non-empty client-id is always valid, see nm_dhcp_utils_client_id_string_to_bytes(). */ - client_id = client_id_default; + client_id = NULL; } } @@ -1745,8 +1735,7 @@ out_good: static GBytes * _prop_get_ipv4_dhcp_vendor_class_identifier(NMDevice *self, NMSettingIP4Config *s_ip4) { - gs_free char *config_data_prop = NULL; - gs_free char *to_free = NULL; + gs_free char *to_free = NULL; const char * conn_prop; GBytes * bytes = NULL; const char * bin; @@ -1756,13 +1745,13 @@ _prop_get_ipv4_dhcp_vendor_class_identifier(NMDevice *self, NMSettingIP4Config * if (!conn_prop) { /* set in NetworkManager.conf ? */ - config_data_prop = nm_config_data_get_connection_default( + conn_prop = nm_config_data_get_connection_default( NM_CONFIG_GET_DATA, NM_CON_DEFAULT("ipv4.dhcp-vendor-class-identifier"), self); - if (config_data_prop && nm_utils_validate_dhcp4_vendor_class_id(config_data_prop, NULL)) - conn_prop = config_data_prop; + if (conn_prop && !nm_utils_validate_dhcp4_vendor_class_id(conn_prop, NULL)) + conn_prop = NULL; } if (conn_prop) { @@ -1828,16 +1817,11 @@ _prop_get_ipv6_ip6_privacy(NMDevice *self) } static const char * -_prop_get_x_cloned_mac_address(NMDevice * self, - NMConnection *connection, - gboolean is_wifi, - char ** out_addr) +_prop_get_x_cloned_mac_address(NMDevice *self, NMConnection *connection, gboolean is_wifi) { NMSetting * setting; const char *addr = NULL; - nm_assert(out_addr && !*out_addr); - setting = nm_connection_get_setting(connection, is_wifi ? NM_TYPE_SETTING_WIRELESS : NM_TYPE_SETTING_WIRED); if (setting) { @@ -1846,7 +1830,7 @@ _prop_get_x_cloned_mac_address(NMDevice * self, } if (!addr) { - gs_free char *a = NULL; + const char *a; a = nm_config_data_get_connection_default( NM_CONFIG_GET_DATA, @@ -1861,36 +1845,28 @@ _prop_get_x_cloned_mac_address(NMDevice * self, NMSettingMacRandomization v; /* for backward compatibility, read the deprecated wifi.mac-address-randomization setting. */ - a = nm_config_data_get_connection_default( + v = nm_config_data_get_connection_default_int64( NM_CONFIG_GET_DATA, NM_CON_DEFAULT("wifi.mac-address-randomization"), - self); - v = _nm_utils_ascii_str_to_int64(a, - 10, - NM_SETTING_MAC_RANDOMIZATION_DEFAULT, - NM_SETTING_MAC_RANDOMIZATION_ALWAYS, - NM_SETTING_MAC_RANDOMIZATION_DEFAULT); + self, + NM_SETTING_MAC_RANDOMIZATION_DEFAULT, + NM_SETTING_MAC_RANDOMIZATION_ALWAYS, + NM_SETTING_MAC_RANDOMIZATION_DEFAULT); if (v == NM_SETTING_MAC_RANDOMIZATION_ALWAYS) addr = NM_CLONED_MAC_RANDOM; } } else if (NM_CLONED_MAC_IS_SPECIAL(a) || nm_utils_hwaddr_valid(a, ETH_ALEN)) - addr = *out_addr = g_steal_pointer(&a); + addr = a; } return addr; } static const char * -_prop_get_x_generate_mac_address_mask(NMDevice * self, - NMConnection *connection, - gboolean is_wifi, - char ** out_value) +_prop_get_x_generate_mac_address_mask(NMDevice *self, NMConnection *connection, gboolean is_wifi) { NMSetting * setting; - const char *value = NULL; - char * a; - - nm_assert(out_value && !*out_value); + const char *value; setting = nm_connection_get_setting(connection, is_wifi ? NM_TYPE_SETTING_WIRELESS : NM_TYPE_SETTING_WIRED); @@ -1903,15 +1879,11 @@ _prop_get_x_generate_mac_address_mask(NMDevice * self, return value; } - a = nm_config_data_get_connection_default( + return nm_config_data_get_connection_default( NM_CONFIG_GET_DATA, is_wifi ? NM_CON_DEFAULT("wifi.generate-mac-address-mask") : NM_CON_DEFAULT("ethernet.generate-mac-address-mask"), self); - if (!a) - return NULL; - *out_value = a; - return a; } /*****************************************************************************/ @@ -9417,7 +9389,6 @@ dhcp4_start(NMDevice *self) gs_unref_bytes GBytes *hwaddr = NULL; gs_unref_bytes GBytes *bcast_hwaddr = NULL; gs_unref_bytes GBytes *client_id = NULL; - gs_free char * mud_url_free = NULL; NMConnection * connection; NMSettingConnection * s_con; GError * error = NULL; @@ -9477,7 +9448,7 @@ dhcp4_start(NMDevice *self) nm_setting_ip_config_get_dhcp_hostname(s_ip4), nm_setting_ip4_config_get_dhcp_fqdn(NM_SETTING_IP4_CONFIG(s_ip4)), _prop_get_ipvx_dhcp_hostname_flags(self, AF_INET), - _prop_get_connection_mud_url(self, s_con, &mud_url_free), + _prop_get_connection_mud_url(self, s_con), client_id, _prop_get_ipvx_dhcp_timeout(self, AF_INET), _device_get_dhcp_anycast_address(self), @@ -9881,8 +9852,7 @@ dhcp6_start_with_link_ready(NMDevice *self, NMConnection *connection) gs_unref_bytes GBytes * duid = NULL; gboolean enforce_duid = FALSE; const NMPlatformLink * pllink; - gs_free char * mud_url_free = NULL; - GError * error = NULL; + GError * error = NULL; guint32 iaid; gboolean iaid_explicit; NMSettingConnection * s_con; @@ -9927,7 +9897,7 @@ dhcp6_start_with_link_ready(NMDevice *self, NMConnection *connection) nm_setting_ip_config_get_dhcp_send_hostname(s_ip6), nm_setting_ip_config_get_dhcp_hostname(s_ip6), _prop_get_ipvx_dhcp_hostname_flags(self, AF_INET6), - _prop_get_connection_mud_url(self, s_con, &mud_url_free), + _prop_get_connection_mud_url(self, s_con), duid, enforce_duid, iaid, @@ -17277,14 +17247,13 @@ _hw_addr_get_cloned(NMDevice * self, gboolean * preserve, char ** hwaddr, HwAddrType * hwaddr_type, - char ** hwaddr_detail, + const char ** hwaddr_detail, GError ** error) { NMDevicePrivate *priv; - gs_free char * addr_setting_free = NULL; - gs_free char * hw_addr_generated = NULL; - gs_free char * generate_mac_address_mask_tmp = NULL; - const char * addr, *addr_setting; + gs_free char * hw_addr_generated = NULL; + const char * addr; + const char * addr_setting; char * addr_out; HwAddrType type_out; @@ -17297,15 +17266,16 @@ _hw_addr_get_cloned(NMDevice * self, if (!connection) g_return_val_if_reached(FALSE); - addr = addr_setting = - _prop_get_x_cloned_mac_address(self, connection, is_wifi, &addr_setting_free); + addr_setting = _prop_get_x_cloned_mac_address(self, connection, is_wifi); + + addr = addr_setting; if (nm_streq(addr, NM_CLONED_MAC_PRESERVE)) { /* "preserve" means to reset the initial MAC address. */ NM_SET_OUT(preserve, TRUE); NM_SET_OUT(hwaddr, NULL); NM_SET_OUT(hwaddr_type, HW_ADDR_TYPE_UNSET); - NM_SET_OUT(hwaddr_detail, g_steal_pointer(&addr_setting_free) ?: g_strdup(addr_setting)); + NM_SET_OUT(hwaddr_detail, addr_setting); return TRUE; } @@ -17318,8 +17288,7 @@ _hw_addr_get_cloned(NMDevice * self, NM_SET_OUT(preserve, TRUE); NM_SET_OUT(hwaddr, NULL); NM_SET_OUT(hwaddr_type, HW_ADDR_TYPE_UNSET); - NM_SET_OUT(hwaddr_detail, - g_steal_pointer(&addr_setting_free) ?: g_strdup(addr_setting)); + NM_SET_OUT(hwaddr_detail, addr_setting); return TRUE; } else if (!addr) { g_set_error_literal(error, @@ -17339,10 +17308,7 @@ _hw_addr_get_cloned(NMDevice * self, } hw_addr_generated = nm_utils_hw_addr_gen_random_eth( nm_device_get_initial_hw_address(self), - _prop_get_x_generate_mac_address_mask(self, - connection, - is_wifi, - &generate_mac_address_mask_tmp)); + _prop_get_x_generate_mac_address_mask(self, connection, is_wifi)); if (!hw_addr_generated) { g_set_error(error, NM_DEVICE_ERROR, @@ -17370,10 +17336,7 @@ _hw_addr_get_cloned(NMDevice * self, stable_id, nm_device_get_ip_iface(self), nm_device_get_initial_hw_address(self), - _prop_get_x_generate_mac_address_mask(self, - connection, - is_wifi, - &generate_mac_address_mask_tmp)); + _prop_get_x_generate_mac_address_mask(self, connection, is_wifi)); if (!hw_addr_generated) { g_set_error(error, NM_DEVICE_ERROR, @@ -17397,7 +17360,7 @@ _hw_addr_get_cloned(NMDevice * self, NM_SET_OUT(preserve, FALSE); NM_SET_OUT(hwaddr, addr_out); NM_SET_OUT(hwaddr_type, type_out); - NM_SET_OUT(hwaddr_detail, g_steal_pointer(&addr_setting_free) ?: g_strdup(addr_setting)); + NM_SET_OUT(hwaddr_detail, addr_setting); return TRUE; out_no_action: NM_SET_OUT(preserve, FALSE); @@ -17427,7 +17390,7 @@ nm_device_hw_addr_set_cloned(NMDevice *self, NMConnection *connection, gboolean NMDevicePrivate *priv; gboolean preserve = FALSE; gs_free char * hwaddr = NULL; - gs_free char * detail = NULL; + const char * detail = NULL; HwAddrType type = HW_ADDR_TYPE_UNSET; gs_free_error GError *error = NULL; diff --git a/src/core/devices/wifi/nm-device-wifi.c b/src/core/devices/wifi/nm-device-wifi.c index dd52186d82..fca2fde515 100644 --- a/src/core/devices/wifi/nm-device-wifi.c +++ b/src/core/devices/wifi/nm-device-wifi.c @@ -1382,8 +1382,8 @@ _hw_addr_set_scanning(NMDeviceWifi *self, gboolean do_reset) now = nm_utils_get_monotonic_timestamp_sec(); if (now >= priv->hw_addr_scan_expire) { - gs_free char *generate_mac_address_mask = NULL; - gs_free char *hw_addr_scan = NULL; + gs_free char *hw_addr_scan = NULL; + const char * generate_mac_address_mask; /* the random MAC address for scanning expires after a while. * diff --git a/src/core/devices/wifi/nm-wifi-factory.c b/src/core/devices/wifi/nm-wifi-factory.c index c7be7ab8cb..d2269c74ef 100644 --- a/src/core/devices/wifi/nm-wifi-factory.c +++ b/src/core/devices/wifi/nm-wifi-factory.c @@ -74,7 +74,8 @@ create_device(NMDeviceFactory * factory, NMConnection * connection, gboolean * out_ignore) { - gs_free char *backend = NULL; + gs_free char *backend_free = NULL; + const char * backend; g_return_val_if_fail(iface != NULL, NULL); g_return_val_if_fail(plink != NULL, NULL); @@ -89,7 +90,7 @@ create_device(NMDeviceFactory * factory, plink, "wifi", NULL); - nm_strstrip(backend); + backend = nm_strstrip_avoid_copy_a(300, backend, &backend_free); nm_log_dbg(LOGD_PLATFORM | LOGD_WIFI, "(%s) config: backend is %s%s%s%s", diff --git a/src/core/nm-config-data.c b/src/core/nm-config-data.c index f6667e926d..e127ea23bb 100644 --- a/src/core/nm-config-data.c +++ b/src/core/nm-config-data.c @@ -359,9 +359,9 @@ nm_config_data_get_iwd_config_path(const NMConfigData *self) gboolean nm_config_data_get_ignore_carrier(const NMConfigData *self, NMDevice *device) { - gs_free char *value = NULL; - gboolean has_match; - int m; + const char *value; + gboolean has_match; + int m; g_return_val_if_fail(NM_IS_CONFIG_DATA(self), FALSE); g_return_val_if_fail(NM_IS_DEVICE(device), FALSE); @@ -1397,7 +1397,7 @@ _match_section_infos_lookup(const MatchSectionInfo *match_section_infos, NMDevice * device, const NMPlatformLink * pllink, const char * match_device_type, - char ** out_value) + const char ** out_value) { const char *match_dhcp_plugin; @@ -1436,7 +1436,7 @@ _match_section_infos_lookup(const MatchSectionInfo *match_section_infos, match = TRUE; if (match) { - *out_value = g_strdup(value); + *out_value = value; return match_section_infos; } } @@ -1446,7 +1446,7 @@ out: return NULL; } -char * +const char * nm_config_data_get_device_config(const NMConfigData *self, const char * property, NMDevice * device, @@ -1454,7 +1454,7 @@ nm_config_data_get_device_config(const NMConfigData *self, { const NMConfigDataPrivate *priv; const MatchSectionInfo * connection_info; - char * value = NULL; + const char * value; NM_SET_OUT(has_match, FALSE); @@ -1474,7 +1474,7 @@ nm_config_data_get_device_config(const NMConfigData *self, return value; } -char * +const char * nm_config_data_get_device_config_by_pllink(const NMConfigData * self, const char * property, const NMPlatformLink *pllink, @@ -1483,7 +1483,7 @@ nm_config_data_get_device_config_by_pllink(const NMConfigData * self, { const NMConfigDataPrivate *priv; const MatchSectionInfo * connection_info; - char * value = NULL; + const char * value; g_return_val_if_fail(self, NULL); g_return_val_if_fail(property && *property, NULL); @@ -1508,8 +1508,8 @@ nm_config_data_get_device_config_boolean(const NMConfigData *self, int val_no_match, int val_invalid) { - gs_free char *value = NULL; - gboolean has_match; + const char *value; + gboolean has_match; value = nm_config_data_get_device_config(self, property, device, &has_match); if (!has_match) @@ -1527,8 +1527,8 @@ nm_config_data_get_device_config_int64(const NMConfigData *self, gint64 val_no_match, gint64 val_invalid) { - gs_free char *value = NULL; - gboolean has_match; + const char *value; + gboolean has_match; value = nm_config_data_get_device_config(self, property, device, &has_match); if (!has_match) { @@ -1538,13 +1538,13 @@ nm_config_data_get_device_config_int64(const NMConfigData *self, return _nm_utils_ascii_str_to_int64(value, base, min, max, val_invalid); } -char * +const char * nm_config_data_get_connection_default(const NMConfigData *self, const char * property, NMDevice * device) { const NMConfigDataPrivate *priv; - char * value = NULL; + const char * value; g_return_val_if_fail(self, NULL); g_return_val_if_fail(property && *property, NULL); @@ -1583,7 +1583,7 @@ nm_config_data_get_connection_default_int64(const NMConfigData *self, gint64 max, gint64 fallback) { - gs_free char *value = NULL; + const char *value; value = nm_config_data_get_connection_default(self, property, device); return _nm_utils_ascii_str_to_int64(value, 10, min, max, fallback); diff --git a/src/core/nm-config-data.h b/src/core/nm-config-data.h index 0d133b8c57..42d69a8b92 100644 --- a/src/core/nm-config-data.h +++ b/src/core/nm-config-data.h @@ -207,9 +207,9 @@ extern const char *__stop_connection_defaults[]; name; \ }) -char *nm_config_data_get_connection_default(const NMConfigData *self, - const char * property, - NMDevice * device); +const char *nm_config_data_get_connection_default(const NMConfigData *self, + const char * property, + NMDevice * device); gint64 nm_config_data_get_connection_default_int64(const NMConfigData *self, const char * property, @@ -218,16 +218,16 @@ gint64 nm_config_data_get_connection_default_int64(const NMConfigData *self, gint64 max, gint64 fallback); -char *nm_config_data_get_device_config(const NMConfigData *self, - const char * property, - NMDevice * device, - gboolean * has_match); +const char *nm_config_data_get_device_config(const NMConfigData *self, + const char * property, + NMDevice * device, + gboolean * has_match); -char *nm_config_data_get_device_config_by_pllink(const NMConfigData * self, - const char * property, - const NMPlatformLink *pllink, - const char * match_device_type, - gboolean * has_match); +const char *nm_config_data_get_device_config_by_pllink(const NMConfigData * self, + const char * property, + const NMPlatformLink *pllink, + const char * match_device_type, + gboolean * has_match); gboolean nm_config_data_get_device_config_boolean(const NMConfigData *self, const char * property, diff --git a/src/core/tests/config/test-config.c b/src/core/tests/config/test-config.c index 3bb148152b..6794fbf6cb 100644 --- a/src/core/tests/config/test-config.c +++ b/src/core/tests/config/test-config.c @@ -151,6 +151,7 @@ test_config_simple(void) gs_unref_object NMConfig *config = NULL; gs_strfreev char ** plugins = NULL; char * value; + const char * cvalue; gs_unref_object NMDevice *dev50 = nm_test_device_new("00:00:00:00:00:50"); gs_unref_object NMDevice *dev51 = nm_test_device_new("00:00:00:00:00:51"); gs_unref_object NMDevice *dev52 = nm_test_device_new("00:00:00:00:00:52"); @@ -206,59 +207,50 @@ test_config_simple(void) g_assert_cmpstr(value, ==, "51"); g_free(value); - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv6.route-metric", - NULL); - g_assert_cmpstr(value, ==, NULL); - g_free(value); + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv6.route-metric", + NULL); + g_assert_cmpstr(cvalue, ==, NULL); - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.route-metric", - NULL); - g_assert_cmpstr(value, ==, "50"); - g_free(value); + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.route-metric", + NULL); + g_assert_cmpstr(cvalue, ==, "50"); - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.route-metric", - dev50); - g_assert_cmpstr(value, ==, "50"); - g_free(value); + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.route-metric", + dev50); + g_assert_cmpstr(cvalue, ==, "50"); - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.route-metric", - dev51); - g_assert_cmpstr(value, ==, "51"); - g_free(value); + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.route-metric", + dev51); + g_assert_cmpstr(cvalue, ==, "51"); - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.route-metric", - dev52); - g_assert_cmpstr(value, ==, "52"); - g_free(value); + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.route-metric", + dev52); + g_assert_cmpstr(cvalue, ==, "52"); - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ethernet.mtu", - dev51); - g_assert_cmpstr(value, ==, "9000"); - g_free(value); + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ethernet.mtu", + dev51); + g_assert_cmpstr(cvalue, ==, "9000"); - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ethernet.mtu", - dev50); - g_assert_cmpstr(value, ==, "1400"); - g_free(value); + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ethernet.mtu", + dev50); + g_assert_cmpstr(cvalue, ==, "1400"); - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.dns-priority", - dev51); - g_assert_cmpstr(value, ==, NULL); - g_free(value); + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.dns-priority", + dev51); + g_assert_cmpstr(cvalue, ==, NULL); - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.dns-priority", - dev50); - g_assert_cmpstr(value, ==, "60"); - g_free(value); + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.dns-priority", + dev50); + g_assert_cmpstr(cvalue, ==, "60"); } static void @@ -616,7 +608,7 @@ test_config_confdir(void) #define ASSERT_GET_CONN_DEFAULT(xconfig, xname, xvalue) \ G_STMT_START \ { \ - gs_free char *_value = \ + const char *_value = \ nm_config_data_get_connection_default(nm_config_get_data_orig(xconfig), \ (xname), \ NULL); \