From patchwork Tue Aug 13 02:47:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 14468 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 52028449ADB for ; Tue, 13 Aug 2019 05:54:59 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3B69B68ABB7; Tue, 13 Aug 2019 05:54:59 +0300 (EEST) 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 6E0F468AB39 for ; Tue, 13 Aug 2019 05:54:53 +0300 (EEST) Received: by mail-wm1-f67.google.com with SMTP id p77so68370wme.0 for ; Mon, 12 Aug 2019 19:54: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:in-reply-to:references :mime-version:content-transfer-encoding; bh=eq2tgrnzmI42Kdqb0rOHfSmvEOVaDPZymiGqVXI0jQo=; b=pLHaDxJpTHc0OBXkVcrDLvPTB1U97MNQUC/KStyEFZS59htfbYSL6vzIOL7jGwJ9fE +i4Bsxw8TIM4YtY9HDe+UNFI+gxCjK9vwN5LWRZvQS1+QutswYqMaG/Li1kbLm9NKIYY 0Uzc3f9wZVBtPoMc8fAlVeltTJGgxvsbTAPsoEsQn+VjCJBLxveJn4OVZ0EURFnsoqxt PBr8Q65aeMzSsSEoo4s1zXRCSJqwSHAkhsEELLxQgkfe8GZPJnl8i5t0kzGBXd6UjGS1 mzsExvF4MglT4He16/EUMRyW7rmiH0RcDMQNmZ/KKytmdxMiflzHtEEBj8JURPF/QQnu RbVg== 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=eq2tgrnzmI42Kdqb0rOHfSmvEOVaDPZymiGqVXI0jQo=; b=Qh5iS8ilBRvufr7udSfsB1ApDQXdjw+oYzaAxCeAaN14uPFomB0VjjXkR8D/Qhum1y Q+bLNYB7cC2eegOlxGGm2ZxZm+HGq4J1/gEt4CZQ4aJNaSu3k0+8arspJrOmyeJEcxVT Ka8TJqlPpPwq2LaGaS2Gsz9KIRjqS3Jq8CFzjlFmEG4eM32hi1U0Id43RqUO5Qx3P0X6 NvyJ13Yj5xidcyd4hgKkQkh8oma26O69KWZdnztpsgjcwQ2S4QbBShVv4pP3iHrLnpbK P2zzmVifOX4ba1n3h/ZzCyX4Gbehf5RXpFzgZiMsI7+rzETMbalVrIaRG3y04xA+etOY yFHQ== X-Gm-Message-State: APjAAAVLJiHdoSQY+XsV85VAihHUN/ZwNbGTE6qmGN+ydu38+OgiBUdu +GsIVhT8b7ZjTSLj7SfgxVLU0Qsx X-Google-Smtp-Source: APXvYqzLurp8yXCTT4Q4hSuNmhOhQkanwkxHp5nkMJRPwOiMUOMki37A8EpdBIlHcHUvSWrNS1cCMQ== X-Received: by 2002:a7b:c0d0:: with SMTP id s16mr148255wmh.65.1565664530918; Mon, 12 Aug 2019 19:48:50 -0700 (PDT) Received: from localhost.localdomain (ipbcc06ceb.dynamic.kabel-deutschland.de. [188.192.108.235]) by smtp.gmail.com with ESMTPSA id k13sm25020613wro.97.2019.08.12.19.48.50 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 12 Aug 2019 19:48:50 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 13 Aug 2019 04:47:23 +0200 Message-Id: <20190813024726.6596-10-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190813024726.6596-1-andreas.rheinhardt@gmail.com> References: <20190813024726.6596-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/13] avformat/mux: Fix memleaks on errors I 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" If writing an uncoded frame fails at the preparatory steps of av_[interleaved_]write_frame, the frame would be either not freed at all in case of av_write_frame or would leak when the fake packet would be unreferenced like an ordinary packet. There is also a memleak when the output format is not suited for writing uncoded frames as the documentation states that ownership of the frame passes to av_[interleaved_]write_uncoded frame. Both of these memleaks have been fixed. Signed-off-by: Andreas Rheinhardt --- libavformat/mux.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index 5b67a793ac..c524c0886c 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -1241,7 +1241,11 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt) return s->pb->error; } fail: - av_packet_unref(pkt); + // This is a deviation from the usual behaviour + // of av_interleaved_write_frame: We leave cleaning + // up uncoded frames to write_uncoded_frame_internal. + if (!(pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) + av_packet_unref(pkt); return ret; } @@ -1334,10 +1338,13 @@ static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index, AVFrame *frame, int interleaved) { AVPacket pkt, *pktp; + int ret; av_assert0(s->oformat); - if (!s->oformat->write_uncoded_frame) - return AVERROR(ENOSYS); + if (!s->oformat->write_uncoded_frame) { + ret = AVERROR(ENOSYS); + goto free; + } if (!frame) { pktp = NULL; @@ -1353,8 +1360,14 @@ static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index, pkt.flags |= AV_PKT_FLAG_UNCODED_FRAME; } - return interleaved ? av_interleaved_write_frame(s, pktp) : - av_write_frame(s, pktp); + ret = interleaved ? av_interleaved_write_frame(s, pktp) : + av_write_frame(s, pktp); + if (ret < 0 && pktp && pktp->data) { + frame = (AVFrame*)pktp->data; + free: + av_frame_free(&frame); + } + return ret; } int av_write_uncoded_frame(AVFormatContext *s, int stream_index,