History

libavfilter: guard against ff_draw_init/ff_draw_init2 failures

The return value of ff_draw_init and ff_draw_init2 are not checked in most usages. However, if they return an error, they don't get to the point where they set the attributes of the FFDrawContext. These functions are typically used in conjunction with ff_draw_color, which checks draw->desc->flags, causing a null pointer dereference.

Nil Fons Miret committed 1 year ago

Commit 9899da81 parent bdc07f312 files · +109 −26Browse files

Unified · Split

12 files
  1. libavfilter/qrencode.c +22 −5
  2. libavfilter/src_avsynctest.c +6 −1
  3. libavfilter/vf_datascope.c +25 −6
  4. libavfilter/vf_drawtext.c +5 −1
  5. libavfilter/vf_pad.c +5 −1
  6. libavfilter/vf_shear.c +6 −1
  7. libavfilter/vf_stack.c +5 −1
  8. libavfilter/vf_subtitles.c +11 −5
  9. libavfilter/vf_tile.c +6 −1
  10. libavfilter/vf_tinterlace.c +5 −1
  11. libavfilter/vf_tpad.c +6 −1
  12. libavfilter/vsrc_testsrc.c +7 −2
libavfilter/qrencode.c+22−5
@@ -636,11 +636,20 @@ static int qrencodesrc_config_props(AVFilterLink *outlink) · 635 unmodified lines
636636 return AVERROR(EINVAL);
637637 }
638638
639− ff_draw_init(&qr->draw, AV_PIX_FMT_ARGB, FF_DRAW_PROCESS_ALPHA);
639+ ret = ff_draw_init(&qr->draw, AV_PIX_FMT_ARGB, FF_DRAW_PROCESS_ALPHA);
640+ if (ret < 0) {
641+ // This call using constants should not fail. Checking its error code for completeness.
642+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
643+ return ret;
644+ }
640645 ff_draw_color(&qr->draw, &qr->draw_foreground_color, (const uint8_t *)&qr->foreground_color);
641646 ff_draw_color(&qr->draw, &qr->draw_background_color, (const uint8_t *)&qr->background_color);
642647
643− ff_draw_init2(&qr->draw0, outlink->format, outlink->colorspace, outlink->color_range, FF_DRAW_PROCESS_ALPHA);
648+ ret = ff_draw_init2(&qr->draw0, outlink->format, outlink->colorspace, outlink->color_range, FF_DRAW_PROCESS_ALPHA);
649+ if (ret < 0) {
650+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
651+ return ret;
652+ }
644653 ff_draw_color(&qr->draw0, &qr->draw0_background_color, (const uint8_t *)&qr->background_color);
645654
646655 outlink->w = qr->rendered_padded_qrcode_width;
@@ -734,8 +743,12 @@ static int qrencode_config_input(AVFilterLink *inlink) · 87 unmodified lines
734743
735744 qr->is_source = 0;
736745
737− ff_draw_init2(&qr->draw, inlink->format, inlink->colorspace, inlink->color_range,
738− FF_DRAW_PROCESS_ALPHA);
746+ ret = ff_draw_init2(&qr->draw, inlink->format, inlink->colorspace, inlink->color_range,
747+ FF_DRAW_PROCESS_ALPHA);
748+ if (ret < 0) {
749+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
750+ return ret;
751+ }
739752
740753 V(W) = V(main_w) = inlink->w;
741754 V(H) = V(main_h) = inlink->h;
@@ -764,8 +777,12 @@ static int qrencode_config_input(AVFilterLink *inlink) · 22 unmodified lines
764777 PARSE_EXPR(rendered_qrcode_width);
765778 PARSE_EXPR(rendered_padded_qrcode_width);
766779
767− ff_draw_init2(&qr->draw, inlink->format, inlink->colorspace, inlink->color_range,
780+ ret = ff_draw_init2(&qr->draw, inlink->format, inlink->colorspace, inlink->color_range,
768781 FF_DRAW_PROCESS_ALPHA);
782+ if (ret < 0) {
783+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
784+ return ret;
785+ }
769786 ff_draw_color(&qr->draw, &qr->draw_foreground_color, (const uint8_t *)&qr->foreground_color);
770787 ff_draw_color(&qr->draw, &qr->draw_background_color, (const uint8_t *)&qr->background_color);
771788
libavfilter/src_avsynctest.c+6−1
@@ -147,6 +147,7 @@ static av_cold int config_props(AVFilterLink *outlink) · 146 unmodified lines
147147 FilterLink *l = ff_filter_link(outlink);
148148 AVFilterContext *ctx = outlink->src;
149149 AVSyncTestContext *s = ctx->priv;
150+ int ret;
150151
151152 outlink->w = s->w;
152153 outlink->h = s->h;
@@ -160,7 +161,11 @@ static av_cold int config_props(AVFilterLink *outlink) · 7 unmodified lines
160161 s->dir = 1;
161162 s->prev_intpart = INT64_MIN;
162163
163− ff_draw_init2(&s->draw, outlink->format, outlink->colorspace, outlink->color_range, 0);
164+ ret = ff_draw_init2(&s->draw, outlink->format, outlink->colorspace, outlink->color_range, 0);
165+ if (ret < 0) {
166+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
167+ return ret;
168+ }
164169
165170 ff_draw_color(&s->draw, &s->fg, s->rgba[0]);
166171 ff_draw_color(&s->draw, &s->bg, s->rgba[1]);
libavfilter/vf_datascope.c+25−6
@@ -382,11 +382,18 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 381 unmodified lines
382382
383383 static int config_input(AVFilterLink *inlink)
384384 {
385− DatascopeContext *s = inlink->dst->priv;
385+ AVFilterContext *ctx = inlink->dst;
386+ DatascopeContext *s = ctx->priv;
387+
386388 uint8_t alpha = s->opacity * 255;
389+ int ret;
387390
388391 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
389− ff_draw_init2(&s->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
392+ ret = ff_draw_init2(&s->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
393+ if (ret < 0) {
394+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
395+ return ret;
396+ }
390397 ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
391398 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, alpha} );
392399 ff_draw_color(&s->draw, &s->yellow, (uint8_t[]){ 255, 255, 0, 255} );
@@ -509,10 +516,16 @@ AVFILTER_DEFINE_CLASS(pixscope); · 116 unmodified lines
509516
510517 static int pixscope_config_input(AVFilterLink *inlink)
511518 {
512− PixscopeContext *s = inlink->dst->priv;
519+ AVFilterContext *ctx = inlink->dst;
520+ PixscopeContext *s = ctx->priv;
521+ int ret;
513522
514523 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
515− ff_draw_init(&s->draw, inlink->format, 0);
524+ ret = ff_draw_init(&s->draw, inlink->format, 0);
525+ if (ret < 0) {
526+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
527+ return ret;
528+ }
516529 ff_draw_color(&s->draw, &s->dark, (uint8_t[]){ 0, 0, 0, s->o * 255} );
517530 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
518531 ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
@@ -927,11 +940,17 @@ static void update_oscilloscope(AVFilterContext *ctx) · 408 unmodified lines
927940
928941 static int oscilloscope_config_input(AVFilterLink *inlink)
929942 {
930− OscilloscopeContext *s = inlink->dst->priv;
943+ AVFilterContext *ctx = inlink->dst;
944+ OscilloscopeContext *s = ctx->priv;
931945 int size;
946+ int ret;
932947
933948 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
934− ff_draw_init(&s->draw, inlink->format, 0);
949+ ret = ff_draw_init(&s->draw, inlink->format, 0);
950+ if (ret < 0) {
951+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
952+ return ret;
953+ }
935954 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
936955 ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
937956 ff_draw_color(&s->draw, &s->green, (uint8_t[]){ 0, 255, 0, 255} );
libavfilter/vf_drawtext.c+5−1
@@ -1156,7 +1156,11 @@ static int config_input(AVFilterLink *inlink) · 1155 unmodified lines
11561156 char *expr;
11571157 int ret;
11581158
1159− ff_draw_init2(&s->dc, inlink->format, inlink->colorspace, inlink->color_range, FF_DRAW_PROCESS_ALPHA);
1159+ ret = ff_draw_init2(&s->dc, inlink->format, inlink->colorspace, inlink->color_range, FF_DRAW_PROCESS_ALPHA);
1160+ if (ret < 0) {
1161+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
1162+ return ret;
1163+ }
11601164 ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
11611165 ff_draw_color(&s->dc, &s->shadowcolor, s->shadowcolor.rgba);
11621166 ff_draw_color(&s->dc, &s->bordercolor, s->bordercolor.rgba);
libavfilter/vf_pad.c+5−1
@@ -114,7 +114,11 @@ static int config_input(AVFilterLink *inlink) · 113 unmodified lines
114114 double var_values[VARS_NB], res;
115115 char *expr;
116116
117− ff_draw_init2(&s->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
117+ ret = ff_draw_init2(&s->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
118+ if (ret < 0) {
119+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
120+ return ret;
121+ }
118122 ff_draw_color(&s->draw, &s->color, s->rgba_color);
119123
120124 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
libavfilter/vf_shear.c+6−1
@@ -250,6 +250,7 @@ static int config_output(AVFilterLink *outlink) · 249 unmodified lines
250250 AVFilterContext *ctx = outlink->src;
251251 ShearContext *s = ctx->priv;
252252 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
253+ int ret;
253254
254255 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
255256 s->depth = desc->comp[0].depth;
@@ -260,7 +261,11 @@ static int config_output(AVFilterLink *outlink) · 4 unmodified lines
260261 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(ctx->inputs[0]->h, desc->log2_chroma_h);
261262 s->planeheight[0] = s->planeheight[3] = ctx->inputs[0]->h;
262263
263− ff_draw_init2(&s->draw, outlink->format, outlink->colorspace, outlink->color_range, 0);
264+ ret = ff_draw_init2(&s->draw, outlink->format, outlink->colorspace, outlink->color_range, 0);
265+ if (ret < 0) {
266+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
267+ return ret;
268+ }
264269 ff_draw_color(&s->draw, &s->color, s->fillcolor);
265270
266271 s->filter_slice[0] = s->depth <= 8 ? filter_slice_nn8 : filter_slice_nn16;
libavfilter/vf_stack.c+5−1
@@ -312,7 +312,11 @@ static int config_output(AVFilterLink *outlink) · 311 unmodified lines
312312
313313 if (s->fillcolor_enable) {
314314 const AVFilterLink *inlink = ctx->inputs[0];
315− ff_draw_init2(&s->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
315+ ret = ff_draw_init2(&s->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
316+ if (ret < 0) {
317+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
318+ return ret;
319+ }
316320 ff_draw_color(&s->draw, &s->color, s->fillcolor);
317321 }
318322
libavfilter/vf_subtitles.c+11−5
@@ -182,12 +182,18 @@ static int query_formats(const AVFilterContext *ctx, · 181 unmodified lines
182182
183183 static int config_input(AVFilterLink *inlink)
184184 {
185− AssContext *ass = inlink->dst->priv;
185+ AVFilterContext *ctx = inlink->dst;
186+ AssContext *ass = ctx->priv;
187+ int ret;
186188
187− ff_draw_init2(&ass->draw, inlink->format,
188− ass_get_color_space(ass->track->YCbCrMatrix, inlink->colorspace),
189− ass_get_color_range(ass->track->YCbCrMatrix, inlink->color_range),
190− ass->alpha ? FF_DRAW_PROCESS_ALPHA : 0);
189+ ret = ff_draw_init2(&ass->draw, inlink->format,
190+ ass_get_color_space(ass->track->YCbCrMatrix, inlink->colorspace),
191+ ass_get_color_range(ass->track->YCbCrMatrix, inlink->color_range),
192+ ass->alpha ? FF_DRAW_PROCESS_ALPHA : 0);
193+ if (ret < 0) {
194+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
195+ return ret;
196+ }
191197
192198 ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
193199 if (ass->original_w && ass->original_h) {
libavfilter/vf_tile.c+6−1
@@ -128,6 +128,7 @@ static int config_props(AVFilterLink *outlink) · 127 unmodified lines
128128 FilterLink *ol = ff_filter_link(outlink);
129129 const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
130130 const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
131+ int ret;
131132
132133 if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
133134 av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
@@ -143,7 +144,11 @@ static int config_props(AVFilterLink *outlink) · 9 unmodified lines
143144 outlink->h = tile->h * inlink->h + total_margin_h;
144145 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
145146 ol->frame_rate = av_mul_q(il->frame_rate, av_make_q(1, tile->nb_frames - tile->overlap));
146− ff_draw_init2(&tile->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
147+ ret = ff_draw_init2(&tile->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
148+ if (ret < 0) {
149+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
150+ return ret;
151+ }
147152 ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
148153
149154 return 0;
libavfilter/vf_tinterlace.c+5−1
@@ -228,7 +228,11 @@ static int config_out_props(AVFilterLink *outlink) · 227 unmodified lines
228228
229229 if (tinterlace->mode == MODE_PAD) {
230230 uint8_t black[4] = { 0, 0, 0, 16 };
231− ff_draw_init2(&tinterlace->draw, outlink->format, outlink->colorspace, outlink->color_range, 0);
231+ ret = ff_draw_init2(&tinterlace->draw, outlink->format, outlink->colorspace, outlink->color_range, 0);
232+ if (ret < 0) {
233+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
234+ return ret;
235+ }
232236 ff_draw_color(&tinterlace->draw, &tinterlace->color, black);
233237 /* limited range */
234238 if (!ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts)) {
libavfilter/vf_tpad.c+6−1
@@ -206,9 +206,14 @@ static int config_input(AVFilterLink *inlink) · 205 unmodified lines
206206 AVFilterContext *ctx = inlink->dst;
207207 FilterLink *l = ff_filter_link(inlink);
208208 TPadContext *s = ctx->priv;
209+ int ret;
209210
210211 if (needs_drawing(s)) {
211− ff_draw_init2(&s->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
212+ ret = ff_draw_init2(&s->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
213+ if (ret < 0) {
214+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
215+ return ret;
216+ }
212217 ff_draw_color(&s->draw, &s->color, s->rgba_color);
213218 }
214219
libavfilter/vsrc_testsrc.c+7−2
@@ -262,8 +262,13 @@ static int color_config_props(AVFilterLink *inlink) · 261 unmodified lines
262262 TestSourceContext *test = ctx->priv;
263263 int ret;
264264
265− ff_draw_init2(&test->draw, inlink->format, inlink->colorspace,
266− inlink->color_range, 0);
265+ ret = ff_draw_init2(&test->draw, inlink->format, inlink->colorspace,
266+ inlink->color_range, 0);
267+ if (ret < 0) {
268+ av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
269+ return ret;
270+ }
271+
267272 ff_draw_color(&test->draw, &test->color, test->color_rgba);
268273
269274 if (av_image_check_size(test->w, test->h, 0, ctx) < 0)

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