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

Fixes #26354.

`val()` converted its double channel argument to `int` before checking its 
range, triggering float-cast-overflow for NaN, infinities, and large values. 
Its subsequent unsigned cast also mapped negative indices to the last input 
channel.

Handle NaN and negative values as channel 0, clamp positive values to the final 
input channel, and convert only after the value is in range. This preserves 
truncation of finite in-range indices. Document the behavior and add a 
generated FATE case covering negative, NaN, infinite, oversized, and fractional 
indices.

Validation: the new FATE case passes on x86-64 with Clang UBSan and on AArch64 
with a normal build. Direct x86-64 runs with `UBSAN_OPTIONS=halt_on_error=1` 
also cover `val(val(0))` with a NaN input sample; no sanitizer error occurs.


>From ebbd46b945acdd7face0d05e2adf5ba754bda415 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Fri, 27 Nov 2026 17:04:14 +0000
Subject: [PATCH] avfilter/aeval: clip val channel before integer conversion

Handle NaN and negative channel values as the first input channel, and clamp 
large values to the last channel before converting to int. This avoids 
float-cast-overflow and negative-index wraparound while preserving truncation 
for finite in-range values.

Add a synthetic FATE case covering negative, NaN, infinite, oversized, and 
fractional indices.

Assisted-by: Fairy
---
 doc/filters.texi                | 4 +++-
 libavfilter/aeval.c             | 7 ++++++-
 tests/fate/filter-audio.mak     | 3 +++
 tests/ref/fate/filter-aeval-val | 6 ++++++
 4 files changed, 18 insertions(+), 2 deletions(-)
 create mode 100644 tests/ref/fate/filter-aeval-val

diff --git a/doc/filters.texi b/doc/filters.texi
index 3a516bc1cbb..e0be397fe76 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -1358,7 +1358,9 @@ time of the evaluated sample expressed in seconds
 input and output number of channels
 
 @item val(CH)
-the value of input channel with number @var{CH}
+the value of input channel with number @var{CH}. The channel number is 
truncated
+toward zero and clipped to the available input channels. NaN selects the first
+channel.
 @end table
 
 Note: this filter is slow. For faster processing you should use a
diff --git a/libavfilter/aeval.c b/libavfilter/aeval.c
index e88cb331ff1..66959b57e1e 100644
--- a/libavfilter/aeval.c
+++ b/libavfilter/aeval.c
@@ -23,6 +23,8 @@
  * eval audio source
  */
 
+#include <math.h>
+
 #include "config_components.h"
 
 #include "libavutil/avstring.h"
@@ -77,7 +79,10 @@ typedef struct EvalContext {
 static double val(void *priv, double ch)
 {
     EvalContext *eval = priv;
-    return eval->channel_values[FFMIN((unsigned)(int)ch, 
eval->nb_in_channels-1)];
+
+    if (isnan(ch) || ch < 0)
+        ch = 0;
+    return eval->channel_values[(int)FFMIN(ch, eval->nb_in_channels - 1)];
 }
 
 static double (* const aeval_func1[])(void *, double) = { val, NULL };
diff --git a/tests/fate/filter-audio.mak b/tests/fate/filter-audio.mak
index dd709d51a81..6284421c583 100644
--- a/tests/fate/filter-audio.mak
+++ b/tests/fate/filter-audio.mak
@@ -8,6 +8,9 @@ fate-filter-aecho: tests/data/asynth-44100-2.wav
 fate-filter-aecho: SRC = $(TARGET_PATH)/tests/data/asynth-44100-2.wav
 fate-filter-aecho: CMD = framecrc -i $(SRC) -af 
aresample,aecho=0.5:0.5:32:0.5,aresample
 
+FATE_AFILTER-$(call ALLYES, LAVFI_INDEV AEVALSRC_FILTER AEVAL_FILTER 
PCM_F64LE_DECODER PCM_F64LE_ENCODER FRAMECRC_MUXER PIPE_PROTOCOL) += 
fate-filter-aeval-val
+fate-filter-aeval-val: CMD = framecrc -f lavfi -i 
"aevalsrc=exprs=10|20:s=1000:n=2,aeval=exprs=val(-1)|val(NAN)|val(INF)|val(-INF)|val(1e20)|val(-1e20)|val(0.9)|val(1.9):c=8c"
 -frames:a 1 -c:a pcm_f64le
+
 FATE_FILTER_AEMPHASIS += fate-filter-aemphasis-50fm
 fate-filter-aemphasis-50fm: tests/data/asynth-44100-2.wav
 fate-filter-aemphasis-50fm: SRC = $(TARGET_PATH)/tests/data/asynth-44100-2.wav
diff --git a/tests/ref/fate/filter-aeval-val b/tests/ref/fate/filter-aeval-val
new file mode 100644
index 00000000000..bc4bfdb15d4
--- /dev/null
+++ b/tests/ref/fate/filter-aeval-val
@@ -0,0 +1,6 @@
+#tb 0: 1/1000
+#media_type 0: audio
+#codec_id 0: pcm_f64le
+#sample_rate 0: 1000
+#channel_layout_name 0: 7.1
+0,          0,          0,        2,      128, 0x944f06a0
-- 
2.54.1

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

Reply via email to