diff mbox series

[FFmpeg-devel,5/7] fftools/ffmpeg_demux: Don't use fake object with av_opt_eval

Message ID AS8P250MB07447F0DA55E7B9C5020244E8FC8A@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit d98dfcecad179260182d90ed1d170c1037db7568
Headers show
Series [FFmpeg-devel,1/7] avcodec/wmv2dec: Parse extradata during init | expand

Commit Message

Andreas Rheinhardt Oct. 7, 2023, 12:40 a.m. UTC
The av_opt_eval family of functions emits errors messages on error
and can therefore not be used with fake objects when the AVClass
has a custom item_name callback. The AVClass for AVCodecContext
has such a custom callback (it searches whether an AVCodec is set
to use its name). In practice it means that whatever is directly
after the "cc" pointer to the AVClass for AVCodec in the stack frame
of ist_add() will be treated as a pointer to an AVCodec with
unpredictable consequences.

Fix this by using an actual AVCodecContext instead of a fake object.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 fftools/ffmpeg_demux.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

Comments

Anton Khirnov Oct. 10, 2023, 11:48 a.m. UTC | #1
Quoting Andreas Rheinhardt (2023-10-07 02:40:29)
> The av_opt_eval family of functions emits errors messages on error
> and can therefore not be used with fake objects when the AVClass
> has a custom item_name callback. The AVClass for AVCodecContext
> has such a custom callback (it searches whether an AVCodec is set
> to use its name). In practice it means that whatever is directly
> after the "cc" pointer to the AVClass for AVCodec in the stack frame
> of ist_add() will be treated as a pointer to an AVCodec with
> unpredictable consequences.
> 
> Fix this by using an actual AVCodecContext instead of a fake object.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  fftools/ffmpeg_demux.c | 15 +++++++--------
>  1 file changed, 7 insertions(+), 8 deletions(-)

Ok
diff mbox series

Patch

diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index c71edf01a5..41fcb678c6 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -1042,9 +1042,6 @@  static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
     char *codec_tag = NULL;
     char *next;
     char *discard_str = NULL;
-    const AVClass *cc = avcodec_get_class();
-    const AVOption *discard_opt = av_opt_find(&cc, "skip_frame", NULL,
-                                              0, AV_OPT_SEARCH_FAKE_OBJ);
     int ret;
 
     ds  = demux_stream_alloc(d, st);
@@ -1176,18 +1173,20 @@  static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
         (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
             ist->user_set_discard = AVDISCARD_ALL;
 
+    ist->dec_ctx = avcodec_alloc_context3(ist->dec);
+    if (!ist->dec_ctx)
+        return AVERROR(ENOMEM);
+
     if (discard_str) {
-        ret = av_opt_eval_int(&cc, discard_opt, discard_str, &ist->user_set_discard);
+        const AVOption *discard_opt = av_opt_find(ist->dec_ctx, "skip_frame",
+                                                  NULL, 0, 0);
+        ret = av_opt_eval_int(ist->dec_ctx, discard_opt, discard_str, &ist->user_set_discard);
         if (ret  < 0) {
             av_log(ist, AV_LOG_ERROR, "Error parsing discard %s.\n", discard_str);
             return ret;
         }
     }
 
-    ist->dec_ctx = avcodec_alloc_context3(ist->dec);
-    if (!ist->dec_ctx)
-        return AVERROR(ENOMEM);
-
     ret = avcodec_parameters_to_context(ist->dec_ctx, par);
     if (ret < 0) {
         av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");