bitstream: Avoid undefined behavior in bitstream_skip()

Message ID 20170709105628.5420A5DAD3@aruru.libav.org
State New
Headers show

Commit Message

Janne Grunau July 9, 2017, 10:56 a.m.
Module: libav
Branch: master
Commit: 79f64f7ebc2b2b7030c6219914bc141cc03fd377

Author:    Luca Barbato <lu_zero@gentoo.org>
Committer: Luca Barbato <lu_zero@gentoo.org>
Date:      Sat Jul  8 21:04:44 2017 +0200

bitstream: Avoid undefined behavior in bitstream_skip()

Do not use skip_remaining() to fully wipe the cache, as this could do
a 64-bit shift of a 64-bit variable which is undefined behavior in C.
Instead set the related variables to zero directly.

Thanks to Uoti for pointing out the problem.

CC: libav-stable@libav.org

---

 libavcodec/bitstream.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Patch

diff --git a/libavcodec/bitstream.h b/libavcodec/bitstream.h
index d6dd2b9..1b23cb2 100644
--- a/libavcodec/bitstream.h
+++ b/libavcodec/bitstream.h
@@ -239,11 +239,13 @@  static inline void skip_remaining(BitstreamContext *bc, unsigned n)
 /* Skip n bits in the buffer. */
 static inline void bitstream_skip(BitstreamContext *bc, unsigned n)
 {
-    if (n <= bc->bits_left)
+    if (n < bc->bits_left)
         skip_remaining(bc, n);
     else {
         n -= bc->bits_left;
-        skip_remaining(bc, bc->bits_left);
+        bc->bits      = 0;
+        bc->bits_left = 0;
+
         if (n >= 64) {
             unsigned skip = n / 8;