Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Alexander Wichers via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: "ffmpeg-devel@ffmpeg.org" <ffmpeg-devel@ffmpeg.org>
Cc: Alexander Wichers <sander.wichers@gmail.com>
Subject: [FFmpeg-devel] [PATCH] avcodec/dstdec: fix arithmetic coder read past end of stream
Date: Tue, 10 Feb 2026 08:55:21 +0000
Message-ID: <AM0P194MB06909CC9210C0338240E17DAF962A@AM0P194MB0690.EURP194.PROD.OUTLOOK.COM> (raw)

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

                 reply	other threads:[~2026-02-11 14:59 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AM0P194MB06909CC9210C0338240E17DAF962A@AM0P194MB0690.EURP194.PROD.OUTLOOK.COM \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=sander.wichers@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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