From 56d861ebe0353df4b3a12a51dc1f103e12292f34 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Thu, 27 Apr 2023 11:33:08 +0200 Subject: [PATCH] AK: Prevent bit counter underflows in the new BitStream Our current `peek_bits` function allows retrieving more bits than we can actually provide, so whenever someone discards the requested bit count afterwards we were underflowing the value instead. --- AK/BitStream.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/AK/BitStream.h b/AK/BitStream.h index 3ddca57267..c03ac22ada 100644 --- a/AK/BitStream.h +++ b/AK/BitStream.h @@ -222,6 +222,10 @@ public: ALWAYS_INLINE void discard_previously_peeked_bits(u8 count) { + // We allow "retrieving" more bits than we can provide, but we need to make sure that we don't underflow the current bit counter. + if (count > m_bit_count) + count = m_bit_count; + m_bit_offset += count; m_bit_count -= count; }