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
636 return AVERROR(EINVAL);636 return AVERROR(EINVAL);
637 }637 }
638 638
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 }
640 ff_draw_color(&qr->draw, &qr->draw_foreground_color, (const uint8_t *)&qr->foreground_color);645 ff_draw_color(&qr->draw, &qr->draw_foreground_color, (const uint8_t *)&qr->foreground_color);
641 ff_draw_color(&qr->draw, &qr->draw_background_color, (const uint8_t *)&qr->background_color);646 ff_draw_color(&qr->draw, &qr->draw_background_color, (const uint8_t *)&qr->background_color);
642 647
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 }
644 ff_draw_color(&qr->draw0, &qr->draw0_background_color, (const uint8_t *)&qr->background_color);653 ff_draw_color(&qr->draw0, &qr->draw0_background_color, (const uint8_t *)&qr->background_color);
645 654
646 outlink->w = qr->rendered_padded_qrcode_width;655 outlink->w = qr->rendered_padded_qrcode_width;
@@ -734,8 +743,12 @@ static int qrencode_config_input(AVFilterLink *inlink) · 87 unmodified lines
734 743
735 qr->is_source = 0;744 qr->is_source = 0;
736 745
737 ff_draw_init2(&qr->draw, inlink->format, inlink->colorspace, inlink->color_range,746 ret = ff_draw_init2(&qr->draw, inlink->format, inlink->colorspace, inlink->color_range,
738 FF_DRAW_PROCESS_ALPHA);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 }
739 752
740 V(W) = V(main_w) = inlink->w;753 V(W) = V(main_w) = inlink->w;
741 V(H) = V(main_h) = inlink->h;754 V(H) = V(main_h) = inlink->h;
@@ -764,8 +777,12 @@ static int qrencode_config_input(AVFilterLink *inlink) · 22 unmodified lines
764 PARSE_EXPR(rendered_qrcode_width);777 PARSE_EXPR(rendered_qrcode_width);
765 PARSE_EXPR(rendered_padded_qrcode_width);778 PARSE_EXPR(rendered_padded_qrcode_width);
766 779
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,
768 FF_DRAW_PROCESS_ALPHA);781 FF_DRAW_PROCESS_ALPHA);
782 if (ret < 0) {
783 av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
784 return ret;
785 }
769 ff_draw_color(&qr->draw, &qr->draw_foreground_color, (const uint8_t *)&qr->foreground_color);786 ff_draw_color(&qr->draw, &qr->draw_foreground_color, (const uint8_t *)&qr->foreground_color);
770 ff_draw_color(&qr->draw, &qr->draw_background_color, (const uint8_t *)&qr->background_color);787 ff_draw_color(&qr->draw, &qr->draw_background_color, (const uint8_t *)&qr->background_color);
771 788
libavfilter/src_avsynctest.c+6−1
@@ -147,6 +147,7 @@ static av_cold int config_props(AVFilterLink *outlink) · 146 unmodified lines
147 FilterLink *l = ff_filter_link(outlink);147 FilterLink *l = ff_filter_link(outlink);
148 AVFilterContext *ctx = outlink->src;148 AVFilterContext *ctx = outlink->src;
149 AVSyncTestContext *s = ctx->priv;149 AVSyncTestContext *s = ctx->priv;
150 int ret;
150 151
151 outlink->w = s->w;152 outlink->w = s->w;
152 outlink->h = s->h;153 outlink->h = s->h;
@@ -160,7 +161,11 @@ static av_cold int config_props(AVFilterLink *outlink) · 7 unmodified lines
160 s->dir = 1;161 s->dir = 1;
161 s->prev_intpart = INT64_MIN;162 s->prev_intpart = INT64_MIN;
162 163
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 }
164 169
165 ff_draw_color(&s->draw, &s->fg, s->rgba[0]);170 ff_draw_color(&s->draw, &s->fg, s->rgba[0]);
166 ff_draw_color(&s->draw, &s->bg, s->rgba[1]);171 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
382 382
383static int config_input(AVFilterLink *inlink)383static int config_input(AVFilterLink *inlink)
384{384{
385 DatascopeContext *s = inlink->dst->priv;385 AVFilterContext *ctx = inlink->dst;
386 DatascopeContext *s = ctx->priv;
387
386 uint8_t alpha = s->opacity * 255;388 uint8_t alpha = s->opacity * 255;
389 int ret;
387 390
388 s->nb_planes = av_pix_fmt_count_planes(inlink->format);391 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 }
390 ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );397 ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
391 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, alpha} );398 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, alpha} );
392 ff_draw_color(&s->draw, &s->yellow, (uint8_t[]){ 255, 255, 0, 255} );399 ff_draw_color(&s->draw, &s->yellow, (uint8_t[]){ 255, 255, 0, 255} );
@@ -509,10 +516,16 @@ AVFILTER_DEFINE_CLASS(pixscope); · 116 unmodified lines
509 516
510static int pixscope_config_input(AVFilterLink *inlink)517static int pixscope_config_input(AVFilterLink *inlink)
511{518{
512 PixscopeContext *s = inlink->dst->priv;519 AVFilterContext *ctx = inlink->dst;
520 PixscopeContext *s = ctx->priv;
521 int ret;
513 522
514 s->nb_planes = av_pix_fmt_count_planes(inlink->format);523 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 }
516 ff_draw_color(&s->draw, &s->dark, (uint8_t[]){ 0, 0, 0, s->o * 255} );529 ff_draw_color(&s->draw, &s->dark, (uint8_t[]){ 0, 0, 0, s->o * 255} );
517 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );530 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
518 ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );531 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
927 940
928static int oscilloscope_config_input(AVFilterLink *inlink)941static int oscilloscope_config_input(AVFilterLink *inlink)
929{942{
930 OscilloscopeContext *s = inlink->dst->priv;943 AVFilterContext *ctx = inlink->dst;
944 OscilloscopeContext *s = ctx->priv;
931 int size;945 int size;
946 int ret;
932 947
933 s->nb_planes = av_pix_fmt_count_planes(inlink->format);948 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 }
935 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );954 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
936 ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );955 ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
937 ff_draw_color(&s->draw, &s->green, (uint8_t[]){ 0, 255, 0, 255} );956 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
1156 char *expr;1156 char *expr;
1157 int ret;1157 int ret;
1158 1158
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 }
1160 ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);1164 ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
1161 ff_draw_color(&s->dc, &s->shadowcolor, s->shadowcolor.rgba);1165 ff_draw_color(&s->dc, &s->shadowcolor, s->shadowcolor.rgba);
1162 ff_draw_color(&s->dc, &s->bordercolor, s->bordercolor.rgba);1166 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
114 double var_values[VARS_NB], res;114 double var_values[VARS_NB], res;
115 char *expr;115 char *expr;
116 116
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 }
118 ff_draw_color(&s->draw, &s->color, s->rgba_color);122 ff_draw_color(&s->draw, &s->color, s->rgba_color);
119 123
120 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;124 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
250 AVFilterContext *ctx = outlink->src;250 AVFilterContext *ctx = outlink->src;
251 ShearContext *s = ctx->priv;251 ShearContext *s = ctx->priv;
252 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);252 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
253 int ret;
253 254
254 s->nb_planes = av_pix_fmt_count_planes(outlink->format);255 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
255 s->depth = desc->comp[0].depth;256 s->depth = desc->comp[0].depth;
@@ -260,7 +261,11 @@ static int config_output(AVFilterLink *outlink) · 4 unmodified lines
260 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(ctx->inputs[0]->h, desc->log2_chroma_h);261 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(ctx->inputs[0]->h, desc->log2_chroma_h);
261 s->planeheight[0] = s->planeheight[3] = ctx->inputs[0]->h;262 s->planeheight[0] = s->planeheight[3] = ctx->inputs[0]->h;
262 263
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 }
264 ff_draw_color(&s->draw, &s->color, s->fillcolor);269 ff_draw_color(&s->draw, &s->color, s->fillcolor);
265 270
266 s->filter_slice[0] = s->depth <= 8 ? filter_slice_nn8 : filter_slice_nn16;271 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
312 312
313 if (s->fillcolor_enable) {313 if (s->fillcolor_enable) {
314 const AVFilterLink *inlink = ctx->inputs[0];314 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 }
316 ff_draw_color(&s->draw, &s->color, s->fillcolor);320 ff_draw_color(&s->draw, &s->color, s->fillcolor);
317 }321 }
318 322
libavfilter/vf_subtitles.c+11−5
@@ -182,12 +182,18 @@ static int query_formats(const AVFilterContext *ctx, · 181 unmodified lines
182 182
183static int config_input(AVFilterLink *inlink)183static int config_input(AVFilterLink *inlink)
184{184{
185 AssContext *ass = inlink->dst->priv;185 AVFilterContext *ctx = inlink->dst;
186 AssContext *ass = ctx->priv;
187 int ret;
186 188
187 ff_draw_init2(&ass->draw, inlink->format,189 ret = ff_draw_init2(&ass->draw, inlink->format,
188 ass_get_color_space(ass->track->YCbCrMatrix, inlink->colorspace),190 ass_get_color_space(ass->track->YCbCrMatrix, inlink->colorspace),
189 ass_get_color_range(ass->track->YCbCrMatrix, inlink->color_range),191 ass_get_color_range(ass->track->YCbCrMatrix, inlink->color_range),
190 ass->alpha ? FF_DRAW_PROCESS_ALPHA : 0);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 }
191 197
192 ass_set_frame_size (ass->renderer, inlink->w, inlink->h);198 ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
193 if (ass->original_w && ass->original_h) {199 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
128 FilterLink *ol = ff_filter_link(outlink);128 FilterLink *ol = ff_filter_link(outlink);
129 const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;129 const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
130 const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;130 const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
131 int ret;
131 132
132 if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {133 if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
133 av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",134 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
143 outlink->h = tile->h * inlink->h + total_margin_h;144 outlink->h = tile->h * inlink->h + total_margin_h;
144 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;145 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
145 ol->frame_rate = av_mul_q(il->frame_rate, av_make_q(1, tile->nb_frames - tile->overlap));146 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 }
147 ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);152 ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
148 153
149 return 0;154 return 0;
libavfilter/vf_tinterlace.c+5−1
@@ -228,7 +228,11 @@ static int config_out_props(AVFilterLink *outlink) · 227 unmodified lines
228 228
229 if (tinterlace->mode == MODE_PAD) {229 if (tinterlace->mode == MODE_PAD) {
230 uint8_t black[4] = { 0, 0, 0, 16 };230 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 }
232 ff_draw_color(&tinterlace->draw, &tinterlace->color, black);236 ff_draw_color(&tinterlace->draw, &tinterlace->color, black);
233 /* limited range */237 /* limited range */
234 if (!ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts)) {238 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
206 AVFilterContext *ctx = inlink->dst;206 AVFilterContext *ctx = inlink->dst;
207 FilterLink *l = ff_filter_link(inlink);207 FilterLink *l = ff_filter_link(inlink);
208 TPadContext *s = ctx->priv;208 TPadContext *s = ctx->priv;
209 int ret;
209 210
210 if (needs_drawing(s)) {211 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 }
212 ff_draw_color(&s->draw, &s->color, s->rgba_color);217 ff_draw_color(&s->draw, &s->color, s->rgba_color);
213 }218 }
214 219
libavfilter/vsrc_testsrc.c+7−2
@@ -262,8 +262,13 @@ static int color_config_props(AVFilterLink *inlink) · 261 unmodified lines
262 TestSourceContext *test = ctx->priv;262 TestSourceContext *test = ctx->priv;
263 int ret;263 int ret;
264 264
265 ff_draw_init2(&test->draw, inlink->format, inlink->colorspace,265 ret = ff_draw_init2(&test->draw, inlink->format, inlink->colorspace,
266 inlink->color_range, 0);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
267 ff_draw_color(&test->draw, &test->color, test->color_rgba);272 ff_draw_color(&test->draw, &test->color, test->color_rgba);
268 273
269 if (av_image_check_size(test->w, test->h, 0, ctx) < 0)274 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