chiark / gitweb /
strbuf: fix leak on memory error
[elogind.git] / src / shared / strbuf.c
index 9314f097ab5950091eb0977b87106883c828bd43..abc5c0dd296263c989d4ddc69ef30ae31453c92e 100644 (file)
@@ -3,7 +3,7 @@
 /***
   This file is part of systemd.
 
-  Copyright 2012 Kay Sievers <kay.sievers@vrfy.org>
+  Copyright 2012 Kay Sievers <kay@vrfy.org>
 
   systemd is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published by
 
 /*
  * Strbuf stores given strings in a single continous allocated memory
- * area. Identical strings are de-duplicated. If the tail of a string
- * already exist in the buffer, the tail is returned.
+ * area. Identical strings are de-duplicated and return the same offset
+ * as the first string stored. If the tail of a string already exists
+ * in the buffer, the tail is returned.
  *
- * A Particia Trie is used to maintain the information about the stored
- * strings.
+ * A trie (http://en.wikipedia.org/wiki/Trie) is used to maintain the
+ * information about the stored strings.
  *
  * Example of udev rules:
  *   $ ./udevadm test .
@@ -157,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;