initrd: use cleanup attribute in nmi_dt_reader_parse()

This commit is contained in:
Thomas Haller 2019-09-21 15:20:24 +02:00
parent 321a323df4
commit e7cf22be3e

View file

@ -135,7 +135,7 @@ str_addr (const char *str, int *family)
NMConnection * NMConnection *
nmi_dt_reader_parse (const char *sysfs_dir) nmi_dt_reader_parse (const char *sysfs_dir)
{ {
NMConnection *connection; gs_unref_object NMConnection *connection = NULL;
gs_free char *base = NULL; gs_free char *base = NULL;
gs_free char *bootpath = NULL; gs_free char *bootpath = NULL;
gs_strfreev char **tokens = NULL; gs_strfreev char **tokens = NULL;
@ -144,9 +144,8 @@ nmi_dt_reader_parse (const char *sysfs_dir)
const char *s_ipaddr = NULL; const char *s_ipaddr = NULL;
const char *s_netmask = NULL; const char *s_netmask = NULL;
const char *s_gateway = NULL; const char *s_gateway = NULL;
NMIPAddress *ipaddr = NULL; nm_auto_unref_ip_address NMIPAddress *ipaddr = NULL;
NMIPAddress *netmask = NULL; nm_auto_unref_ip_address NMIPAddress *gateway = NULL;
NMIPAddress *gateway = NULL;
const char *duplex = NULL; const char *duplex = NULL;
gs_free char *hwaddr = NULL; gs_free char *hwaddr = NULL;
gs_free char *local_hwaddr = NULL; gs_free char *local_hwaddr = NULL;
@ -279,10 +278,10 @@ nmi_dt_reader_parse (const char *sysfs_dir)
connection = nm_simple_connection_new (); connection = nm_simple_connection_new ();
nm_connection_add_setting (connection, nm_connection_add_setting (connection,
g_object_new (NM_TYPE_SETTING_CONNECTION, g_object_new (NM_TYPE_SETTING_CONNECTION,
NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME,
NM_SETTING_CONNECTION_ID, "OpenFirmware Connection", NM_SETTING_CONNECTION_ID, "OpenFirmware Connection",
NULL)); NULL));
s_ip4 = nm_setting_ip4_config_new (); s_ip4 = nm_setting_ip4_config_new ();
nm_connection_add_setting (connection, s_ip4); nm_connection_add_setting (connection, s_ip4);
@ -298,6 +297,8 @@ nmi_dt_reader_parse (const char *sysfs_dir)
bootp = TRUE; bootp = TRUE;
if (!bootp) { if (!bootp) {
nm_auto_unref_ip_address NMIPAddress *netmask = NULL;
netmask = dt_get_ipaddr_property (base, "chosen", "netmask-ip", &family); netmask = dt_get_ipaddr_property (base, "chosen", "netmask-ip", &family);
gateway = dt_get_ipaddr_property (base, "chosen", "gateway-ip", &family); gateway = dt_get_ipaddr_property (base, "chosen", "gateway-ip", &family);
if (gateway) if (gateway)
@ -305,9 +306,9 @@ nmi_dt_reader_parse (const char *sysfs_dir)
ipaddr = dt_get_ipaddr_property (base, "chosen", "client-ip", &family); ipaddr = dt_get_ipaddr_property (base, "chosen", "client-ip", &family);
if (family == AF_UNSPEC) { if (family == AF_UNSPEC) {
g_warn_if_fail (netmask == NULL); nm_assert (netmask == NULL);
g_warn_if_fail (ipaddr == NULL); nm_assert (gateway == NULL);
g_warn_if_fail (gateway == NULL); nm_assert (ipaddr == NULL);
netmask = str_addr (s_netmask, &family); netmask = str_addr (s_netmask, &family);
ipaddr = str_addr (s_ipaddr, &family); ipaddr = str_addr (s_ipaddr, &family);
@ -326,9 +327,6 @@ nmi_dt_reader_parse (const char *sysfs_dir)
_LOGW (LOGD_CORE, "Unable to determine the network prefix"); _LOGW (LOGD_CORE, "Unable to determine the network prefix");
else else
nm_ip_address_set_prefix (ipaddr, prefix); nm_ip_address_set_prefix (ipaddr, prefix);
if (netmask)
nm_ip_address_unref (netmask);
} }
if (!ipaddr) { if (!ipaddr) {
@ -373,11 +371,6 @@ nmi_dt_reader_parse (const char *sysfs_dir)
g_object_set (s_ip, NM_SETTING_IP_CONFIG_GATEWAY, s_gateway, NULL); g_object_set (s_ip, NM_SETTING_IP_CONFIG_GATEWAY, s_gateway, NULL);
} }
if (ipaddr)
nm_ip_address_unref (ipaddr);
if (gateway)
nm_ip_address_unref (gateway);
if (duplex || speed || hwaddr || local_hwaddr) { if (duplex || speed || hwaddr || local_hwaddr) {
s_wired = nm_setting_wired_new (); s_wired = nm_setting_wired_new ();
nm_connection_add_setting (connection, s_wired); nm_connection_add_setting (connection, s_wired);
@ -390,11 +383,11 @@ nmi_dt_reader_parse (const char *sysfs_dir)
NULL); NULL);
} }
if (!nm_connection_normalize (connection, NULL, NULL, &error)) { if (!nm_connection_normalize (connection, NULL, NULL, &error)) {
_LOGW (LOGD_CORE, "Generated an invalid connection: %s", _LOGW (LOGD_CORE, "Generated an invalid connection: %s",
error->message); error->message);
g_clear_pointer (&connection, g_object_unref); g_clear_pointer (&connection, g_object_unref);
} }
return connection; return g_steal_pointer (&connection);
} }