PR #26054 opened by Marti2203
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26054
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26054.patch
For formats with more than 8 bits, the histogram has `1 << depth` entries, but
`filter_frame()` indexes it with the raw 16-bit sample, so a sample with bits
set above the depth increments `int64_t` entries past the 512-entry histogram:
ffmpeg -filter_complex
"nullsrc=s=64x32:r=5:d=2,format=gray9,geq=lum='if(lt(X,8),X*7+Y,65535-Y)',entropy,metadata=mode=print"
-f null -
gives an ASan heap-buffer-overflow in `filter_frame()` for gray9–gray12, in
both modes. For gray14 the writes land beyond the redzone, so there is no ASan
report, but the heap is still corrupted and the reported entropy changes.
The patch clips the sample to the last entry before using it as an index, with
an `unsigned` bound as in #25902. In-depth input is not affected: the
`lavfi.entropy.*` metadata is identical before and after for gray9–gray16 in
both modes, and the out-of-depth input now runs without an ASan report.
This is the `entropy` case pointed out by Forgejo_Fairy in the review of #25902.
From bedbaf67ba5ce4a057faf2825da5ce6a870131cc Mon Sep 17 00:00:00 2001
From: Martin Mirchev <[email protected]>
Date: Sat, 14 Nov 2026 14:11:55 +0800
Subject: [PATCH] avfilter/vf_entropy: clip out-of-depth samples to the
histogram
For formats with more than 8 bits, the histogram has 1 << depth
entries, but filter_frame() indexes it with the raw 16-bit sample. A
sample with bits set above the depth, e.g. from a gray9le rawvideo
input with arbitrary words, increments entries past the 512-entry
histogram.
Clip the sample to the last entry before using it as an index, as
vf_midequalizer does.
Signed-off-by: Martin Mirchev <[email protected]>
---
libavfilter/vf_entropy.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/libavfilter/vf_entropy.c b/libavfilter/vf_entropy.c
index 1d13bb484b9..d4a5f4836ad 100644
--- a/libavfilter/vf_entropy.c
+++ b/libavfilter/vf_entropy.c
@@ -125,9 +125,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
src8 += in->linesize[plane];
}
} else {
+ const unsigned max = (1U << s->depth) - 1;
+
for (y = 0; y < s->planeheight[plane]; y++) {
for (x = 0; x < s->planewidth[plane]; x++) {
- s->histogram[src16[x]]++;
+ s->histogram[FFMIN(src16[x], max)]++;
}
src16 += in->linesize[plane] / 2;
--
2.54.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]