Document expand_item_list's args & make sure incr==0 works OK.
authorWayne Davison <wayned@samba.org>
Fri, 18 Dec 2015 22:38:10 +0000 (14:38 -0800)
committerWayne Davison <wayned@samba.org>
Fri, 18 Dec 2015 22:41:22 +0000 (14:41 -0800)
util.c

diff --git a/util.c b/util.c
index 41e0c780161e6261c538edeb21a620309f5406cb..3bece5ca3b75c1b57f222b5f005ffe2c55cff88e 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1605,6 +1605,12 @@ int flist_ndx_pop(flist_ndx_list *lp)
        return ndx;
 }
 
+/* Make sure there is room for one more item in the item list.  If there
+ * is not, expand the list as indicated by the value of "incr":
+ *  - if incr < 0 then increase the malloced size by -1 * incr
+ *  - if incr >= 0 then either make the malloced size equal to "incr"
+ *    or (if that's not large enough) double the malloced size
+ */
 void *expand_item_list(item_list *lp, size_t item_size,
                       const char *desc, int incr)
 {
@@ -1616,9 +1622,11 @@ void *expand_item_list(item_list *lp, size_t item_size,
                        new_size += -incr; /* increase slowly */
                else if (new_size < (size_t)incr)
                        new_size = incr;
-               else
+               else if (new_size)
                        new_size *= 2;
-               if (new_size < lp->malloced)
+               else
+                       new_size = 1;
+               if (new_size <= lp->malloced)
                        overflow_exit("expand_item_list");
                /* Using _realloc_array() lets us pass the size, not a type. */
                new_ptr = _realloc_array(lp->items, item_size, new_size);