Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avformat/demux: Fix segfault due to avcodec_open2 failure
@ 2025-06-10  2:31 Pavel Koshevoy
  2025-06-10  2:58 ` Andreas Rheinhardt
  0 siblings, 1 reply; 3+ messages in thread
From: Pavel Koshevoy @ 2025-06-10  2:31 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Pavel Koshevoy

Fixes 'ffprobe 1_poc.mp4' segfault introduced with
commit 0021484d05f9b0f032fa319399de6e24eea0c04f
---
 libavformat/demux.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/libavformat/demux.c b/libavformat/demux.c
index ecd4f40da9..d74d51e169 100644
--- a/libavformat/demux.c
+++ b/libavformat/demux.c
@@ -2078,8 +2078,8 @@ static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
 static int try_decode_frame(AVFormatContext *s, AVStream *st,
                             const AVPacket *pkt, AVDictionary **options)
 {
-    FFStream *const sti = ffstream(st);
-    AVCodecContext *const avctx = sti->avctx;
+    FFStream *sti = ffstream(st);
+    AVCodecContext *avctx = sti->avctx;
     const AVCodec *codec;
     int got_picture = 1, ret = 0;
     AVFrame *frame = av_frame_alloc();
@@ -2104,6 +2104,12 @@ static int try_decode_frame(AVFormatContext *s, AVStream *st,
             goto fail;
         }
 
+        if (avctx && avctx->codec != codec) {
+          avcodec_free_context(&avctx);
+          avctx = avcodec_alloc_context3(codec);
+          sti->avctx = avctx;
+        }
+
         /* Force thread count to 1 since the H.264 decoder will not extract
          * SPS and PPS to extradata during multi-threaded decoding. */
         av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
-- 
2.43.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".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [FFmpeg-devel] [PATCH] avformat/demux: Fix segfault due to avcodec_open2 failure
  2025-06-10  2:31 [FFmpeg-devel] [PATCH] avformat/demux: Fix segfault due to avcodec_open2 failure Pavel Koshevoy
@ 2025-06-10  2:58 ` Andreas Rheinhardt
  2025-06-10  3:40   ` Pavel Koshevoy
  0 siblings, 1 reply; 3+ messages in thread
From: Andreas Rheinhardt @ 2025-06-10  2:58 UTC (permalink / raw)
  To: ffmpeg-devel

Pavel Koshevoy:
> Fixes 'ffprobe 1_poc.mp4' segfault introduced with
> commit 0021484d05f9b0f032fa319399de6e24eea0c04f
> ---
>  libavformat/demux.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/libavformat/demux.c b/libavformat/demux.c
> index ecd4f40da9..d74d51e169 100644
> --- a/libavformat/demux.c
> +++ b/libavformat/demux.c
> @@ -2078,8 +2078,8 @@ static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
>  static int try_decode_frame(AVFormatContext *s, AVStream *st,
>                              const AVPacket *pkt, AVDictionary **options)
>  {
> -    FFStream *const sti = ffstream(st);
> -    AVCodecContext *const avctx = sti->avctx;
> +    FFStream *sti = ffstream(st);
> +    AVCodecContext *avctx = sti->avctx;
>      const AVCodec *codec;
>      int got_picture = 1, ret = 0;
>      AVFrame *frame = av_frame_alloc();
> @@ -2104,6 +2104,12 @@ static int try_decode_frame(AVFormatContext *s, AVStream *st,
>              goto fail;
>          }
>  
> +        if (avctx && avctx->codec != codec) {
> +          avcodec_free_context(&avctx);
> +          avctx = avcodec_alloc_context3(codec);
> +          sti->avctx = avctx;
> +        }
> +
>          /* Force thread count to 1 since the H.264 decoder will not extract
>           * SPS and PPS to extradata during multi-threaded decoding. */
>          av_dict_set(options ? options : &thread_opt, "threads", "1", 0);

1. Unchecked allocation.
2. This AVCodecContext will be mostly blank; it does not inherit
available extradata and other parameters. See codec_close().

- Andreas

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [FFmpeg-devel] [PATCH] avformat/demux: Fix segfault due to avcodec_open2 failure
  2025-06-10  2:58 ` Andreas Rheinhardt
@ 2025-06-10  3:40   ` Pavel Koshevoy
  0 siblings, 0 replies; 3+ messages in thread
From: Pavel Koshevoy @ 2025-06-10  3:40 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

On Mon, Jun 9, 2025 at 8:58 PM Andreas Rheinhardt <
andreas.rheinhardt@outlook.com> wrote:

> Pavel Koshevoy:
> > Fixes 'ffprobe 1_poc.mp4' segfault introduced with
> > commit 0021484d05f9b0f032fa319399de6e24eea0c04f
> > ---
> >  libavformat/demux.c | 10 ++++++++--
> >  1 file changed, 8 insertions(+), 2 deletions(-)
> >
> > diff --git a/libavformat/demux.c b/libavformat/demux.c
> > index ecd4f40da9..d74d51e169 100644
> > --- a/libavformat/demux.c
> > +++ b/libavformat/demux.c
> > @@ -2078,8 +2078,8 @@ static int has_codec_parameters(const AVStream
> *st, const char **errmsg_ptr)
> >  static int try_decode_frame(AVFormatContext *s, AVStream *st,
> >                              const AVPacket *pkt, AVDictionary **options)
> >  {
> > -    FFStream *const sti = ffstream(st);
> > -    AVCodecContext *const avctx = sti->avctx;
> > +    FFStream *sti = ffstream(st);
> > +    AVCodecContext *avctx = sti->avctx;
> >      const AVCodec *codec;
> >      int got_picture = 1, ret = 0;
> >      AVFrame *frame = av_frame_alloc();
> > @@ -2104,6 +2104,12 @@ static int try_decode_frame(AVFormatContext *s,
> AVStream *st,
> >              goto fail;
> >          }
> >
> > +        if (avctx && avctx->codec != codec) {
> > +          avcodec_free_context(&avctx);
> > +          avctx = avcodec_alloc_context3(codec);
> > +          sti->avctx = avctx;
> > +        }
> > +
> >          /* Force thread count to 1 since the H.264 decoder will not
> extract
> >           * SPS and PPS to extradata during multi-threaded decoding. */
> >          av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
>
> 1. Unchecked allocation.
> 2. This AVCodecContext will be mostly blank; it does not inherit
> available extradata and other parameters. See codec_close().
>
>
I'll be submitting a v2 of the patch that modifies codec_close instead ...
this one broke a lot of fate tests.

Thank you,
    Pavel
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-06-10  3:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-10  2:31 [FFmpeg-devel] [PATCH] avformat/demux: Fix segfault due to avcodec_open2 failure Pavel Koshevoy
2025-06-10  2:58 ` Andreas Rheinhardt
2025-06-10  3:40   ` Pavel Koshevoy

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git