From 12f375f3b186a6abd38874a96ec0f80fc8e7805b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Thu, 5 Aug 2010 21:08:23 +0000 Subject: array.c: improve array_get_unused_element to check data type; fix mem leak if unused_element didn't find a matching entry (fixes #2145) - the "mem leak" could only be triggered if you use different entry types in the same array (this wasn't supported by array_get_unused_element) or didn't call array_get_unused_element before creating new entries. git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2750 152afb58-edef-0310-8abb-c4023f1b3aa9 --- src/array.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'src/array.c') diff --git a/src/array.c b/src/array.c index e93906b1..12437d4f 100644 --- a/src/array.c +++ b/src/array.c @@ -130,20 +130,21 @@ data_unset *array_get_element(array *a, const char *key) { data_unset *array_get_unused_element(array *a, data_type_t t) { data_unset *ds = NULL; + unsigned int i; - UNUSED(t); + for (i = a->used; i < a->size; i++) { + if (a->data[i] && a->data[i]->type == t) { + ds = a->data[i]; - if (a->size == 0) return NULL; + /* make empty slot at a->used for next insert */ + a->data[i] = a->data[a->used]; + a->data[a->used] = NULL; - if (a->used == a->size) return NULL; - - if (a->data[a->used]) { - ds = a->data[a->used]; - - a->data[a->used] = NULL; + return ds; + } } - return ds; + return NULL; } void array_set_key_value(array *hdrs, const char *key, size_t key_len, const char *value, size_t val_len) { @@ -224,6 +225,9 @@ int array_insert_unique(array *a, data_unset *str) { ndx = (int) a->used; + /* make sure there is nothing here */ + if (a->data[ndx]) a->data[ndx]->free(a->data[ndx]); + a->data[a->used++] = str; if (pos != ndx && -- cgit v1.2.1