diff options
author | Thomas Haller <thaller@redhat.com> | 2017-04-28 12:38:55 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-04-28 12:42:18 +0200 |
commit | 2c46b65923fdd6ca2fab90d8e128c070f6385f1b (patch) | |
tree | 6bb7d3b703262fb5b8c7c8824f5057b077333190 | |
parent | f16ec61a011396ee30fd4748d64eca48ae93fff7 (diff) | |
download | NetworkManager-2c46b65923fdd6ca2fab90d8e128c070f6385f1b.tar.gz |
ifcfg-rh: use cleanup attribute for error handling in reader
Drop some uses of "goto error". Instead, have the memory owned
by an auto variable and automatically release it via the cleanup
attribute.
-rw-r--r-- | src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c | 93 |
1 files changed, 40 insertions, 53 deletions
diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c index e7dbc7ed26..a520a6076e 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c @@ -653,9 +653,10 @@ read_one_ip4_route (shvarFile *ifcfg, static gboolean read_route_file_legacy (const char *filename, NMSettingIPConfig *s_ip4, GError **error) { - char *contents = NULL; + gs_free char *contents = NULL; + gs_strfreev char **lines = NULL; gsize len = 0; - char **lines = NULL, **iter; + char **iter; GRegex *regex_to1, *regex_to2, *regex_via, *regex_metric; GMatchInfo *match_info; int prefix_int; @@ -675,8 +676,8 @@ read_route_file_legacy (const char *filename, NMSettingIPConfig *s_ip4, GError * g_return_val_if_fail (!error || !*error, FALSE); /* Read the route file */ - if (!g_file_get_contents (filename, &contents, &len, NULL) || !len) { - g_free (contents); + if ( !g_file_get_contents (filename, &contents, &len, NULL) + || !len) { return TRUE; /* missing/empty = success */ } @@ -787,8 +788,6 @@ read_route_file_legacy (const char *filename, NMSettingIPConfig *s_ip4, GError * success = TRUE; error: - g_free (contents); - g_strfreev (lines); g_regex_unref (regex_to1); g_regex_unref (regex_to2); g_regex_unref (regex_via); @@ -3384,23 +3383,22 @@ make_leap_setting (shvarFile *ifcfg, const char *file, GError **error) { - NMSettingWirelessSecurity *wsec; + gs_unref_object NMSettingWirelessSecurity *wsec = NULL; shvarFile *keys_ifcfg; - char *value; + gs_free char *value = NULL; NMSettingSecretFlags flags; wsec = NM_SETTING_WIRELESS_SECURITY (nm_setting_wireless_security_new ()); value = svGetValueStr_cp (ifcfg, "KEY_MGMT"); if (!value || strcmp (value, "IEEE8021X")) - goto error; /* Not LEAP */ + return NULL; + nm_clear_g_free (&value); - g_free (value); value = svGetValueStr_cp (ifcfg, "SECURITYMODE"); if (!value || strcasecmp (value, "leap")) - goto error; /* Not LEAP */ - - g_free (value); + return NULL; /* Not LEAP */ + nm_clear_g_free (&value); flags = read_secret_flags (ifcfg, "IEEE_8021X_PASSWORD_FLAGS"); g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_LEAP_PASSWORD_FLAGS, flags, NULL); @@ -3418,30 +3416,24 @@ make_leap_setting (shvarFile *ifcfg, } if (value && strlen (value)) g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_LEAP_PASSWORD, value, NULL); - g_free (value); + nm_clear_g_free (&value); } value = svGetValueStr_cp (ifcfg, "IEEE_8021X_IDENTITY"); if (!value) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Missing LEAP identity"); - goto error; + return NULL; } g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, value, NULL); - g_free (value); + nm_clear_g_free (&value); g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap", NULL); - return (NMSetting *) wsec; - -error: - g_free (value); - if (wsec) - g_object_unref (wsec); - return NULL; + return (NMSetting *) g_steal_pointer (&wsec); } static NMSetting * @@ -4007,8 +3999,8 @@ make_wired_setting (shvarFile *ifcfg, NMSetting8021x **s_8021x, GError **error) { - NMSettingWired *s_wired; - char *value = NULL; + gs_unref_object NMSettingWired *s_wired = NULL; + gs_free char *value = NULL; char *nettype; s_wired = NM_SETTING_WIRED (nm_setting_wired_new ()); @@ -4022,14 +4014,14 @@ make_wired_setting (shvarFile *ifcfg, g_object_set (s_wired, NM_SETTING_WIRED_MTU, (guint) mtu, NULL); else PARSE_WARNING ("invalid MTU '%s'", value); - g_free (value); + nm_clear_g_free (&value); } value = svGetValueStr_cp (ifcfg, "HWADDR"); if (value) { value = g_strstrip (value); g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, value, NULL); - g_free (value); + nm_clear_g_free (&value); } value = svGetValueStr_cp (ifcfg, "SUBCHANNELS"); @@ -4060,19 +4052,19 @@ make_wired_setting (shvarFile *ifcfg, g_object_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, chans, NULL); g_strfreev (chans); } - g_free (value); + nm_clear_g_free (&value); } value = svGetValueStr_cp (ifcfg, "PORTNAME"); if (value) { nm_setting_wired_add_s390_option (s_wired, "portname", value); - g_free (value); + nm_clear_g_free (&value); } value = svGetValueStr_cp (ifcfg, "CTCPROT"); if (value) { nm_setting_wired_add_s390_option (s_wired, "ctcprot", value); - g_free (value); + nm_clear_g_free (&value); } nettype = svGetValueStr_cp (ifcfg, "NETTYPE"); @@ -4102,28 +4094,28 @@ make_wired_setting (shvarFile *ifcfg, iter++; } g_strfreev (options); - g_free (value); + nm_clear_g_free (&value); } - value = svGetValueStr_cp (ifcfg, "MACADDR"); - if (value) { - value = g_strstrip (value); - g_object_set (s_wired, NM_SETTING_WIRED_CLONED_MAC_ADDRESS, value, NULL); - g_free (value); - } + g_object_set (s_wired, + NM_SETTING_WIRED_CLONED_MAC_ADDRESS, + svGetValueStr (ifcfg, "MACADDR", &value), + NULL); + nm_clear_g_free (&value); - value = svGetValueStr_cp (ifcfg, "GENERATE_MAC_ADDRESS_MASK"); - g_object_set (s_wired, NM_SETTING_WIRED_GENERATE_MAC_ADDRESS_MASK, value, NULL); - g_free (value); + g_object_set (s_wired, + NM_SETTING_WIRED_GENERATE_MAC_ADDRESS_MASK, + svGetValueStr (ifcfg, "GENERATE_MAC_ADDRESS_MASK", &value), + NULL); + nm_clear_g_free (&value); value = svGetValueStr_cp (ifcfg, "HWADDR_BLACKLIST"); if (value) { - char **strv; + gs_strfreev char **strv = NULL; strv = transform_hwaddr_blacklist (value); g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS_BLACKLIST, strv, NULL); - g_strfreev (strv); - g_free (value); + nm_clear_g_free (&value); } value = svGetValueStr_cp (ifcfg, "KEY_MGMT"); @@ -4131,25 +4123,20 @@ make_wired_setting (shvarFile *ifcfg, if (!strcmp (value, "IEEE8021X")) { *s_8021x = fill_8021x (ifcfg, file, value, FALSE, error); if (!*s_8021x) - goto error; + return NULL; } else { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Unknown wired KEY_MGMT type '%s'", value); - goto error; + return NULL; } - g_free (value); + nm_clear_g_free (&value); } parse_ethtool_options (ifcfg, s_wired, svGetValue (ifcfg, "ETHTOOL_OPTS", &value)); - g_free (value); - - return (NMSetting *) s_wired; + nm_clear_g_free (&value); -error: - g_free (value); - g_object_unref (s_wired); - return NULL; + return (NMSetting *) g_steal_pointer (&s_wired); } static NMConnection * |