From patchwork Mon Dec 16 00:04:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16815 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 0AB7B44A07A for ; Mon, 16 Dec 2019 02:04:53 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E3BC468AB6A; Mon, 16 Dec 2019 02:04:52 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DD32268AB36 for ; Mon, 16 Dec 2019 02:04:44 +0200 (EET) Received: by mail-wm1-f67.google.com with SMTP id p17so4801390wma.1 for ; Sun, 15 Dec 2019 16:04:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=B/oyIM5u+EQKk6mecAQDyeq4pEaW7AS7wp/DxEitO1U=; b=LNQNs7vrA6md4vX+n4UM6H3tQH/bsrTRtDx4lSTDgWlqNesWjCwf+S1x4vJZq9FTtm AGYv4vr926af73IUpjDupSqe1RsF5bScKiiAFb305KzEAMFFYV4fO7yRrNX1h6pWhKbv N6B59BMCCH/h1NrjJEyF5sJWnDWScDcd+PSz3eTCMUraPRv9sAhFRR8w5n0Lz+IetkU7 Lu1msPRbG0FSLxkJwu4Lol6hgqP5kmV9tMVLWqsau8sRfEtJokuxAUcm8Fj8DBv9xUcI LW+kh3LrsqpwTd1FYzjyas8B6HA9wz2AwyezsUyxogxQbfdTvYADGm2ote+yTIPkGbvh JzbA== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=B/oyIM5u+EQKk6mecAQDyeq4pEaW7AS7wp/DxEitO1U=; b=s0ELdCmaFjVACjXWdlnKd4wwhfCXVk2wjPI3hkKtia2MJ5acRBNsbLJDY3l4lL402l GSWb8kG3wejhud3ozWaQ4CV9Spuq5TO3UfkWGfifuAn4hQnGvMqqw86rp1BbBY3E2Mzy V/tUNBWsUFzg+3WDfPbRo7urhphyXZDSMZxcQbbdNe2sEv1ZgG+f7UV0pvKrJ3P9+MoR sfe5CWPiQr8EsV6yiiBxrHClkXHh/uaumiWqnjw9zSzQEQXUrtPqD7RJ1OebFWur+Ydv T+4bB8lC1+nL2mv0nfWYRf0CPR7K6FCJIu8HpuF56rguKIa2omP/b/HaVp/FFEDKP9gF 5OFQ== X-Gm-Message-State: APjAAAUgxwvu4VifqffQbk1V7SWTvgAOnm3EDGLVk4Bf16ElMx24cTQ7 kAEVqiZ1kyajR1BABe1LsKZOI16B X-Google-Smtp-Source: APXvYqy3aH+pWavGNv0q7BMd1GWLVvN6n/ZyN9iOfuqTs8fXenVknwVC1AQtjYF7FmUD0SqYQ4spaQ== X-Received: by 2002:a1c:ddc5:: with SMTP id u188mr13687311wmg.83.1576454684045; Sun, 15 Dec 2019 16:04:44 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08e23.dynamic.kabel-deutschland.de. [188.192.142.35]) by smtp.gmail.com with ESMTPSA id s1sm19214442wmc.23.2019.12.15.16.04.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Dec 2019 16:04:43 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 16 Dec 2019 01:04:07 +0100 Message-Id: <20191216000418.24707-6-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191216000418.24707-1-andreas.rheinhardt@gmail.com> References: <20191216000418.24707-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/16] avformat/hlsenc: Fix leak of options when writing packets 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" Under certain circumstances hls_write_packet() would add options to an AVDictionary. Said dictionary was never explicitly freed, instead it was presumed that these options would be consumed when opening a new IO-context. This left several possibilities for memleaks: a) When no new IO-context would be opened at all. This is possible when using both the flags temp_file and single_file together with a file output. b) When an error happens before one actually tries to open the new IO-context. c) When the new IO-context does not consume all options. All three have been fixed; furthermore, the AVDictionary has been put into a smaller scope (namely the only part of hls_write_packet() where it is actually used). Signed-off-by: Andreas Rheinhardt --- This patch is concerned with fixing memleaks. Yet I noticed a discrepancy in the handling of options between the first try to open output and the second one (the one for reflushing in case the first one fails): The second one only gets the options not consumed in the first one. Is this intended? libavformat/hlsenc.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index 5695af2208..5b3856099c 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -2241,7 +2241,6 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) int use_temp_file = 0; uint8_t *buffer = NULL; VariantStream *vs = NULL; - AVDictionary *options = NULL; char *old_filename = NULL; for (i = 0; i < hls->nb_varstreams; i++) { @@ -2343,11 +2342,6 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) use_temp_file = proto && !strcmp(proto, "file") && (hls->flags & HLS_TEMP_FILE); } - // look to rename the asset name - if (use_temp_file) { - av_dict_set(&options, "mpegts_flags", "resend_headers", 0); - } - if (hls->flags & HLS_SINGLE_FILE) { ret = flush_dynbuf(vs, &range_length); av_freep(&vs->temp_buffer); @@ -2356,8 +2350,8 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) } vs->size = range_length; } else { - set_http_options(s, &options, hls); if ((hls->max_seg_size > 0 && (vs->size >= hls->max_seg_size)) || !byterange_mode) { + AVDictionary *options = NULL; char *filename = NULL; if (hls->key_info_file || hls->encrypt) { av_dict_set(&options, "encryption_key", hls->key_string, 0); @@ -2367,12 +2361,21 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) filename = av_asprintf("%s", oc->url); } if (!filename) { + av_dict_free(&options); return AVERROR(ENOMEM); } + + // look to rename the asset name + if (use_temp_file) + av_dict_set(&options, "mpegts_flags", "resend_headers", 0); + + set_http_options(s, &options, hls); + ret = hlsenc_io_open(s, &vs->out, filename, &options); if (ret < 0) { av_log(s, hls->ignore_io_errors ? AV_LOG_WARNING : AV_LOG_ERROR, "Failed to open file '%s'\n", filename); + av_dict_free(&options); return hls->ignore_io_errors ? 0 : ret; } if (hls->segment_type == SEGMENT_TYPE_FMP4) { @@ -2380,6 +2383,7 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) } ret = flush_dynbuf(vs, &range_length); if (ret < 0) { + av_dict_free(&options); return ret; } ret = hlsenc_io_close(s, &vs->out, filename); @@ -2391,6 +2395,7 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) reflush_dynbuf(vs, &range_length); ret = hlsenc_io_close(s, &vs->out, filename); } + av_dict_free(&options); av_freep(&vs->temp_buffer); av_freep(&filename); }