]> git.ipfire.org Git - thirdparty/systemd.git/commit
hashmap: always set key output argument of internal_hashmap_first_key_and_value() 11647/head
authorThomas Haller <thaller@redhat.com>
Mon, 4 Feb 2019 08:36:17 +0000 (09:36 +0100)
committerThomas Haller <thaller@redhat.com>
Mon, 4 Feb 2019 08:47:00 +0000 (09:47 +0100)
commit51c682df38fa032599c6c0e4556136c7d6d7c504
tree11f4dc96846d173690d464e702e27b3845574ea5
parentca3237150e965c33863addb9e89100030692d36d
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