commit 036ffa12b673dc48d9ca79e277814e2b24f98bb2
Author: Michael Niedermayer <michael@niedermayer.cc>
Date: Thu Nov 12 01:30:41 2026 +0100
avfilter/af_adelay: fix the resize of the delay line
The size of the new delay line could overflow, a shrink could leave
the read position beyond the new delay, which made the next increase
move a negative number of samples, and a change during the initial
fill dropped or overwrote stored samples.
Fixes: out of array write
Fixes: negative size memmove
Fixes: use of uninitialized memory
Fixes: juZLhjrRs7X8
Found during the triage of a finding from the review of https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800
Out of array write and negative size memmove Replicated through UnModified FFmpeg with ASAN
Use of uninitialized memory Replicated through UnModified FFmpeg with MSAN
diff --git a/libavfilter/af_adelay.c b/libavfilter/af_adelay.c
index 0ce65e7b9b..24e390e0bc 100644
--- a/libavfilter/af_adelay.c
+++ b/libavfilter/af_adelay.c
@@ -118,17 +118,22 @@ static int resize_samples_## name ##p(ChanDelay *d, int64_t new_delay)
return 0; \
} \
\
+ if (new_delay > SIZE_MAX / sizeof(type)) { \
+ return AVERROR(ENOMEM); \
+ } \
+ \
samples = (type *) av_fast_realloc(d->samples, &d->samples_size, new_delay * sizeof(type)); \
if (!samples) { \
return AVERROR(ENOMEM); \
} \
\
if (new_delay < d->delay) { \
- if (d->index > new_delay) { \
- d->index -= new_delay; \
- memmove(samples, &samples[new_delay], d->index * sizeof(type)); \
+ const size_t newest_end = d->delay_index < d->delay ? d->delay_index : d->index; \
+ if (newest_end >= new_delay) { \
+ memmove(samples, &samples[newest_end - new_delay], new_delay * sizeof(type)); \
+ d->index = 0; \
d->delay_index = new_delay; \
- } else if (d->delay_index > d->index) { \
+ } else if (d->delay_index >= d->delay) { \
memmove(&samples[d->index], &samples[d->index+(d->delay-new_delay)], \
(new_delay - d->index) * sizeof(type)); \
d->delay_index -= d->delay - new_delay; \
@@ -139,11 +144,9 @@ static int resize_samples_## name ##p(ChanDelay *d, int64_t new_delay)
block_size = (d->delay - d->index) * sizeof(type); \
memmove(&samples[d->index+(new_delay - d->delay)], &samples[d->index], block_size); \
d->delay_index = new_delay; \
- } else { \
- d->delay_index += new_delay - d->delay; \
+ block_size = (new_delay - d->delay) * sizeof(type); \
+ memset(&samples[d->index], fill, block_size); \
} \
- block_size = (new_delay - d->delay) * sizeof(type); \
- memset(&samples[d->index], fill, block_size); \
} \
d->delay = new_delay; \
d->samples = (void *) samples; \