From ca3237150e965c33863addb9e89100030692d36d Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 3 Feb 2019 12:56:24 +0100 Subject: [PATCH 1/2] hashmap: avoid uninitialized variable warning in internal_hashmap_clear() GCC 8.2 with LTO and -O2 emits a false warning: src/basic/hashmap.c: In function 'internal_hashmap_free.constprop': src/basic/hashmap.c:898:33: error: 'k' may be used uninitialized in this function [-Werror=maybe-uninitialized] free_key(k); ^ Avoid it by initializing the variable. --- src/basic/hashmap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index f2e33d2265..80fec8fb53 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -888,7 +888,8 @@ void internal_hashmap_clear(HashmapBase *h, free_func_t default_free_key, free_f * themselves from our hash table a second time, the entry is already gone. */ while (internal_hashmap_size(h) > 0) { - void *v, *k; + void *k = NULL; + void *v; v = internal_hashmap_first_key_and_value(h, true, &k); From 51c682df38fa032599c6c0e4556136c7d6d7c504 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 4 Feb 2019 09:36:17 +0100 Subject: [PATCH 2/2] hashmap: always set key output argument of internal_hashmap_first_key_and_value() internal_hashmap_first_key_and_value() returns the first value, or %NULL if the hashmap is empty. However, hashmaps may contain %NULL values. That means, a caller getting %NULL doesn't know whether the hashmap is empty or whether the first value is %NULL. For example, a caller may be tempted to do something like: if ((val = hashmap_steal_first_key_and_value (h, (void **) key))) { // process first entry. } But this is only correct if the caller made sure that the hash is either not empty or contains no NULL values. Anyway, since a %NULL return value can signal an empty hash or a %NULL value, it seems error prone to leave the key output argument uninitialized in situations that the caller cannot clearly distinguish (without making additional assumptions). --- src/basic/hashmap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index 80fec8fb53..66e9e0046b 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -1516,8 +1516,11 @@ void *internal_hashmap_first_key_and_value(HashmapBase *h, bool remove, void **r unsigned idx; idx = find_first_entry(h); - if (idx == IDX_NIL) + if (idx == IDX_NIL) { + if (ret_key) + *ret_key = NULL; return NULL; + } e = bucket_at(h, idx); key = (void*) e->key;