]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hashmap: Add extra uncounted entry to returned array from hashmap_dump_sorted()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 20 Oct 2023 10:07:32 +0000 (12:07 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 20 Oct 2023 12:09:32 +0000 (14:09 +0200)
This allows using the returned array as a strv.

src/basic/hashmap.c

index ac1c73297885d9e00b15df431634632c6e87c7a7..894760ca6098df22a83f898fce06d2c7207e2e44 100644 (file)
@@ -2135,7 +2135,9 @@ int _hashmap_dump_sorted(HashmapBase *h, void ***ret, size_t *ret_n) {
                 return 0;
         }
 
-        entries = new(struct hashmap_base_entry*, _hashmap_size(h));
+        /* We append one more element than needed so that the resulting array can be used as a strv. We
+         * don't count this entry in the returned size. */
+        entries = new(struct hashmap_base_entry*, _hashmap_size(h) + 1);
         if (!entries)
                 return -ENOMEM;
 
@@ -2143,6 +2145,7 @@ int _hashmap_dump_sorted(HashmapBase *h, void ***ret, size_t *ret_n) {
                 entries[n++] = bucket_at(h, idx);
 
         assert(n == _hashmap_size(h));
+        entries[n] = NULL;
 
         typesafe_qsort_r(entries, n, hashmap_entry_compare, h->hash_ops->compare);