]> granicus.if.org Git - curl/commitdiff
hostip: fix unintended destruction of hash table
authorAnthony Avina <aavina2@gmail.com>
Sat, 2 May 2015 18:49:55 +0000 (13:49 -0500)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 18 May 2015 09:15:43 +0000 (11:15 +0200)
.. and added unit1602 for hash.c

lib/conncache.c
lib/hash.c
lib/hash.h
lib/hostip.c
lib/multi.c
lib/share.c
tests/data/Makefile.inc
tests/data/test1602 [new file with mode: 0644]
tests/unit/Makefile.inc
tests/unit/unit1305.c
tests/unit/unit1602.c [new file with mode: 0644]

index 634b673e316dfd30fdd7c09e61739e8b55d4f3d4..5c3957519237aed4a0cc585e033251b679593dd5 100644 (file)
@@ -126,7 +126,7 @@ int Curl_conncache_init(struct conncache *connc, int size)
 void Curl_conncache_destroy(struct conncache *connc)
 {
   if(connc)
-    Curl_hash_clean(&connc->hash);
+    Curl_hash_destroy(&connc->hash);
 }
 
 /* returns an allocated key to find a bundle for this connection */
index b1aebd5b839d0eb0704506f1ae5db935557cf535..c46760ae1f69290715d9233e41648d7482cae9e2 100644 (file)
@@ -212,8 +212,11 @@ Curl_hash_apply(curl_hash *h, void *user,
 }
 #endif
 
+/* Destroys all the entries in the given hash and resets its attributes,
+ * prepping the given hash for [static|dynamic] deallocation.
+ */
 void
-Curl_hash_clean(struct curl_hash *h)
+Curl_hash_destroy(struct curl_hash *h)
 {
   int i;
 
@@ -227,6 +230,17 @@ Curl_hash_clean(struct curl_hash *h)
   h->slots = 0;
 }
 
+/* Removes all the entries in the given hash.
+ *
+ * @unittest: 1602
+ */
+void
+Curl_hash_clean(struct curl_hash *h)
+{
+  Curl_hash_clean_with_criterium(h, NULL, NULL);
+}
+
+/* Cleans all entries that pass the comp function criteria. */
 void
 Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
                                int (*comp)(void *, void *))
@@ -246,7 +260,7 @@ Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
       struct curl_hash_element *he = le->ptr;
       lnext = le->next;
       /* ask the callback function if we shall remove this entry or not */
-      if(comp(user, he->ptr)) {
+      if(comp == NULL || comp(user, he->ptr)) {
         Curl_llist_remove(list, le, (void *) h);
         --h->size; /* one less entry in the hash now */
       }
index bc7c20dece1e824a626b3052aef483a7fdbdbac7..b13a236bb1ecbab1477955b496b25e80d6c4c5e5 100644 (file)
@@ -80,6 +80,7 @@ void *Curl_hash_pick(struct curl_hash *, void * key, size_t key_len);
 void Curl_hash_apply(struct curl_hash *h, void *user,
                      void (*cb)(void *user, void *ptr));
 int Curl_hash_count(struct curl_hash *h);
+void Curl_hash_destroy(struct curl_hash *h);
 void Curl_hash_clean(struct curl_hash *h);
 void Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
                                     int (*comp)(void *, void *));
index 338cf2de66d1d32330850aa701721fa57e0d854d..82f3897f9c7cb5e2c4abb12f17196715f0007300 100644 (file)
@@ -137,7 +137,7 @@ struct curl_hash *Curl_global_host_cache_init(void)
 void Curl_global_host_cache_dtor(void)
 {
   if(host_cache_initialized) {
-    Curl_hash_clean(&hostname_cache);
+    Curl_hash_destroy(&hostname_cache);
     host_cache_initialized = 0;
   }
 }
index 718f6581698b8156f60c43e64b90e1cfae912f22..2a7d8bede709d0f8e974dd1c94b92557b8362ad5 100644 (file)
@@ -324,8 +324,8 @@ struct Curl_multi *Curl_multi_handle(int hashsize, /* socket hash */
 
   error:
 
-  Curl_hash_clean(&multi->sockhash);
-  Curl_hash_clean(&multi->hostcache);
+  Curl_hash_destroy(&multi->sockhash);
+  Curl_hash_destroy(&multi->hostcache);
   Curl_conncache_destroy(&multi->conn_cache);
   Curl_close(multi->closure_handle);
   multi->closure_handle = NULL;
@@ -1874,7 +1874,7 @@ CURLMcode curl_multi_cleanup(CURLM *multi_handle)
       Curl_close(multi->closure_handle);
     }
 
-    Curl_hash_clean(&multi->sockhash);
+    Curl_hash_destroy(&multi->sockhash);
     Curl_conncache_destroy(&multi->conn_cache);
     Curl_llist_destroy(multi->msglist, NULL);
     Curl_llist_destroy(multi->pending, NULL);
@@ -1897,7 +1897,7 @@ CURLMcode curl_multi_cleanup(CURLM *multi_handle)
       data = nextdata;
     }
 
-    Curl_hash_clean(&multi->hostcache);
+    Curl_hash_destroy(&multi->hostcache);
 
     /* Free the blacklists by setting them to NULL */
     Curl_pipeline_set_site_blacklist(NULL, &multi->pipelining_site_bl);
index b61a86bc25a109ad60a97c8639e30f116d992dd4..7fb068625872175e3dc72bf72c2a4d547be14390 100644 (file)
@@ -188,7 +188,7 @@ curl_share_cleanup(CURLSH *sh)
     return CURLSHE_IN_USE;
   }
 
-  Curl_hash_clean(&share->hostcache);
+  Curl_hash_destroy(&share->hostcache);
 
 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
   Curl_cookie_cleanup(share->cookies);
index 57a4e79dcd7d9a33f3dcf6c60fb93ae6c4b31ee4..a15ff8afbc6508b433757b33543adc372e95181e 100644 (file)
@@ -155,7 +155,7 @@ test1520 \
 \
 test1525 test1526 test1527 test1528 test1529 \
 \
-test1600 test1601 \
+test1600 test1601 test1602 \
 \
 test1800 test1801 \
 \
diff --git a/tests/data/test1602 b/tests/data/test1602
new file mode 100644 (file)
index 0000000..4717058
--- /dev/null
@@ -0,0 +1,26 @@
+<testcase>
+<info>
+<keywords>
+unittest
+hash
+</keywords>
+</info>
+
+#
+# Client-side
+<client>
+<server>
+none
+</server>
+<features>
+unittest
+</features>
+ <name>
+Internal hash create/add/destroy testing, exercising clean functions
+ </name>
+<tool>
+unit1602
+</tool>
+</client>
+
+</testcase>
index d68c0370147730ea6605d3bfd678f5ee65c9e2e1..9073b34e635e556b3048f834f555cc6796a95d7b 100644 (file)
@@ -7,7 +7,7 @@ UNITFILES = curlcheck.h \
 # These are all unit test programs
 UNITPROGS = unit1300 unit1301 unit1302 unit1303 unit1304 unit1305 unit1307     \
  unit1308 unit1309 unit1330 unit1394 unit1395 unit1396 unit1397 unit1398       \
- unit1600 unit1601
+ unit1600 unit1601 unit1602
 
 unit1300_SOURCES = unit1300.c $(UNITFILES)
 unit1300_CPPFLAGS = $(AM_CPPFLAGS)
@@ -63,3 +63,6 @@ unit1600_CPPFLAGS = $(AM_CPPFLAGS)
 unit1601_SOURCES = unit1601.c $(UNITFILES)
 unit1601_CPPFLAGS = $(AM_CPPFLAGS)
 
+unit1602_SOURCES = unit1602.c $(UNITFILES)
+unit1602_CPPFLAGS = $(AM_CPPFLAGS)
+
index b4e83776276f0d7ed95c8b5e274e3f0def874ab5..96913f1fa421c07425514f6c9327f0edfb7dfad0 100644 (file)
@@ -67,7 +67,7 @@ static void unit_stop( void )
     free(data_node);
   }
   free(data_key);
-  Curl_hash_clean(&hp);
+  Curl_hash_destroy(&hp);
 
   curl_easy_cleanup(data);
   curl_global_cleanup();
diff --git a/tests/unit/unit1602.c b/tests/unit/unit1602.c
new file mode 100644 (file)
index 0000000..3b25556
--- /dev/null
@@ -0,0 +1,80 @@
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include "curlcheck.h"
+
+#define ENABLE_CURLX_PRINTF
+#include "curlx.h"
+
+#include "hash.h"
+
+#include "memdebug.h" /* LAST include file */
+
+ static CURLcode unit_setup( void )
+{
+  return CURLE_OK;
+}
+
+static void unit_stop( void )
+{
+
+}
+
+static void mydtor(void *p)
+{
+  int *ptr = (int*)p;
+  free(ptr);
+}
+
+UNITTEST_START
+  int *value;
+  int *value2;
+  size_t klen = sizeof(int);
+
+  struct curl_hash hash_static;
+  int key = 20;
+  int key2 = 25;
+  int rc = 0;
+
+  rc = Curl_hash_init(&hash_static, 7, Curl_hash_str,
+                        Curl_str_key_compare, mydtor);
+
+  if(rc)
+  {
+    fail("Curl_hash_init failed to initialize static hash!");
+    goto unit_test_abort;
+  }
+
+  value = malloc(sizeof(int));
+  value2 = malloc(sizeof(int));
+
+  *value = 199;
+  *value2 = 204;
+  Curl_hash_add(&hash_static, &key, klen, value);
+  
+  Curl_hash_clean(&hash_static);
+
+  /* Attempt to add another key/value pair */
+  Curl_hash_add(&hash_static, &key2, klen, value2);
+
+  Curl_hash_destroy(&hash_static);
+  
+UNITTEST_STOP