W3C home > Mailing lists > Public > www-lib@w3.org > July to September 2000

HTArray_addObject() underallocates with growby == 1

From: Wayne Davison <wayned@users.sourceforge.net>
Date: Sun, 3 Sep 2000 14:19:05 -0700 (PDT)
To: www-lib <www-lib@w3.org>
Message-ID: <Pine.LNX.4.21.0009031350060.12858-200000@phong.blorf.net>
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.)


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

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:33:53 UTC