HTArray_addObject() underallocates with growby == 1

I had a program ask for the keys of a hashtable that had one item in the
hash, and then, when it tried to iterate over the returned array, it did
not find a null terminator at the end of the 1 item.  The reason for this
is two-fold:

 + The HTHashtable_keys() routine creates an array with a growby equal to
   the number of elements in the hash.  Normally this just wastes memory
   by returning an array with 2*N elements allocated rather than the more
   optimal N+1 (since we need one extra element to store the NULL), but in
   the N==1 case, it also triggers the following bug.

 + The HTArray_addObject() routine does not properly handle a growby size
   of 1.  The first allocation only allocates 1 item (rather than the 2
   required to be able to store the first item and a terminating NULL),
   and the array will always stay under-allocated after that.

The appended patch fixes both these deficiencies.  (Note that I didn't
check to see if other allocated objects had the same problem as the
HTArray object.)

..wayne..

Received on Sunday, 3 September 2000 17:19:39 UTC