[FFmpeg-devel] [PATCH] avformat: Replace ffurl_close() by ffurl_closep() where appropriate

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Mon May 25 09:27:44 EEST 2020


It avoids leaving dangling pointers behind in memory.

Also remove redundant checks for whether the URLContext to be closed is
already NULL.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavformat/async.c               |  4 ++--
 libavformat/cache.c               |  2 +-
 libavformat/concat.c              |  2 +-
 libavformat/crypto.c              |  3 +--
 libavformat/gopher.c              |  5 +----
 libavformat/hlsproto.c            |  5 ++---
 libavformat/icecast.c             |  3 +--
 libavformat/mmsh.c                |  5 ++---
 libavformat/mmst.c                |  2 +-
 libavformat/rtmpcrypt.c           |  2 +-
 libavformat/rtmphttp.c            |  2 +-
 libavformat/rtmpproto.c           |  5 ++---
 libavformat/rtpproto.c            | 10 ++++------
 libavformat/rtsp.c                | 17 +++++++----------
 libavformat/sapdec.c              |  3 +--
 libavformat/sapenc.c              |  3 +--
 libavformat/smoothstreamingenc.c  | 19 ++++++-------------
 libavformat/srtpproto.c           |  3 +--
 libavformat/subfile.c             |  4 ++--
 libavformat/tls_gnutls.c          |  3 +--
 libavformat/tls_libtls.c          |  3 +--
 libavformat/tls_openssl.c         |  3 +--
 libavformat/tls_schannel.c        |  3 +--
 libavformat/tls_securetransport.c |  3 +--
 24 files changed, 43 insertions(+), 71 deletions(-)

diff --git a/libavformat/async.c b/libavformat/async.c
index 4e295b5e10..a0bdfa2ee3 100644
--- a/libavformat/async.c
+++ b/libavformat/async.c
@@ -293,7 +293,7 @@ cond_wakeup_background_fail:
 cond_wakeup_main_fail:
     pthread_mutex_destroy(&c->mutex);
 mutex_fail:
-    ffurl_close(c->inner);
+    ffurl_closep(&c->inner);
 url_fail:
     ring_destroy(&c->ring);
 fifo_fail:
@@ -317,7 +317,7 @@ static int async_close(URLContext *h)
     pthread_cond_destroy(&c->cond_wakeup_background);
     pthread_cond_destroy(&c->cond_wakeup_main);
     pthread_mutex_destroy(&c->mutex);
-    ffurl_close(c->inner);
+    ffurl_closep(&c->inner);
     ring_destroy(&c->ring);
 
     return 0;
diff --git a/libavformat/cache.c b/libavformat/cache.c
index 09e5d5f832..1e19dafc6a 100644
--- a/libavformat/cache.c
+++ b/libavformat/cache.c
@@ -310,7 +310,7 @@ static int cache_close(URLContext *h)
             av_log(h, AV_LOG_ERROR, "Could not delete %s.\n", c->filename);
         av_freep(&c->filename);
     }
-    ffurl_close(c->inner);
+    ffurl_closep(&c->inner);
     av_tree_enumerate(c->root, NULL, NULL, enu_free);
     av_tree_destroy(c->root);
 
diff --git a/libavformat/concat.c b/libavformat/concat.c
index cfe14760eb..418405dd50 100644
--- a/libavformat/concat.c
+++ b/libavformat/concat.c
@@ -49,7 +49,7 @@ static av_cold int concat_close(URLContext *h)
     struct concat_nodes *nodes = data->nodes;
 
     for (i = 0; i != data->length; i++)
-        err |= ffurl_close(nodes[i].uc);
+        err |= ffurl_closep(&nodes[i].uc);
 
     av_freep(&data->nodes);
 
diff --git a/libavformat/crypto.c b/libavformat/crypto.c
index 9a48f2e6f5..31f9ac0ab9 100644
--- a/libavformat/crypto.c
+++ b/libavformat/crypto.c
@@ -385,8 +385,7 @@ static int crypto_close(URLContext *h)
         ret = ffurl_write(c->hd, out_buf, BLOCKSIZE);
     }
 
-    if (c->hd)
-        ffurl_close(c->hd);
+    ffurl_closep(&c->hd);
     av_freep(&c->aes_decrypt);
     av_freep(&c->aes_encrypt);
     av_freep(&c->write_buf);
diff --git a/libavformat/gopher.c b/libavformat/gopher.c
index 3070b24caf..8b6d14a1f7 100644
--- a/libavformat/gopher.c
+++ b/libavformat/gopher.c
@@ -68,10 +68,7 @@ static int gopher_connect(URLContext *h, const char *path)
 static int gopher_close(URLContext *h)
 {
     GopherContext *s = h->priv_data;
-    if (s->hd) {
-        ffurl_close(s->hd);
-        s->hd = NULL;
-    }
+    ffurl_closep(&s->hd);
     return 0;
 }
 
diff --git a/libavformat/hlsproto.c b/libavformat/hlsproto.c
index e5673e5e03..de45f771d6 100644
--- a/libavformat/hlsproto.c
+++ b/libavformat/hlsproto.c
@@ -178,7 +178,7 @@ static int hls_close(URLContext *h)
 
     free_segment_list(s);
     free_variant_list(s);
-    ffurl_close(s->seg_hd);
+    ffurl_closep(&s->seg_hd);
     return 0;
 }
 
@@ -260,8 +260,7 @@ start:
             return ret;
     }
     if (s->seg_hd) {
-        ffurl_close(s->seg_hd);
-        s->seg_hd = NULL;
+        ffurl_closep(&s->seg_hd);
         s->cur_seq_no++;
     }
     reload_interval = s->n_segments > 0 ?
diff --git a/libavformat/icecast.c b/libavformat/icecast.c
index 7d8f92fe73..38af16b99e 100644
--- a/libavformat/icecast.c
+++ b/libavformat/icecast.c
@@ -75,8 +75,7 @@ static void cat_header(AVBPrint *bp, const char key[], const char value[])
 static int icecast_close(URLContext *h)
 {
     IcecastContext *s = h->priv_data;
-    if (s->hd)
-        ffurl_close(s->hd);
+    ffurl_closep(&s->hd);
     return 0;
 }
 
diff --git a/libavformat/mmsh.c b/libavformat/mmsh.c
index 13c0ffe438..495d7fb73b 100644
--- a/libavformat/mmsh.c
+++ b/libavformat/mmsh.c
@@ -65,8 +65,7 @@ static int mmsh_close(URLContext *h)
 {
     MMSHContext *mmsh = (MMSHContext *)h->priv_data;
     MMSContext *mms   = &mmsh->mms;
-    if (mms->mms_hd)
-        ffurl_closep(&mms->mms_hd);
+    ffurl_closep(&mms->mms_hd);
     av_freep(&mms->streams);
     av_freep(&mms->asf_header);
     return 0;
@@ -265,7 +264,7 @@ static int mmsh_open_internal(URLContext *h, const char *uri, int flags, int tim
     }
 
     // close the socket and then reopen it for sending the second play request.
-    ffurl_close(mms->mms_hd);
+    ffurl_closep(&mms->mms_hd);
     memset(headers, 0, sizeof(headers));
     if ((err = ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ,
                            &h->interrupt_callback)) < 0) {
diff --git a/libavformat/mmst.c b/libavformat/mmst.c
index 533cbe7698..377323fe27 100644
--- a/libavformat/mmst.c
+++ b/libavformat/mmst.c
@@ -473,7 +473,7 @@ static int mms_close(URLContext *h)
     MMSContext *mms   = &mmst->mms;
     if(mms->mms_hd) {
         send_close_packet(mmst);
-        ffurl_close(mms->mms_hd);
+        ffurl_closep(&mms->mms_hd);
     }
 
     /* free all separately allocated pointers in mms */
diff --git a/libavformat/rtmpcrypt.c b/libavformat/rtmpcrypt.c
index 253b8ca2ce..a835ab263f 100644
--- a/libavformat/rtmpcrypt.c
+++ b/libavformat/rtmpcrypt.c
@@ -240,7 +240,7 @@ static int rtmpe_close(URLContext *h)
     RTMPEContext *rt = h->priv_data;
 
     ff_dh_free(rt->dh);
-    ffurl_close(rt->stream);
+    ffurl_closep(&rt->stream);
 
     return 0;
 }
diff --git a/libavformat/rtmphttp.c b/libavformat/rtmphttp.c
index ef6146ca86..c920c19ab5 100644
--- a/libavformat/rtmphttp.c
+++ b/libavformat/rtmphttp.c
@@ -176,7 +176,7 @@ static int rtmp_http_close(URLContext *h)
     }
 
     av_freep(&rt->out_data);
-    ffurl_close(rt->stream);
+    ffurl_closep(&rt->stream);
 
     return ret;
 }
diff --git a/libavformat/rtmpproto.c b/libavformat/rtmpproto.c
index e23426b770..d9741bc622 100644
--- a/libavformat/rtmpproto.c
+++ b/libavformat/rtmpproto.c
@@ -2511,7 +2511,7 @@ static int rtmp_close(URLContext *h)
 
     free_tracked_methods(rt);
     av_freep(&rt->flv_data);
-    ffurl_close(rt->stream);
+    ffurl_closep(&rt->stream);
     return ret;
 }
 
@@ -2824,8 +2824,7 @@ reconnect:
 
     if (rt->do_reconnect) {
         int i;
-        ffurl_close(rt->stream);
-        rt->stream       = NULL;
+        ffurl_closep(&rt->stream);
         rt->do_reconnect = 0;
         rt->nb_invokes   = 0;
         for (i = 0; i < 2; i++)
diff --git a/libavformat/rtpproto.c b/libavformat/rtpproto.c
index 1f0a82ac7e..ca63c165e4 100644
--- a/libavformat/rtpproto.c
+++ b/libavformat/rtpproto.c
@@ -363,10 +363,8 @@ static int rtp_open(URLContext *h, const char *uri, int flags)
     return 0;
 
  fail:
-    if (s->rtp_hd)
-        ffurl_close(s->rtp_hd);
-    if (s->rtcp_hd)
-        ffurl_close(s->rtcp_hd);
+    ffurl_closep(&s->rtp_hd);
+    ffurl_closep(&s->rtcp_hd);
     ffurl_closep(&s->fec_hd);
     av_free(fec_protocol);
     av_dict_free(&fec_opts);
@@ -506,8 +504,8 @@ static int rtp_close(URLContext *h)
 
     ff_ip_reset_filters(&s->filters);
 
-    ffurl_close(s->rtp_hd);
-    ffurl_close(s->rtcp_hd);
+    ffurl_closep(&s->rtp_hd);
+    ffurl_closep(&s->rtcp_hd);
     ffurl_closep(&s->fec_hd);
     return 0;
 }
diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index b2b3f32011..5d8491b74b 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -763,9 +763,7 @@ void ff_rtsp_undo_setup(AVFormatContext *s, int send_packets)
                 ff_rtp_parse_close(rtsp_st->transport_priv);
         }
         rtsp_st->transport_priv = NULL;
-        if (rtsp_st->rtp_handle)
-            ffurl_close(rtsp_st->rtp_handle);
-        rtsp_st->rtp_handle = NULL;
+        ffurl_closep(&rtsp_st->rtp_handle);
     }
 }
 
@@ -1666,9 +1664,10 @@ fail:
 void ff_rtsp_close_connections(AVFormatContext *s)
 {
     RTSPState *rt = s->priv_data;
-    if (rt->rtsp_hd_out != rt->rtsp_hd) ffurl_close(rt->rtsp_hd_out);
-    ffurl_close(rt->rtsp_hd);
-    rt->rtsp_hd = rt->rtsp_hd_out = NULL;
+    if (rt->rtsp_hd_out != rt->rtsp_hd)
+        ffurl_closep(&rt->rtsp_hd_out);
+    rt->rtsp_hd_out = NULL;
+    ffurl_closep(&rt->rtsp_hd);
 }
 
 int ff_rtsp_connect(AVFormatContext *s)
@@ -2496,8 +2495,7 @@ static int rtp_read_header(AVFormatContext *s)
         break;
     }
     getsockname(ffurl_get_file_handle(in), (struct sockaddr*) &addr, &addrlen);
-    ffurl_close(in);
-    in = NULL;
+    ffurl_closep(&in);
 
     par = avcodec_parameters_alloc();
     if (!par) {
@@ -2571,8 +2569,7 @@ fail_nobuf:
     av_bprint_finalize(&sdp, NULL);
 fail:
     avcodec_parameters_free(&par);
-    if (in)
-        ffurl_close(in);
+    ffurl_closep(&in);
     ff_network_close();
     return ret;
 }
diff --git a/libavformat/sapdec.c b/libavformat/sapdec.c
index b3644b436b..eec73aa2f4 100644
--- a/libavformat/sapdec.c
+++ b/libavformat/sapdec.c
@@ -54,8 +54,7 @@ static int sap_read_close(AVFormatContext *s)
     struct SAPState *sap = s->priv_data;
     if (sap->sdp_ctx)
         avformat_close_input(&sap->sdp_ctx);
-    if (sap->ann_fd)
-        ffurl_close(sap->ann_fd);
+    ffurl_closep(&sap->ann_fd);
     av_freep(&sap->sdp);
     ff_network_close();
     return 0;
diff --git a/libavformat/sapenc.c b/libavformat/sapenc.c
index f9afab0c33..dc1c210408 100644
--- a/libavformat/sapenc.c
+++ b/libavformat/sapenc.c
@@ -60,8 +60,7 @@ static int sap_write_close(AVFormatContext *s)
     }
 
     av_freep(&sap->ann);
-    if (sap->ann_fd)
-        ffurl_close(sap->ann_fd);
+    ffurl_closep(&sap->ann_fd);
     ff_network_close();
     return 0;
 }
diff --git a/libavformat/smoothstreamingenc.c b/libavformat/smoothstreamingenc.c
index d10d5d1444..33bb404f46 100644
--- a/libavformat/smoothstreamingenc.c
+++ b/libavformat/smoothstreamingenc.c
@@ -99,14 +99,9 @@ static int64_t ism_seek(void *opaque, int64_t offset, int whence)
     if (whence != SEEK_SET)
         return AVERROR(ENOSYS);
     if (os->tail_out) {
-        if (os->out) {
-            ffurl_close(os->out);
-        }
-        if (os->out2) {
-            ffurl_close(os->out2);
-        }
+        ffurl_closep(&os->out);
+        ffurl_closep(&os->out2);
         os->out = os->tail_out;
-        os->out2 = NULL;
         os->tail_out = NULL;
     }
     if (offset >= os->cur_start_pos) {
@@ -175,10 +170,9 @@ static void ism_free(AVFormatContext *s)
         return;
     for (i = 0; i < s->nb_streams; i++) {
         OutputStream *os = &c->streams[i];
-        ffurl_close(os->out);
-        ffurl_close(os->out2);
-        ffurl_close(os->tail_out);
-        os->out = os->out2 = os->tail_out = NULL;
+        ffurl_closep(&os->out);
+        ffurl_closep(&os->out2);
+        ffurl_closep(&os->tail_out);
         if (os->ctx && os->ctx_inited)
             av_write_trailer(os->ctx);
         if (os->ctx && os->ctx->pb)
@@ -537,8 +531,7 @@ static int ism_flush(AVFormatContext *s, int final)
         if (!os->out || os->tail_out)
             return AVERROR(EIO);
 
-        ffurl_close(os->out);
-        os->out = NULL;
+        ffurl_closep(&os->out);
         size = os->tail_pos - os->cur_start_pos;
         if ((ret = parse_fragment(s, filename, &start_ts, &duration, &moof_size, size)) < 0)
             break;
diff --git a/libavformat/srtpproto.c b/libavformat/srtpproto.c
index 5e6e5164d7..13e2245015 100644
--- a/libavformat/srtpproto.c
+++ b/libavformat/srtpproto.c
@@ -59,8 +59,7 @@ static int srtp_close(URLContext *h)
     SRTPProtoContext *s = h->priv_data;
     ff_srtp_free(&s->srtp_out);
     ff_srtp_free(&s->srtp_in);
-    ffurl_close(s->rtp_hd);
-    s->rtp_hd = NULL;
+    ffurl_closep(&s->rtp_hd);
     return 0;
 }
 
diff --git a/libavformat/subfile.c b/libavformat/subfile.c
index 5d8659c8c4..300672e657 100644
--- a/libavformat/subfile.c
+++ b/libavformat/subfile.c
@@ -86,7 +86,7 @@ static int subfile_open(URLContext *h, const char *filename, int flags,
         return ret;
     c->pos = c->start;
     if ((ret = slave_seek(h)) < 0) {
-        ffurl_close(c->h);
+        ffurl_closep(&c->h);
         return ret;
     }
     return 0;
@@ -95,7 +95,7 @@ static int subfile_open(URLContext *h, const char *filename, int flags,
 static int subfile_close(URLContext *h)
 {
     SubfileContext *c = h->priv_data;
-    return ffurl_close(c->h);
+    return ffurl_closep(&c->h);
 }
 
 static int subfile_read(URLContext *h, unsigned char *buf, int size)
diff --git a/libavformat/tls_gnutls.c b/libavformat/tls_gnutls.c
index e41156cc30..0c4ef34f5f 100644
--- a/libavformat/tls_gnutls.c
+++ b/libavformat/tls_gnutls.c
@@ -100,8 +100,7 @@ static int tls_close(URLContext *h)
         gnutls_deinit(c->session);
     if (c->cred)
         gnutls_certificate_free_credentials(c->cred);
-    if (c->tls_shared.tcp)
-        ffurl_close(c->tls_shared.tcp);
+    ffurl_closep(&c->tls_shared.tcp);
     ff_gnutls_deinit();
     return 0;
 }
diff --git a/libavformat/tls_libtls.c b/libavformat/tls_libtls.c
index ba83b56ffe..dff7f2d9fb 100644
--- a/libavformat/tls_libtls.c
+++ b/libavformat/tls_libtls.c
@@ -44,8 +44,7 @@ static int ff_tls_close(URLContext *h)
         tls_close(p->ctx);
         tls_free(p->ctx);
     }
-    if (p->tls_shared.tcp)
-        ffurl_close(p->tls_shared.tcp);
+    ffurl_closep(&p->tls_shared.tcp);
     return 0;
 }
 
diff --git a/libavformat/tls_openssl.c b/libavformat/tls_openssl.c
index e305b2465a..002197fa76 100644
--- a/libavformat/tls_openssl.c
+++ b/libavformat/tls_openssl.c
@@ -142,8 +142,7 @@ static int tls_close(URLContext *h)
     }
     if (c->ctx)
         SSL_CTX_free(c->ctx);
-    if (c->tls_shared.tcp)
-        ffurl_close(c->tls_shared.tcp);
+    ffurl_closep(&c->tls_shared.tcp);
 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
     if (c->url_bio_method)
         BIO_meth_free(c->url_bio_method);
diff --git a/libavformat/tls_schannel.c b/libavformat/tls_schannel.c
index fec43ffafd..4bfaa85228 100644
--- a/libavformat/tls_schannel.c
+++ b/libavformat/tls_schannel.c
@@ -138,8 +138,7 @@ static int tls_close(URLContext *h)
     av_freep(&c->dec_buf);
     c->dec_buf_size = c->dec_buf_offset = 0;
 
-    if (c->tls_shared.tcp)
-        ffurl_close(c->tls_shared.tcp);
+    ffurl_closep(&c->tls_shared.tcp);
     return 0;
 }
 
diff --git a/libavformat/tls_securetransport.c b/libavformat/tls_securetransport.c
index 37380541b1..3250b23051 100644
--- a/libavformat/tls_securetransport.c
+++ b/libavformat/tls_securetransport.c
@@ -251,8 +251,7 @@ static int tls_close(URLContext *h)
     }
     if (c->ca_array)
         CFRelease(c->ca_array);
-    if (c->tls_shared.tcp)
-        ffurl_close(c->tls_shared.tcp);
+    ffurl_closep(&c->tls_shared.tcp);
     return 0;
 }
 
-- 
2.20.1



More information about the ffmpeg-devel mailing list