Skip to content

Commit

Permalink
Fixed BitfieldMan::getOffsetCompletedLength overflow on 32-bit systems
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuhiro-t committed Dec 16, 2012
1 parent de1ca6d commit 04586f5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/BitfieldMan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -863,15 +863,15 @@ int64_t BitfieldMan::getOffsetCompletedLength
}
} else {
if(isBitSet(start)) {
res += (start+1)*blockLength_-offset;
res += static_cast<int64_t>(start+1)*blockLength_-offset;
}
for(size_t i = start+1; i <= end-1; ++i) {
if(isBitSet(i)) {
res += blockLength_;
}
}
if(isBitSet(end)) {
res += offset+length-end*blockLength_;
res += offset+length-static_cast<int64_t>(end)*blockLength_;
}
}
return res;
Expand Down
22 changes: 22 additions & 0 deletions test/BitfieldManTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class BitfieldManTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testGetSparseMissingUnusedIndex_withMinSplitSize);
CPPUNIT_TEST(testIsBitSetOffsetRange);
CPPUNIT_TEST(testGetOffsetCompletedLength);
CPPUNIT_TEST(testGetOffsetCompletedLength_largeFile);
CPPUNIT_TEST(testGetMissingUnusedLength);
CPPUNIT_TEST(testSetBitRange);
CPPUNIT_TEST(testGetAllMissingIndexes);
Expand Down Expand Up @@ -62,6 +63,7 @@ class BitfieldManTest:public CppUnit::TestFixture {
void testGetSparseMissingUnusedIndex_withMinSplitSize();
void testIsBitSetOffsetRange();
void testGetOffsetCompletedLength();
void testGetOffsetCompletedLength_largeFile();
void testGetMissingUnusedLength();
void testSetBitRange();
void testCountFilteredBlock();
Expand Down Expand Up @@ -484,6 +486,26 @@ void BitfieldManTest::testGetOffsetCompletedLength()
CPPUNIT_ASSERT_EQUAL((int64_t)0, bt.getOffsetCompletedLength(1024*20, 1));
}

void BitfieldManTest::testGetOffsetCompletedLength_largeFile()
{
// Test for overflow on 32-bit systems.

// Total 4TiB, 4MiB block
BitfieldMan bt(1 << 22, 1LL << 40);
bt.setBit(1 << 11);
bt.setBit((1 << 11)+1);
bt.setBit((1 << 11)+2);

// The last piece is missing:
CPPUNIT_ASSERT_EQUAL((int64_t)bt.getBlockLength()*3,
bt.getOffsetCompletedLength(1LL << 33, 1 << 24));

// The first piece is missing:
CPPUNIT_ASSERT_EQUAL((int64_t)bt.getBlockLength()*3,
bt.getOffsetCompletedLength
((1LL << 33) - bt.getBlockLength(), 1 << 24));
}

void BitfieldManTest::testGetMissingUnusedLength()
{
int64_t totalLength = 1024*10+10;
Expand Down

0 comments on commit 04586f5

Please sign in to comment.