PR #26391 opened by JuliusBairaktaris
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26391
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26391.patch

`amf_device_create()` returned the AMF_RESULT of a failed `CreateContext()`. 
AMF error codes are positive, and `av_hwdevice_ctx_create()` and 
`amf_device_derive()` only treat negative values as errors, so both went on to 
initialize a device whose context is NULL. The function also called 
`amf_device_uninit()` itself, and the callers then free the device and call it 
a second time, with `factory` pointing into the unloaded runtime and the trace 
writer already freed.

This returns `AVERROR_EXTERNAL` and leaves the cleanup to the single 
`amf_device_uninit()` call that frees the device, as the Forgejo_Fairy review 
of #25458 proposed for this pre-existing bug.

Tested on Windows 11, RX 9070 XT, driver 32.0.32029.2010, with a build that 
makes `CreateContext()` fail: `-init_hw_device amf` and an AMF device derived 
from D3D11VA crashed with an access violation before, and now fail with 
"Generic error in an external library". Without the fault, both device setups 
work as before.


>From 22f390a4a93c4b57c9d71f56eac920760cf3de5d Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Sun, 22 Nov 2026 21:51:56 +0100
Subject: [PATCH] avutil/hwcontext_amf: return an AVERROR when CreateContext()
 fails

amf_device_create() returned the AMF_RESULT of a failed CreateContext().
AMF error codes are positive, while av_hwdevice_ctx_create() and
amf_device_derive() only treat negative values as errors, so both went on
to initialize a device whose context is NULL and dereferenced it.

The function also called amf_device_uninit() before returning. The
callers free the device on failure, which calls amf_device_uninit() a
second time, with the factory still pointing into the unloaded runtime
and the trace writer already freed.

Return AVERROR_EXTERNAL and leave the cleanup to the one amf_device_uninit()
call that frees the device.

Tested on Windows 11, RX 9070 XT, driver 32.0.32029.2010, with a build
that makes CreateContext() fail: "-init_hw_device amf" and an AMF device
derived from a D3D11VA device crashed with an access violation before,
and now fail with "Generic error in an external library".

Reported by the Forgejo_Fairy review of #25458.

Signed-off-by: Julius Bairaktaris <[email protected]>
---
 libavutil/hwcontext_amf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavutil/hwcontext_amf.c b/libavutil/hwcontext_amf.c
index 7709614ebde..aa8137c1d11 100644
--- a/libavutil/hwcontext_amf.c
+++ b/libavutil/hwcontext_amf.c
@@ -800,8 +800,8 @@ static int amf_device_create(AVHWDeviceContext *device_ctx,
             return 0;
         }
         av_log(device_ctx, AV_LOG_ERROR, "CreateContext() failed with error 
%d.\n", ret);
+        ret = AVERROR_EXTERNAL;
     }
-    amf_device_uninit(device_ctx);
     return ret;
 }
 
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to