diff options
author | Luca Boccassi <bluca@debian.org> | 2024-06-17 21:08:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-17 21:08:22 +0200 |
commit | 05c0671ce8e6d1a342c4e4055a0e4e56e00fe79a (patch) | |
tree | 94ff9f670c160c33e356f0a0ec0c9923f8f403fb /src | |
parent | Merge pull request #33375 from yuwata/trivial-follow-ups (diff) | |
parent | strbuf: use _cleanup_ attribute at one more place (diff) | |
download | systemd-05c0671ce8e6d1a342c4e4055a0e4e56e00fe79a.tar.xz systemd-05c0671ce8e6d1a342c4e4055a0e4e56e00fe79a.zip |
Merge pull request #33377 from yuwata/strbuf-cleanups
strbuf: several cleanups
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/strbuf.c | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/src/basic/strbuf.c b/src/basic/strbuf.c index 226893842b..a53d733594 100644 --- a/src/basic/strbuf.c +++ b/src/basic/strbuf.c @@ -27,41 +27,40 @@ */ struct strbuf* strbuf_new(void) { - struct strbuf *str; + _cleanup_(strbuf_freep) struct strbuf *str = NULL; str = new(struct strbuf, 1); if (!str) return NULL; + *str = (struct strbuf) { .buf = new0(char, 1), .root = new0(struct strbuf_node, 1), .len = 1, .nodes_count = 1, }; - if (!str->buf || !str->root) { - free(str->buf); - free(str->root); - return mfree(str); - } + if (!str->buf || !str->root) + return NULL; - return str; + return TAKE_PTR(str); } static struct strbuf_node* strbuf_node_cleanup(struct strbuf_node *node) { - size_t i; + assert(node); + + FOREACH_ARRAY(child, node->children, node->children_count) + strbuf_node_cleanup(child->child); - for (i = 0; i < node->children_count; i++) - strbuf_node_cleanup(node->children[i].child); free(node->children); return mfree(node); } /* clean up trie data, leave only the string buffer */ void strbuf_complete(struct strbuf *str) { - if (!str) + if (!str || !str->root) return; - if (str->root) - str->root = strbuf_node_cleanup(str->root); + + str->root = strbuf_node_cleanup(str->root); } /* clean up everything */ @@ -74,9 +73,11 @@ struct strbuf* strbuf_free(struct strbuf *str) { return mfree(str); } -static int strbuf_children_cmp(const struct strbuf_child_entry *n1, - const struct strbuf_child_entry *n2) { - return n1->c - n2->c; +static int strbuf_children_cmp(const struct strbuf_child_entry *n1, const struct strbuf_child_entry *n2) { + assert(n1); + assert(n2); + + return CMP(n1->c, n2->c); } static void bubbleinsert(struct strbuf_node *node, |