Check-in [cc9ff9d9e4]

Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview

SHA1 Hash:cc9ff9d9e407489f2f9b1ae976c352e37de3714d
Date: 2008-11-03 23:33:26
User: stephan
Comment:fixed a div-by-0 which happend in some search cases

Tags And Properties
Changes
[hide diffs]

Changes to src/whhash.c

@@ -270,11 +270,13 @@
 }
 
 int
 whhash_replace(whhash_table *h, void *k, void *v)
 {
-    if( !h || !k ) return 0;
+    if( !h || !k
+	|| !h->tablelength /* avoid a possible /-by-0 in whhash_index()*/
+	) return 0;
     whhash_entry *e;
     whhash_val_t hashvalue, index;
     hashvalue = whhash_hash(h,k);
     index = whhash_index(h->tablelength,hashvalue);
     e = h->table[index];
@@ -299,11 +301,13 @@
 }
 
 int
 whhash_insert(whhash_table *h, void *k, void *v)
 {
-    if( ! h || !k ) return 0;
+    if( ! h || !k
+	|| !h->tablelength /* avoid a possible /-by-0 in whhash_index()*/
+	) return 0;
 #if 0
     /* Stephan Beal, 13 Feb 2008: now simply replaces the value of existing entries.
 
     A week or three later: profiling has shown that this nearly doubles the insertion
     time. One app was spending almost 1% of its time in this code.
@@ -335,11 +339,13 @@
 }
 
 static whhash_entry * whhash_search_entry(whhash_table *h,
 					  void const *k)
 {
-    if( !h || !k ) return 0;
+    if( !h || !k
+	|| !h->tablelength /* avoid a possible /-by-0 in whhash_index()*/
+	) return 0;
     ++h->stats.searches;
     whhash_entry * e = 0;
     whhash_val_t hashvalue, index;
     hashvalue = whhash_hash(h,k);
     index = whhash_index(h->tablelength,hashvalue);
@@ -371,10 +377,14 @@
     return e ? e->v : 0;
 }
 
 void * whhash_take(whhash_table *h, void const *k)
 {
+    if( !h || !k
+	|| !h->tablelength /* avoid a possible /-by-0 in whhash_index()*/
+	) return 0;
+
     /* TODO: consider compacting the table when the load factor drops enough,
      *       or provide a 'compact' method. */
     whhash_entry *e;
     whhash_entry **pE;
     void *v;
@@ -781,11 +791,13 @@
 
 int
 whhash_iter_search(whhash_iter *itr,
 		   void *k)
 {
-    if( ! itr  || !itr->h || !k ) return 0;
+    if( ! itr  || !itr->h || !k
+	|| !itr->h->tablelength /* avoid a possible /-by-0 in whhash_index()*/
+	) return 0;
     whhash_entry *e, *parent;
     unsigned int hashvalue, index;
     whhash_table *h = itr->h;
     hashvalue = whhash_hash(h,k);
     index = whhash_index(h->tablelength,hashvalue);