Browse code

hashtab_insert should return pointer to newly inserted element. Fix grow bug.

git-svn: trunk@3891

Török Edvin authored on 2008/06/17 06:21:11
Showing 3 changed files
... ...
@@ -1,3 +1,8 @@
1
+Mon Jun 16 23:43:31 EEST 2008 (edwin)
2
+-------------------------------------
3
+  * libclamav/hashtab.[ch]: hashtab_insert should return pointer to newly
4
+  inserted element. Fix grow bug.
5
+
1 6
 Fri Jun 13 13:10:15 CEST 2008 (tk)
2 7
 ----------------------------------
3 8
   * libclamav/matcher-ac.c: fix handling of nodes which also match single
... ...
@@ -243,9 +243,10 @@ struct element* hashtab_find(const struct hashtable *s,const char* key,const siz
243 243
 
244 244
 static int hashtab_grow(struct hashtable *s)
245 245
 {
246
-	const size_t new_capacity = nearest_power(s->capacity);
246
+	const size_t new_capacity = nearest_power(s->capacity + 1);
247 247
 	struct element* htable = cli_calloc(new_capacity, sizeof(*s->htable));
248 248
 	size_t i,idx, used = 0;
249
+	cli_dbgmsg("hashtab.c: new capacity: %lu\n",new_capacity);
249 250
 	if(new_capacity == s->capacity || !htable)
250 251
 		return CL_EMEM;
251 252
 
... ...
@@ -286,14 +287,18 @@ static int hashtab_grow(struct hashtable *s)
286 286
 	return CL_SUCCESS;
287 287
 }
288 288
 
289
-int hashtab_insert(struct hashtable *s, const char* key, const size_t len, const element_data data)
289
+const struct element* hashtab_insert(struct hashtable *s, const char* key, const size_t len, const element_data data)
290 290
 {
291 291
 	struct element* element;
292 292
 	struct element* deleted_element = NULL;
293 293
 	size_t tries = 1;
294 294
 	size_t idx;
295 295
 	if(!s)
296
-		return CL_ENULLARG;
296
+		return NULL;
297
+	if(s->used > s->maxfill) {
298
+		cli_dbgmsg("hashtab.c:Growing hashtable %p, because it has exceeded maxfill, old size:%ld\n",(void*)s,s->capacity);
299
+		hashtab_grow(s);
300
+	}
297 301
 	do {
298 302
 		PROFILE_CALC_HASH(s);
299 303
 		idx = hash((const unsigned char*)key, len, s->capacity);
... ...
@@ -313,18 +318,14 @@ int hashtab_insert(struct hashtable *s, const char* key, const size_t len, const
313 313
 				}
314 314
 				thekey = cli_malloc(len+1);
315 315
 				if(!thekey)
316
-					return CL_EMEM;
316
+					return NULL;
317 317
 				strncpy(thekey, key, len+1);
318 318
 				thekey[len]='\0';
319 319
 				element->key = thekey;
320 320
 				element->data = data;
321 321
 				element->len = len;
322 322
 				s->used++;
323
-				if(s->used > s->maxfill) {
324
-					cli_dbgmsg("hashtab.c:Growing hashtable %p, because it has exceeded maxfill, old size:%ld\n",(void*)s,s->capacity);
325
-					hashtab_grow(s);
326
-				}
327
-				return 0;
323
+				return element;
328 324
 			}
329 325
 			else if(element->key == DELETED_KEY) {
330 326
 				deleted_element = element;
... ...
@@ -332,7 +333,7 @@ int hashtab_insert(struct hashtable *s, const char* key, const size_t len, const
332 332
 			else if(len == element->len && strncmp(key, element->key, len)==0) {
333 333
 				PROFILE_DATA_UPDATE(s, tries);
334 334
 				element->data = data;/* key found, update */
335
-				return 0;
335
+				return element;
336 336
 			}
337 337
 			else {
338 338
 				idx = (idx + tries++) % s->capacity;
... ...
@@ -344,7 +345,7 @@ int hashtab_insert(struct hashtable *s, const char* key, const size_t len, const
344 344
 		cli_dbgmsg("hashtab.c: Growing hashtable %p, because its full, old size:%ld.\n",(void*)s,s->capacity);
345 345
 	} while( hashtab_grow(s) >= 0 );
346 346
 	cli_warnmsg("hashtab.c: Unable to grow hashtable\n");
347
-	return CL_EMEM;
347
+	return NULL;
348 348
 }
349 349
 
350 350
 void hashtab_delete(struct hashtable *s, const char* key, const size_t len)
... ...
@@ -79,7 +79,7 @@ struct hashtable {
79 79
 int hashtab_generate_c(const struct hashtable *s,const char* name);
80 80
 struct element* hashtab_find(const struct hashtable *s, const char* key, const size_t len);
81 81
 int hashtab_init(struct hashtable *s,size_t capacity);
82
-int hashtab_insert(struct hashtable *s, const char* key, const size_t len, const element_data data);
82
+const struct element* hashtab_insert(struct hashtable *s, const char* key, const size_t len, const element_data data);
83 83
 void hashtab_delete(struct hashtable *s,const char* key,const size_t len);
84 84
 void hashtab_clear(struct hashtable *s);
85 85