mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-12-23 17:34:00 +03:00
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).
This commit is contained in:
parent
ca3237150e
commit
51c682df38
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user