From 7bf8c45b192547f83adddde83ffcbac9e90fe0b8 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 22 Aug 2019 11:02:59 +0200 Subject: device/wifi: cleanup supplicant_iface_wps_credentials_cb() Restructure code to return early and free resources with nm_auto. --- src/devices/wifi/nm-device-wifi.c | 41 ++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/devices/wifi/nm-device-wifi.c b/src/devices/wifi/nm-device-wifi.c index a587348c89..521d9e3c78 100644 --- a/src/devices/wifi/nm-device-wifi.c +++ b/src/devices/wifi/nm-device-wifi.c @@ -1755,10 +1755,11 @@ supplicant_iface_wps_credentials_cb (NMSupplicantInterface *iface, NMDeviceWifi *self) { NMActRequest *req; - GVariant *val, *secrets = NULL; + gs_unref_variant GVariant *val_key = NULL; + gs_unref_variant GVariant *secrets = NULL; + gs_free_error GError *error = NULL; const char *array; gsize psk_len = 0; - GError *error = NULL; if (nm_device_get_state (NM_DEVICE (self)) != NM_DEVICE_STATE_NEED_AUTH) { _LOGI (LOGD_DEVICE | LOGD_WIFI, "WPS: The connection can't be updated with credentials"); @@ -1770,11 +1771,11 @@ supplicant_iface_wps_credentials_cb (NMSupplicantInterface *iface, req = nm_device_get_act_request (NM_DEVICE (self)); g_return_if_fail (NM_IS_ACT_REQUEST (req)); - val = g_variant_lookup_value (credentials, "Key", G_VARIANT_TYPE_BYTESTRING); - if (val) { + val_key = g_variant_lookup_value (credentials, "Key", G_VARIANT_TYPE_BYTESTRING); + if (val_key) { char psk[64]; - array = g_variant_get_fixed_array (val, &psk_len, 1); + array = g_variant_get_fixed_array (val_key, &psk_len, 1); if (psk_len >= 8 && psk_len <= 63) { memcpy (psk, array, psk_len); psk[psk_len] = '\0'; @@ -1787,22 +1788,22 @@ supplicant_iface_wps_credentials_cb (NMSupplicantInterface *iface, } if (!secrets) _LOGW (LOGD_DEVICE | LOGD_WIFI, "WPS: ignore invalid PSK"); - g_variant_unref (val); - } - if (secrets) { - if (nm_settings_connection_new_secrets (nm_act_request_get_settings_connection (req), - nm_act_request_get_applied_connection (req), - NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, - secrets, - &error)) { - wifi_secrets_cancel (self); - nm_device_activate_schedule_stage1_device_prepare (NM_DEVICE (self)); - } else { - _LOGW (LOGD_DEVICE | LOGD_WIFI, "WPS: Could not update the connection with credentials: %s", error->message); - g_error_free (error); - } - g_variant_unref (secrets); } + + if (!secrets) + return; + + if (!nm_settings_connection_new_secrets (nm_act_request_get_settings_connection (req), + nm_act_request_get_applied_connection (req), + NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, + secrets, + &error)) { + _LOGW (LOGD_DEVICE | LOGD_WIFI, "WPS: Could not update the connection with credentials: %s", error->message); + return; + } + + wifi_secrets_cancel (self); + nm_device_activate_schedule_stage1_device_prepare (NM_DEVICE (self)); } static gboolean -- cgit v1.2.1