From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 8EC9148120 for ; Tue, 9 Jul 2024 09:34:51 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0EEF068DB42; Tue, 9 Jul 2024 12:34:48 +0300 (EEST) Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7CB8968DB42 for ; Tue, 9 Jul 2024 12:34:41 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1720517680; bh=T7HZH33Jpc7+c/8oU1ZOHidN1I3yj4sMXovPAHbhH2w=; h=From:To:Cc:Subject:Date:From; b=KmUObW1+iwWGdQVAjraqckxTj5QbPrznQWlA4TtGcTYe6HuTmWRfVOmCyraoWJiyj TY2O3p8FqQAUfjqX46WTV2gzl0R5oqq055U5jtyfqVJCWBUIelLfuBbj6lzv4Fi4Zb kY1TjIV3d718kbfhHinuilwWgIvpJahW1M7sBgiU= Received: from localhost (unknown [217.111.52.58]) by haasn.dev (Postfix) with ESMTPSA id 6394C406C7; Tue, 9 Jul 2024 11:34:40 +0200 (CEST) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Tue, 9 Jul 2024 11:34:36 +0200 Message-ID: <20240709093437.16563-1-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] avfilter/vf_scale: fix frame lifetimes X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Niklas Haas Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Niklas Haas scale_frame() inconsistently handled the lifetime of `in`. Fixes a possible double free and a possible memory leak. The new code always has `scale_frame` take over ownership of the input frame. I first tried writing this code in a way where the calling code retains ownership, but this is nontrivial due to the presence of the no-op short-circuit condition in which the input frame is directly returned. (As an alternative, we could use av_frame_clone() instead, but I wanted to avoid touching the original behavior in this commit) --- libavfilter/vf_scale.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c index 841075193e..a1a322ed9e 100644 --- a/libavfilter/vf_scale.c +++ b/libavfilter/vf_scale.c @@ -869,17 +869,20 @@ static int scale_field(ScaleContext *scale, AVFrame *dst, AVFrame *src, return 0; } -static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out) +/* Takes over ownership of *frame_in, passes ownership of *frame_out to caller */ +static int scale_frame(AVFilterLink *link, AVFrame **frame_in, + AVFrame **frame_out) { AVFilterContext *ctx = link->dst; ScaleContext *scale = ctx->priv; AVFilterLink *outlink = ctx->outputs[0]; - AVFrame *out; + AVFrame *out, *in = *frame_in; const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format); char buf[32]; int ret; int frame_changed; + *frame_in = NULL; *frame_out = NULL; if (in->colorspace == AVCOL_SPC_YCGCO) av_log(link->dst, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n"); @@ -922,11 +925,11 @@ static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out) ret = scale_parse_expr(ctx, NULL, &scale->w_pexpr, "width", scale->w_expr); if (ret < 0) - return ret; + goto err; ret = scale_parse_expr(ctx, NULL, &scale->h_pexpr, "height", scale->h_expr); if (ret < 0) - return ret; + goto err; } if (ctx->filter == &ff_vf_scale2ref) { @@ -957,7 +960,7 @@ FF_ENABLE_DEPRECATION_WARNINGS link->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num; if ((ret = config_props(outlink)) < 0) - return ret; + goto err; } scale: @@ -971,10 +974,9 @@ scale: out = ff_get_video_buffer(outlink, outlink->w, outlink->h); if (!out) { - av_frame_free(&in); - return AVERROR(ENOMEM); + ret = AVERROR(ENOMEM); + goto err; } - *frame_out = out; av_frame_copy_props(out, in); out->width = outlink->w; @@ -999,9 +1001,12 @@ scale: ret = sws_scale_frame(scale->sws, out, in); } - av_frame_free(&in); if (ret < 0) - av_frame_free(frame_out); + av_frame_free(&out); + *frame_out = out; + +err: + av_frame_free(&in); return ret; } @@ -1058,7 +1063,7 @@ FF_ENABLE_DEPRECATION_WARNINGS } } - ret = scale_frame(ctx->inputs[0], in, &out); + ret = scale_frame(ctx->inputs[0], &in, &out); if (out) { out->pts = av_rescale_q(fs->pts, fs->time_base, outlink->time_base); return ff_filter_frame(outlink, out); @@ -1077,7 +1082,7 @@ static int filter_frame(AVFilterLink *link, AVFrame *in) AVFrame *out; int ret; - ret = scale_frame(link, in, &out); + ret = scale_frame(link, &in, &out); if (out) return ff_filter_frame(outlink, out); -- 2.44.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".