History

avfilter/vf_scale: fix frame lifetimes

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)

Niklas Haas committed 2 years ago

Commit 084e0b31 parent f1ed3511 file · +17 −12Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/vf_scale.c+17−12
@@ -869,17 +869,20 @@ static int scale_field(ScaleContext *scale, AVFrame *dst, AVFrame *src, · 868 unmodified lines
869869 return 0;
870870 }
871871
872−static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out)
872+/* Takes over ownership of *frame_in, passes ownership of *frame_out to caller */
873+static int scale_frame(AVFilterLink *link, AVFrame **frame_in,
874+ AVFrame **frame_out)
873875 {
874876 AVFilterContext *ctx = link->dst;
875877 ScaleContext *scale = ctx->priv;
876878 AVFilterLink *outlink = ctx->outputs[0];
877− AVFrame *out;
879+ AVFrame *out, *in = *frame_in;
878880 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
879881 char buf[32];
880882 int ret;
881883 int frame_changed;
882884
885+ *frame_in = NULL;
883886 *frame_out = NULL;
884887 if (in->colorspace == AVCOL_SPC_YCGCO)
885888 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) · 36 unmodified lines
922925
923926 ret = scale_parse_expr(ctx, NULL, &scale->w_pexpr, "width", scale->w_expr);
924927 if (ret < 0)
925− return ret;
928+ goto err;
926929
927930 ret = scale_parse_expr(ctx, NULL, &scale->h_pexpr, "height", scale->h_expr);
928931 if (ret < 0)
929− return ret;
932+ goto err;
930933 }
931934
932935 if (ctx->filter == &ff_vf_scale2ref) {
@@ -957,7 +960,7 @@ FF_ENABLE_DEPRECATION_WARNINGS · 24 unmodified lines
957960 link->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
958961
959962 if ((ret = config_props(outlink)) < 0)
960− return ret;
963+ goto err;
961964 }
962965
963966 scale:
@@ -971,10 +974,9 @@ scale: · 7 unmodified lines
971974
972975 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
973976 if (!out) {
974− av_frame_free(&in);
975− return AVERROR(ENOMEM);
977+ ret = AVERROR(ENOMEM);
978+ goto err;
976979 }
977− *frame_out = out;
978980
979981 av_frame_copy_props(out, in);
980982 out->width = outlink->w;
@@ -999,9 +1001,12 @@ scale: · 18 unmodified lines
9991001 ret = sws_scale_frame(scale->sws, out, in);
10001002 }
10011003
1002− av_frame_free(&in);
10031004 if (ret < 0)
1004− av_frame_free(frame_out);
1005+ av_frame_free(&out);
1006+ *frame_out = out;
1007+
1008+err:
1009+ av_frame_free(&in);
10051010 return ret;
10061011 }
10071012
@@ -1058,7 +1063,7 @@ FF_ENABLE_DEPRECATION_WARNINGS · 50 unmodified lines
10581063 }
10591064 }
10601065
1061− ret = scale_frame(ctx->inputs[0], in, &out);
1066+ ret = scale_frame(ctx->inputs[0], &in, &out);
10621067 if (out) {
10631068 out->pts = av_rescale_q(fs->pts, fs->time_base, outlink->time_base);
10641069 return ff_filter_frame(outlink, out);
@@ -1077,7 +1082,7 @@ static int filter_frame(AVFilterLink *link, AVFrame *in) · 12 unmodified lines
10771082 AVFrame *out;
10781083 int ret;
10791084
1080− ret = scale_frame(link, in, &out);
1085+ ret = scale_frame(link, &in, &out);
10811086 if (out)
10821087 return ff_filter_frame(outlink, out);
10831088

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t