Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avcodec/dstdec: fix arithmetic coder read past end of stream
@ 2026-02-10  8:55 Alexander Wichers via ffmpeg-devel
  0 siblings, 0 replies; only message in thread
From: Alexander Wichers via ffmpeg-devel @ 2026-02-10  8:55 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Alexander Wichers

The ac_get() renormalization reads n bits from the bitstream to refill
the arithmetic coder state, but does not check whether enough bits
remain. When decoding near the end of the arithmetic coded data section,
this causes reads past the AC data boundary into trailing frame data,
producing corrupted output in the last bytes of the decoded DSD frame.

Add a bounds check using get_bits_left() before reading. When fewer
than n bits remain, shift in zeros for the missing bits, matching the
behavior of the ISO/IEC 14496-3 reference implementation.

Signed-off-by: Alexander Wichers <sander.wichers@gmail.com>
---
 libavcodec/dstdec.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/libavcodec/dstdec.c b/libavcodec/dstdec.c
index cfb34b7b3c..e37d830ae4 100644
--- a/libavcodec/dstdec.c
+++ b/libavcodec/dstdec.c
@@ -205,8 +205,15 @@ static av_always_inline void ac_get(ArithCoder *ac, GetBitContext *gb, int p, in
 
     if (ac->a < 2048) {
         int n = 11 - av_log2(ac->a);
+        int left = get_bits_left(gb);
         ac->a <<= n;
-        ac->c = (ac->c << n) | get_bits(gb, n);
+        if (left >= n) {
+            ac->c = (ac->c << n) | get_bits(gb, n);
+        } else {
+            ac->c <<= n;
+            if (left > 0)
+                ac->c |= get_bits(gb, left) << (n - left);
+        }
     }
 }
 
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-02-11 14:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-10  8:55 [FFmpeg-devel] [PATCH] avcodec/dstdec: fix arithmetic coder read past end of stream Alexander Wichers via ffmpeg-devel

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git