From 6e12e18c15f8b0daa629f3b242f44e2215df0669 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 14 Jun 2018 11:02:24 +0200 Subject: [PATCH] device: simplify nm_device_hash_check_invalid_keys() Rather trivial change. Return-early, to completely handle the simpler case (the success case) first. In the failure case, we only need extra effort to generate a nice error message. --- src/devices/nm-device.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 6cda25bb3e..9df4141016 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -10203,7 +10203,7 @@ _nm_device_hash_check_invalid_keys (GHashTable *hash, const char *setting_name, nm_assert (whitelist && whitelist[0]); #if NM_MORE_ASSERTS > 10 - /* Assert that the keys are unique. */ + /* Require whitelist to only contain unique keys. */ { gs_unref_hashtable GHashTable *check_dups = g_hash_table_new_full (nm_str_hash, g_str_equal, NULL, NULL); @@ -10220,14 +10220,16 @@ _nm_device_hash_check_invalid_keys (GHashTable *hash, const char *setting_name, found_whitelisted_keys++; } - if (found_whitelisted_keys != g_hash_table_size (hash)) { + if (found_whitelisted_keys == g_hash_table_size (hash)) { + /* Good, there are only whitelisted keys in the hash. */ + return TRUE; + } + + if (error) { GHashTableIter iter; const char *k = NULL; const char *first_invalid_key = NULL; - if (!error) - return FALSE; - g_hash_table_iter_init (&iter, hash); while (g_hash_table_iter_next (&iter, (gpointer *) &k, NULL)) { if (nm_utils_strv_find_first ((char **) whitelist, -1, k) < 0) { @@ -10250,10 +10252,9 @@ _nm_device_hash_check_invalid_keys (GHashTable *hash, const char *setting_name, first_invalid_key); } g_return_val_if_fail (first_invalid_key, FALSE); - return FALSE; } - return TRUE; + return FALSE; } void