Skip to content

Commit

Permalink
Fix avifROStreamSkipBits bug for incomplete byte
Browse files Browse the repository at this point in the history
Fix a bug in avifROStreamSkipBits if
bitCount < 8 - stream->numUsedBitsInPartialByte.

Bug: b:376733153
  • Loading branch information
wantehchang committed Nov 1, 2024
1 parent 99f7eeb commit 2c26b29
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ avifBool avifROStreamSkipBits(avifROStream * stream, size_t bitCount)
const size_t padding = AVIF_MIN(8 - stream->numUsedBitsInPartialByte, bitCount);
stream->numUsedBitsInPartialByte = (stream->numUsedBitsInPartialByte + padding) % 8;
bitCount -= padding;
if (stream->numUsedBitsInPartialByte != 0) {
return AVIF_TRUE;
}
}
const size_t num_bytes = (bitCount + 7) / 8;
AVIF_CHECK(avifROStreamSkip(stream, num_bytes));
Expand Down
26 changes: 26 additions & 0 deletions tests/gtest/avifstreamtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,32 @@ TEST(StreamTest, Roundtrip) {
EXPECT_FALSE(avifROStreamSkip(&ro_stream, /*byteCount=*/1));
}

TEST(StreamTest, SkipBits) {
const uint8_t data[40] = {};
avifROData ro_data = {data, sizeof(data)};
avifDiagnostics diag;
avifDiagnosticsClearError(&diag);
avifROStream ro_stream;
avifROStreamStart(&ro_stream, &ro_data, &diag, "diagContext");

EXPECT_TRUE(avifROStreamSkip(&ro_stream, 32));
EXPECT_EQ(avifROStreamOffset(&ro_stream), 32);
EXPECT_EQ(ro_stream.numUsedBitsInPartialByte, 0);

uint32_t unused;
EXPECT_TRUE(avifROStreamReadBitsU32(&ro_stream, &unused, 5));
EXPECT_EQ(avifROStreamOffset(&ro_stream), 33);
EXPECT_EQ(ro_stream.numUsedBitsInPartialByte, 5);

EXPECT_TRUE(avifROStreamSkipBits(&ro_stream, 1));
EXPECT_EQ(avifROStreamOffset(&ro_stream), 33);
EXPECT_EQ(ro_stream.numUsedBitsInPartialByte, 6);

EXPECT_TRUE(avifROStreamSkipBits(&ro_stream, 2));
EXPECT_EQ(avifROStreamOffset(&ro_stream), 33);
EXPECT_EQ(ro_stream.numUsedBitsInPartialByte, 0);
}

TEST(StreamTest, WriteBitsLimit) {
testutil::AvifRwData rw_data;
avifRWStream rw_stream;
Expand Down

0 comments on commit 2c26b29

Please sign in to comment.