]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/strbuf: use _cleanup_
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 8 Apr 2021 10:54:55 +0000 (12:54 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 8 Apr 2021 11:00:50 +0000 (13:00 +0200)
src/basic/strbuf.c

index 212b561fa645bb2fbcca76271d1f04402feb6576..0617acc8d215308b58af4cab8101cc6d3e76c2cf 100644 (file)
@@ -107,11 +107,9 @@ static void bubbleinsert(struct strbuf_node *node,
 /* add string, return the index/offset into the buffer */
 ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
         uint8_t c;
-        struct strbuf_node *node;
-        size_t depth;
         char *buf_new;
         struct strbuf_child_entry *child;
-        struct strbuf_node *node_child;
+        struct strbuf_node *node;
         ssize_t off;
 
         if (!str->root)
@@ -127,7 +125,7 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
         str->in_len += len;
 
         node = str->root;
-        for (depth = 0; depth <= len; depth++) {
+        for (size_t depth = 0; depth <= len; depth++) {
                 struct strbuf_child_entry search;
 
                 /* match against current node */
@@ -159,6 +157,8 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
         str->buf[str->len++] = '\0';
 
         /* new node */
+        _cleanup_free_ struct strbuf_node *node_child = NULL;
+
         node_child = new(struct strbuf_node, 1);
         if (!node_child)
                 return -ENOMEM;
@@ -169,15 +169,13 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
 
         /* extend array, add new entry, sort for bisection */
         child = reallocarray(node->children, node->children_count + 1, sizeof(struct strbuf_child_entry));
-        if (!child) {
-                free(node_child);
+        if (!child)
                 return -ENOMEM;
-        }
 
         str->nodes_count++;
 
         node->children = child;
-        bubbleinsert(node, c, node_child);
+        bubbleinsert(node, c, TAKE_PTR(node_child));
 
         return off;
 }