chiark / gitweb /
strbuf: fix leak on memory error
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 31 Mar 2013 00:32:56 +0000 (20:32 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 31 Mar 2013 18:34:24 +0000 (14:34 -0400)
Not very likely, but let's fix it for the matter of
principle.

src/shared/strbuf.c

index 915cd3ac99bc4ae6491512d2628e26d667a36f6a..abc5c0dd296263c989d4ddc69ef30ae31453c92e 100644 (file)
@@ -158,14 +158,18 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
         node_child = new0(struct strbuf_node, 1);
         if (!node_child)
                 return -ENOMEM;
-        str->nodes_count++;
         node_child->value_off = off;
         node_child->value_len = len;
 
         /* extend array, add new entry, sort for bisection */
         child = realloc(node->children, (node->children_count + 1) * sizeof(struct strbuf_child_entry));
-        if (!child)
+        if (!child) {
+                free(node_child);
                 return -ENOMEM;
+        }
+
+        str->nodes_count++;
+
         node->children = child;
         node->children[node->children_count].c = c;
         node->children[node->children_count].child = node_child;