From patchwork Mon May 25 06:27:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 19843 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 6579444AC60 for ; Mon, 25 May 2020 09:28:00 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3C88E688069; Mon, 25 May 2020 09:28:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3215A68804A for ; Mon, 25 May 2020 09:27:53 +0300 (EEST) Received: by mail-wm1-f68.google.com with SMTP id v19so8483326wmj.0 for ; Sun, 24 May 2020 23:27:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=UABsMz/89OkNoDLTNf4ZI3PbHxYeASGJRXiXpS/hU58=; b=IapvwETNrnTzioDAwM9hp2J1V7+i+FI3XDOCsaNAip5m2EqIWbJwO+rVbG7M9IWDjh hVw1UautLqyyRUna2zR5dgSfnSOZbELECCEHy6H6fMm6R2bFh2Wd7GT6Zdonezx6al2z I0YptoAnU4rac60L8pQ21Kgf/1YZnfiBiOcIGZ8QcQl7IIHkeMz5sy8eSFsv2tnG0q+o p0BfpTBX+8pYtR1J0yc2IbVZsJ9/kLVE8whDSYEDzOwcVtl+sOe/Aapt2ueytbxk9Fnz Rh4++n2KPt6CrjqdR8+ciAZnKQUqYiu0M/YkUOSDDA1t271jEzIVKDHG+NALidgYZDak KZVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=UABsMz/89OkNoDLTNf4ZI3PbHxYeASGJRXiXpS/hU58=; b=QHXYuRZXGurSbHZdS7u3XvzucjdIu3wvLSUTnB2eveoI2QBNQY1ArS0CrHQ6SqQYtt uoauoCbMee698lfnae45ILfgfe7kO3ItAQzYIGE0nyG0bz6Mr0c1UxVRfnQ3gKVTqyC1 oaKjWInbEoOE5oTenVhF3qJVQ81VjV+DuBT7nz80LFC7cbv9bMgEhw2nx38VWuCgTl3c m/GziVuw47Gr4XNLLsLT8oMEmE8cyBSy/zvsIYiz2dwlpyj03IOG9mLPDzYXui/qlPTr ey5pJRzcEss8/jyK0DJqjlw53P4sPrfNZ2hxtVP8u4XqCayV+taHhi7RETWI6BrajAmm EF5g== X-Gm-Message-State: AOAM5302lsbJNlDBypDjnEaZOjetu35GY6B0rdVI7V+YxPYan0171YsN oDNGdx84GON4mTYUKOuAdgnUkGbe X-Google-Smtp-Source: ABdhPJw8Cq3KMMwkyhs6TYOihdadsDdsrDBMhLZA8znJbdXqt80R5rP11DmOWxce24pTAsirsS3YHg== X-Received: by 2002:a05:600c:34c:: with SMTP id u12mr23683350wmd.4.1590388071970; Sun, 24 May 2020 23:27:51 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id l13sm16859646wrm.55.2020.05.24.23.27.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 May 2020 23:27:51 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 25 May 2020 08:27:44 +0200 Message-Id: <20200525062744.15753-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat: Replace ffurl_close() by ffurl_closep() where appropriate X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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 --- 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; }