diff mbox series

[FFmpeg-devel] avcodec/decode: use avcodec_get_hw_config()

Message ID 20240602013818.1047-1-kasper93@gmail.com
State New
Headers show
Series [FFmpeg-devel] avcodec/decode: use avcodec_get_hw_config() | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Kacper Michajlow June 2, 2024, 1:38 a.m. UTC
Fixes libavcodec/decode.c:1035:61: runtime error: member access within
null pointer of type 'const struct AVCodecHWConfigInternal'.

This can happen when hwaccel fails to initialize and hw_configs[i] is
NULL.

Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
---
 libavcodec/decode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Anton Khirnov July 1, 2024, 10:16 a.m. UTC | #1
Quoting Kacper Michajłow (2024-06-02 03:38:18)
> Fixes libavcodec/decode.c:1035:61: runtime error: member access within
> null pointer of type 'const struct AVCodecHWConfigInternal'.
> 
> This can happen when hwaccel fails to initialize and hw_configs[i] is
> NULL.

How? All this should be runtime-constant.
Kacper Michajlow July 8, 2024, 2:42 p.m. UTC | #2
On Mon, 1 Jul 2024 at 12:16, Anton Khirnov <anton@khirnov.net> wrote:
>
> Quoting Kacper Michajłow (2024-06-02 03:38:18)
> > Fixes libavcodec/decode.c:1035:61: runtime error: member access within
> > null pointer of type 'const struct AVCodecHWConfigInternal'.
> >
> > This can happen when hwaccel fails to initialize and hw_configs[i] is
> > NULL.
>
> How? All this should be runtime-constant.

As we can read in hw_configs description.

> Array of pointers to hardware configurations supported by the codec, or NULL if no hardware supported.
> The array is terminated by a NULL pointer.

It uses for (i = 0;; i++), so it always gets the `public` address.
Since public is the first member of `AVCodecHWConfigInternal`, it
silently works; the offset of `public` is zero, so it remains NULL,
and the check in the next line rejects it. Nevertheless, it is better
to check it correctly as avcodec_get_hw_config() does.

The "fails to initialize" part in the commit message is misleading. I
think I just triggered this part of the code on some fallback path,
but the value of hw_configs[i] is not affected by this.

- Kacper
diff mbox series

Patch

diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 791940648d..ced4ff3421 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -1032,7 +1032,7 @@  enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
         AVHWDeviceContext *device_ctx =
             (AVHWDeviceContext*)avctx->hw_device_ctx->data;
         for (i = 0;; i++) {
-            config = &ffcodec(avctx->codec)->hw_configs[i]->public;
+            config = avcodec_get_hw_config(avctx->codec, i);
             if (!config)
                 break;
             if (!(config->methods &