OOM handling/cleanup slight adjustments
authorYang Tse <yangsita@gmail.com>
Tue, 11 Oct 2011 17:41:30 +0000 (19:41 +0200)
committerYang Tse <yangsita@gmail.com>
Tue, 11 Oct 2011 17:41:30 +0000 (19:41 +0200)
lib/cookie.c
lib/fileinfo.c
lib/hash.c
lib/llist.c
lib/multi.c
lib/ssluse.c
lib/url.c

index fc684ca..41ccdbe 100644 (file)
@@ -144,9 +144,9 @@ void Curl_cookie_loadfiles(struct SessionHandle *data)
                                        data->set.cookiesession);
       list = list->next;
     }
-    Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
     curl_slist_free_all(data->change.cookielist); /* clean up list */
     data->change.cookielist = NULL; /* don't do this again! */
+    Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
   }
 }
 
index 1616668..4ffcbbe 100644 (file)
@@ -48,8 +48,7 @@ void Curl_fileinfo_dtor(void *user, void *element)
   if(!finfo)
     return;
 
-  if(finfo->b_data)
-    free(finfo->b_data);
+  Curl_safefree(finfo->b_data);
 
   free(finfo);
 }
index 15b3eff..3704eea 100644 (file)
@@ -38,11 +38,14 @@ hash_element_dtor(void *user, void *element)
   struct curl_hash *h = (struct curl_hash *) user;
   struct curl_hash_element *e = (struct curl_hash_element *) element;
 
-  if(e->key)
-    free(e->key);
+  Curl_safefree(e->key);
 
-  if(e->ptr)
+  if(e->ptr) {
     h->dtor(e->ptr);
+    e->ptr = NULL;
+  }
+
+  e->key_len = 0;
 
   free(e);
 }
@@ -78,13 +81,16 @@ Curl_hash_init(struct curl_hash *h,
         }
         free(h->table);
         h->table = NULL;
+        h->slots = 0;
         return 1; /* failure */
       }
     }
     return 0; /* fine */
   }
-  else
+  else {
+    h->slots = 0;
     return 1; /* failure */
+  }
 }
 
 struct curl_hash *
@@ -190,6 +196,7 @@ int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
     he = le->ptr;
     if(h->comp_func(he->key, he->key_len, key, key_len)) {
       Curl_llist_remove(l, le, (void *) h);
+      --h->size;
       return 0;
     }
   }
@@ -244,6 +251,8 @@ Curl_hash_clean(struct curl_hash *h)
 
   free(h->table);
   h->table = NULL;
+  h->size = 0;
+  h->slots = 0;
 }
 
 void
index 0aecf10..a302e32 100644 (file)
@@ -46,7 +46,7 @@ Curl_llist_alloc(curl_llist_dtor dtor)
   struct curl_llist *list;
 
   list = malloc(sizeof(struct curl_llist));
-  if(NULL == list)
+  if(!list)
     return NULL;
 
   llist_init(list, dtor);
index 7786ccc..8a8779c 100644 (file)
@@ -425,12 +425,13 @@ CURLM *curl_multi_init(void)
   return (CURLM *) multi;
 
   error:
-  if(multi->sockhash)
-    Curl_hash_destroy(multi->sockhash);
-  if(multi->hostcache)
-    Curl_hash_destroy(multi->hostcache);
-  if(multi->connc)
-    Curl_rm_connc(multi->connc);
+
+  Curl_hash_destroy(multi->sockhash);
+  multi->sockhash = NULL;
+  Curl_hash_destroy(multi->hostcache);
+  multi->hostcache = NULL;
+  Curl_rm_connc(multi->connc);
+  multi->connc = NULL;
 
   free(multi);
   return NULL;
@@ -1801,6 +1802,7 @@ CURLMcode curl_multi_cleanup(CURLM *multi_handle)
     }
 
     Curl_rm_connc(multi->connc);
+    multi->connc = NULL;
 
     /* remove the pending list of messages */
     Curl_llist_destroy(multi->msglist, NULL);
index 51bd909..d65fd98 100644 (file)
@@ -1855,6 +1855,7 @@ static CURLcode push_certinfo_len(struct SessionHandle *data,
      equivalent of curl_slist_append but doesn't strdup() the given data as
      like in this place the extra malloc/free is totally pointless */
   nl = curl_slist_append(ci->certinfo[certnum], output);
+  free(output);
   if(!nl) {
     curl_slist_free_all(ci->certinfo[certnum]);
     ci->certinfo[certnum] = NULL;
@@ -1863,8 +1864,6 @@ static CURLcode push_certinfo_len(struct SessionHandle *data,
   else
     ci->certinfo[certnum] = nl;
 
-  free(output);
-
   return res;
 }
 
index 7813b82..7eb59f5 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -458,6 +458,7 @@ CURLcode Curl_close(struct SessionHandle *data)
 
       /* free the connection cache if allocated privately */
       Curl_rm_connc(data->state.connc);
+      data->state.connc = NULL;
     }
   }
 
@@ -618,13 +619,19 @@ CURLcode Curl_ch_connc(struct SessionHandle *data,
    curl_multi_cleanup(). */
 void Curl_rm_connc(struct conncache *c)
 {
+  if(!c)
+    return;
+
   if(c->connects) {
     long i;
-    for(i = 0; i < c->num; ++i)
+    for(i = 0; i < c->num; ++i) {
       conn_free(c->connects[i]);
-
+      c->connects[i] = NULL;
+    }
     free(c->connects);
+    c->connects = NULL;
   }
+  c->num = 0;
 
   free(c);
 }
@@ -1258,10 +1265,11 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
       /* append the cookie file name to the list of file names, and deal with
          them later */
       cl = curl_slist_append(data->change.cookielist, argptr);
-
-      if(!cl)
+      if(!cl) {
+        curl_slist_free_all(data->change.cookielist);
+        data->change.cookielist = NULL;
         return CURLE_OUT_OF_MEMORY;
-
+      }
       data->change.cookielist = cl; /* store the list for later use */
     }
     break;