Skip to content

Commit

Permalink
Fix potential incorrect shift value at EndOfStream
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Jun 20, 2024
1 parent d5b6f5c commit 2e2221b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions v2/bitstream/DefaultInputBitStream.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ func (this *DefaultInputBitStream) ReadBits(count uint) uint64 {
count -= this.availBits
res := this.current & (0xFFFFFFFFFFFFFFFF >> (64 - this.availBits))
this.pullCurrent()
this.availBits -= count
return (res << count) | (this.current >> this.availBits)
return (res << count) | this.ReadBits(count) // handle this.availBits < count (at EOS)
}

// ReadArray reads 'count' bits from the stream and returns them to the 'bits'
Expand Down Expand Up @@ -154,6 +153,11 @@ func (this *DefaultInputBitStream) ReadArray(bits []byte, count uint) uint {

if this.position+32 > this.maxPosition {
this.pullCurrent()

if this.availBits < r {
panic("No more data to read in the bitstream")
}

this.availBits -= r
binary.BigEndian.PutUint64(bits[start:start+8], (v0<<r)|(this.current>>uint(this.availBits)))
start += 8
Expand All @@ -178,6 +182,11 @@ func (this *DefaultInputBitStream) ReadArray(bits []byte, count uint) uint {
for remaining >= 64 {
v := this.current
this.pullCurrent()

if this.availBits < r {
panic("No more data to read in the bitstream")
}

this.availBits -= r
binary.BigEndian.PutUint64(bits[start:start+8], (v<<r)|(this.current>>uint(this.availBits)))
start += 8
Expand Down

0 comments on commit 2e2221b

Please sign in to comment.