From patchwork Mon Aug 19 21:56:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 14592 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 7AD28444AC6 for ; Tue, 20 Aug 2019 01:02:59 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4F53E68AAE8; Tue, 20 Aug 2019 01:02:59 +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 76ABD68A7CB for ; Tue, 20 Aug 2019 01:02:53 +0300 (EEST) Received: by mail-wm1-f68.google.com with SMTP id v19so840889wmj.5 for ; Mon, 19 Aug 2019 15:02: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=s4loACCt9JFJfjAuutYeolqGcjNWRKPpSN0ezA7+pT4=; b=MZ8yrO3rJKNOzkt80ZtRuBG3g+3EAOP/fcCZYln+tGxuF4zv0TPDX7azJJjVQIL7BB qrZZoFNzE652TswwIpbkLUlnOWgJh4xgtJPxFT2grzzoybLwMjoNwoUE4DkkE3U5Msnr RQGp1TxTXi+Qw0CtzycDl2eLx01akadQRluPh1ZCIGo6xGpNMQTd8OAw9vIYPS043w62 abSp0DNbzjrHLAjIR/V10yUUFVjq9/oS1F2Y79+aGGHGu5efNZAQ7ikct2QIrooLAC/w 0DX9l9K38yPvM7PNLL2+Yd8bZevekMAWMyWdSVb7hoy6W5curHZiVYsEcTjgWSNiPyFY sT9g== 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=s4loACCt9JFJfjAuutYeolqGcjNWRKPpSN0ezA7+pT4=; b=mPPyE9bGwo4y6qnvOmZshEDZUt99NjGNrWCa9AH7XlLo2GXPexAEmLZBU2vBIa4sW/ UpUV3XwsAaUyoAZjLAeMc+kFp40RA+n4kdtOLgvrAVbUa6i0npg++N9BX9eTBEMRngOe gvpTbCNifCOYvBdKvmMIp74DrmiHGVHiOm8OzJQiLoN6E5VfMK8G0euc05vuMfnO90Fr yeMxMePCOiDMP4KSCCv+iocPnXrVJ2MZg2cLiOS7AmsU6HvQhUY9DUg5hxdiJ0/fJ1y3 xnagzHVVImRpCwvuV+TOlgQ1weEk71syCQJrnyH3q4QFOjjjWmI0qGf+VCcL581tx9PF /fFw== X-Gm-Message-State: APjAAAVp7NCmjMxcGJyJi+9Cb5UeyaElYvc6Iu8IIIog0H2Q1g3xvfhj 0Fw/cipefpiFhvjt0iPiaiVsrWpa X-Google-Smtp-Source: APXvYqyqzlzOxdYoAvgSX4J4jwetLfr51z2MBS1mIQdQFkaCdCgeu7mbUF71ltfKM3VfzJDbfqxNIQ== X-Received: by 2002:a1c:dd82:: with SMTP id u124mr22738721wmg.89.1566251834869; Mon, 19 Aug 2019 14:57:14 -0700 (PDT) Received: from localhost.localdomain (ipbcc08c5c.dynamic.kabel-deutschland.de. [188.192.140.92]) by smtp.gmail.com with ESMTPSA id s64sm32952293wmf.16.2019.08.19.14.57.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Aug 2019 14:57:14 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 19 Aug 2019 23:56:17 +0200 Message-Id: <20190819215624.49795-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 1/8] avformat/utils: Fix memleaks 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" ff_read_packet had several potential memleaks: 1. If av_packet_make_refcounted fails, it means that the packet is not refcounted, but it could nevertheless carry side data and therefore needs to be unreferenced. 2. If a packet happens to have an illegal stream index (i.e. one that does not correspond to a stream), it should nevertheless be unreferenced. 3. If putting a packet on a packet list fails, it wasn't unreferenced. Furthermore, read_frame_internal leaked a packet's (side) data if a context update was required and failed. Signed-off-by: Andreas Rheinhardt --- The two memleaks in read_frame_internal have been added in v2. libavformat/utils.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/libavformat/utils.c b/libavformat/utils.c index b57e680089..564be02334 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -873,8 +873,10 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt) } err = av_packet_make_refcounted(pkt); - if (err < 0) + if (err < 0) { + av_packet_unref(pkt); return err; + } if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) && (pkt->flags & AV_PKT_FLAG_CORRUPT)) { @@ -887,6 +889,7 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt) if (pkt->stream_index >= (unsigned)s->nb_streams) { av_log(s, AV_LOG_ERROR, "Invalid stream index %d\n", pkt->stream_index); + av_packet_unref(pkt); continue; } @@ -917,8 +920,10 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt) err = ff_packet_list_put(&s->internal->raw_packet_buffer, &s->internal->raw_packet_buffer_end, pkt, 0); - if (err) + if (err < 0) { + av_packet_unref(pkt); return err; + } s->internal->raw_packet_buffer_remaining_size -= pkt->size; if ((err = probe_codec(s, st, pkt)) < 0) @@ -1611,15 +1616,19 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt) } ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar); - if (ret < 0) + if (ret < 0) { + av_packet_unref(&cur_pkt); return ret; + } #if FF_API_LAVF_AVCTX FF_DISABLE_DEPRECATION_WARNINGS /* update deprecated public codec context */ ret = avcodec_parameters_to_context(st->codec, st->codecpar); - if (ret < 0) + if (ret < 0) { + av_packet_unref(&cur_pkt); return ret; + } FF_ENABLE_DEPRECATION_WARNINGS #endif