libnm: fix memleak in nm_setting_wired_get_s390_option() and refactor

@keys was leaked. Also refactor the function to iterate the hash only
once.
This commit is contained in:
Thomas Haller
2015-02-07 16:52:43 +01:00
parent f1bfe459ee
commit 7a82d64e47
2 changed files with 30 additions and 36 deletions

View File

@@ -434,27 +434,24 @@ nm_setting_wired_get_s390_option (NMSettingWired *setting,
const char **out_key, const char **out_key,
const char **out_value) const char **out_value)
{ {
NMSettingWiredPrivate *priv; const char *_key, *_value;
guint32 num_keys; GHashTableIter iter;
GList *keys; guint i = 0;
const char *_key = NULL, *_value = NULL;
g_return_val_if_fail (NM_IS_SETTING_WIRED (setting), FALSE); g_return_val_if_fail (NM_IS_SETTING_WIRED (setting), FALSE);
priv = NM_SETTING_WIRED_GET_PRIVATE (setting); g_hash_table_iter_init (&iter, NM_SETTING_WIRED_GET_PRIVATE (setting)->s390_options);
while (g_hash_table_iter_next (&iter, (gpointer) &_key, (gpointer) &_value)) {
num_keys = nm_setting_wired_get_num_s390_options (setting); if (i == idx) {
g_return_val_if_fail (idx < num_keys, FALSE); if (out_key)
*out_key = _key;
keys = g_hash_table_get_keys (priv->s390_options); if (out_value)
_key = g_list_nth_data (keys, idx); *out_value = _value;
_value = g_hash_table_lookup (priv->s390_options, _key); return TRUE;
}
if (out_key) i++;
*out_key = _key; }
if (out_value) g_return_val_if_reached (FALSE);
*out_value = _value;
return TRUE;
} }
/** /**

View File

@@ -474,27 +474,24 @@ nm_setting_wired_get_s390_option (NMSettingWired *setting,
const char **out_key, const char **out_key,
const char **out_value) const char **out_value)
{ {
NMSettingWiredPrivate *priv; const char *_key, *_value;
guint32 num_keys; GHashTableIter iter;
GList *keys; guint i = 0;
const char *_key = NULL, *_value = NULL;
g_return_val_if_fail (NM_IS_SETTING_WIRED (setting), FALSE); g_return_val_if_fail (NM_IS_SETTING_WIRED (setting), FALSE);
priv = NM_SETTING_WIRED_GET_PRIVATE (setting); g_hash_table_iter_init (&iter, NM_SETTING_WIRED_GET_PRIVATE (setting)->s390_options);
while (g_hash_table_iter_next (&iter, (gpointer) &_key, (gpointer) &_value)) {
num_keys = nm_setting_wired_get_num_s390_options (setting); if (i == idx) {
g_return_val_if_fail (idx < num_keys, FALSE); if (out_key)
*out_key = _key;
keys = g_hash_table_get_keys (priv->s390_options); if (out_value)
_key = g_list_nth_data (keys, idx); *out_value = _value;
_value = g_hash_table_lookup (priv->s390_options, _key); return TRUE;
}
if (out_key) i++;
*out_key = _key; }
if (out_value) g_return_val_if_reached (FALSE);
*out_value = _value;
return TRUE;
} }
/** /**