]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
url: make Curl_close() NULLify the pointer too
authorDaniel Stenberg <daniel@haxx.se>
Mon, 28 Oct 2019 08:28:05 +0000 (09:28 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 30 Oct 2019 08:36:21 +0000 (09:36 +0100)
This is the common pattern used in the code and by a unified approach we
avoid mistakes.

Closes #4534

lib/conncache.c
lib/doh.c
lib/easy.c
lib/http2.c
lib/url.c
lib/url.h
tests/unit/unit1620.c

index 2f4dd4bc3e0b29a0abe9a7a60e31a6e044be1235..57d6061fdae3721cc402f783888187c902cc9ba4 100644 (file)
@@ -143,10 +143,8 @@ int Curl_conncache_init(struct conncache *connc, int size)
 
   rc = Curl_hash_init(&connc->hash, size, Curl_hash_str,
                       Curl_str_key_compare, free_bundle_hash_entry);
-  if(rc) {
-    Curl_close(connc->closure_handle);
-    connc->closure_handle = NULL;
-  }
+  if(rc)
+    Curl_close(&connc->closure_handle);
   else
     connc->closure_handle->state.conn_cache = connc;
 
@@ -595,7 +593,7 @@ void Curl_conncache_close_all_connections(struct conncache *connc)
 
     Curl_hostcache_clean(connc->closure_handle,
                          connc->closure_handle->dns.hostcache);
-    Curl_close(connc->closure_handle);
+    Curl_close(&connc->closure_handle);
     sigpipe_restore(&pipe_st);
   }
 }
index 196e89d93dfc23d6f6770e1b09fd36452910951b..d1795789e5e6127d92a4ce53296d21c522b10e7e 100644 (file)
--- a/lib/doh.c
+++ b/lib/doh.c
@@ -346,7 +346,7 @@ static CURLcode dohprobe(struct Curl_easy *data,
 
   error:
   free(nurl);
-  Curl_close(doh);
+  Curl_close(&doh);
   return result;
 }
 
@@ -402,10 +402,8 @@ Curl_addrinfo *Curl_doh(struct connectdata *conn,
   error:
   curl_slist_free_all(data->req.doh.headers);
   data->req.doh.headers = NULL;
-  Curl_close(data->req.doh.probe[0].easy);
-  data->req.doh.probe[0].easy = NULL;
-  Curl_close(data->req.doh.probe[1].easy);
-  data->req.doh.probe[1].easy = NULL;
+  Curl_close(&data->req.doh.probe[0].easy);
+  Curl_close(&data->req.doh.probe[1].easy);
   return NULL;
 }
 
@@ -925,11 +923,9 @@ CURLcode Curl_doh_is_resolved(struct connectdata *conn,
     struct dohentry de;
     /* remove DOH handles from multi handle and close them */
     curl_multi_remove_handle(data->multi, data->req.doh.probe[0].easy);
-    Curl_close(data->req.doh.probe[0].easy);
-    data->req.doh.probe[0].easy = NULL;
+    Curl_close(&data->req.doh.probe[0].easy);
     curl_multi_remove_handle(data->multi, data->req.doh.probe[1].easy);
-    Curl_close(data->req.doh.probe[1].easy);
-    data->req.doh.probe[1].easy = NULL;
+    Curl_close(&data->req.doh.probe[1].easy);
     /* parse the responses, create the struct and return it! */
     init_dohentry(&de);
     rc = doh_decode(data->req.doh.probe[0].serverdoh.memory,
index cbb65a5f61560cb75ab6b5a75c5509ca2cd44a5f..001648d49bb56692a9479f556057afe51e60a0e5 100644 (file)
@@ -731,7 +731,7 @@ void curl_easy_cleanup(struct Curl_easy *data)
     return;
 
   sigpipe_ignore(data, &pipe_st);
-  Curl_close(data);
+  Curl_close(&data);
   sigpipe_restore(&pipe_st);
 }
 
index bae9388118fdfb6c5e5b7de29c60d3b9be83f2e2..6315fc401415ea77ea46c454f80f66f34555bc78 100644 (file)
@@ -496,16 +496,14 @@ static struct Curl_easy *duphandle(struct Curl_easy *data)
     /* setup the request struct */
     struct HTTP *http = calloc(1, sizeof(struct HTTP));
     if(!http) {
-      (void)Curl_close(second);
-      second = NULL;
+      (void)Curl_close(&second);
     }
     else {
       second->req.protop = http;
       http->header_recvbuf = Curl_add_buffer_init();
       if(!http->header_recvbuf) {
         free(http);
-        (void)Curl_close(second);
-        second = NULL;
+        (void)Curl_close(&second);
       }
       else {
         Curl_http2_setup_req(second);
@@ -547,7 +545,7 @@ static int push_promise(struct Curl_easy *data,
     stream = data->req.protop;
     if(!stream) {
       failf(data, "Internal NULL stream!\n");
-      (void)Curl_close(newhandle);
+      (void)Curl_close(&newhandle);
       rv = 1;
       goto fail;
     }
@@ -569,7 +567,7 @@ static int push_promise(struct Curl_easy *data,
       /* denied, kill off the new handle again */
       http2_stream_free(newhandle->req.protop);
       newhandle->req.protop = NULL;
-      (void)Curl_close(newhandle);
+      (void)Curl_close(&newhandle);
       goto fail;
     }
 
@@ -585,7 +583,7 @@ static int push_promise(struct Curl_easy *data,
       infof(data, "failed to add handle to multi\n");
       http2_stream_free(newhandle->req.protop);
       newhandle->req.protop = NULL;
-      Curl_close(newhandle);
+      Curl_close(&newhandle);
       rv = 1;
       goto fail;
     }
index bbae273fdbe472d5ec22f2ced901d450f4ee1f64..8285474fd724af8d78f2d6bb44931d76598ffcf2 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -317,13 +317,17 @@ static void up_free(struct Curl_easy *data)
  * when curl_easy_perform() is invoked.
  */
 
-CURLcode Curl_close(struct Curl_easy *data)
+CURLcode Curl_close(struct Curl_easy **datap)
 {
   struct Curl_multi *m;
+  struct Curl_easy *data;
 
-  if(!data)
+  if(!datap || !*datap)
     return CURLE_OK;
 
+  data = *datap;
+  *datap = NULL;
+
   Curl_expire_clear(data); /* shut off timers */
 
   m = data->multi;
@@ -1983,10 +1987,8 @@ void Curl_free_request_state(struct Curl_easy *data)
 {
   Curl_safefree(data->req.protop);
   Curl_safefree(data->req.newurl);
-  Curl_close(data->req.doh.probe[0].easy);
-  data->req.doh.probe[0].easy = NULL;
-  Curl_close(data->req.doh.probe[1].easy);
-  data->req.doh.probe[1].easy = NULL;
+  Curl_close(&data->req.doh.probe[0].easy);
+  Curl_close(&data->req.doh.probe[1].easy);
 }
 
 
index f4d611adda4d358f926eb131597bd793f780be9c..053fbdffc21d1aa59fc7e719150871d8f1d46ded 100644 (file)
--- a/lib/url.h
+++ b/lib/url.h
@@ -49,7 +49,7 @@ CURLcode Curl_init_userdefined(struct Curl_easy *data);
 
 void Curl_freeset(struct Curl_easy * data);
 CURLcode Curl_uc_to_curlcode(CURLUcode uc);
-CURLcode Curl_close(struct Curl_easy *data); /* opposite of curl_open() */
+CURLcode Curl_close(struct Curl_easy **datap); /* opposite of curl_open() */
 CURLcode Curl_connect(struct Curl_easy *, bool *async, bool *protocol_connect);
 CURLcode Curl_disconnect(struct Curl_easy *data,
                          struct connectdata *, bool dead_connection);
index b8b09652149e554bd0f80643f3bda8e14cb2bcf5..c6aa721cf21b2b9cd8f220eb21831f3b99cb42e7 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2019, 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
@@ -83,7 +83,7 @@ UNITTEST_START
 
   Curl_free_request_state(empty);
 
-  rc = Curl_close(empty);
+  rc = Curl_close(&empty);
   fail_unless(rc == CURLE_OK, "Curl_close() failed");
 
 }