1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 02:52:53 +02:00

DataExtractor: Fix integer truncation issues in LEB128 extraction.

llvm-svn: 162201
This commit is contained in:
Benjamin Kramer 2012-08-20 10:52:11 +00:00
parent 0a4850d1ef
commit 3c9d9b798d
2 changed files with 12 additions and 3 deletions

View File

@ -139,7 +139,7 @@ uint64_t DataExtractor::getULEB128(uint32_t *offset_ptr) const {
while (isValidOffset(offset)) {
byte = Data[offset++];
result |= (byte & 0x7f) << shift;
result |= uint64_t(byte & 0x7f) << shift;
shift += 7;
if ((byte & 0x80) == 0)
break;
@ -160,7 +160,7 @@ int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
while (isValidOffset(offset)) {
byte = Data[offset++];
result |= (byte & 0x7f) << shift;
result |= uint64_t(byte & 0x7f) << shift;
shift += 7;
if ((byte & 0x80) == 0)
break;
@ -168,7 +168,7 @@ int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
// Sign bit of byte is 2nd high order bit (0x40)
if (shift < 64 && (byte & 0x40))
result |= -(1 << shift);
result |= -(1ULL << shift);
*offset_ptr = offset;
return result;

View File

@ -16,6 +16,7 @@ namespace {
const char numberData[] = "\x80\x90\xFF\xFF\x80\x00\x00\x00";
const char stringData[] = "hellohello\0hello";
const char leb128data[] = "\xA6\x49";
const char bigleb128data[] = "\xAA\xA9\xFF\xAA\xFF\xAA\xFF\x4A";
TEST(DataExtractorTest, OffsetOverflow) {
DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8);
@ -106,6 +107,14 @@ TEST(DataExtractorTest, LEB128) {
offset = 0;
EXPECT_EQ(-7002LL, DE.getSLEB128(&offset));
EXPECT_EQ(2U, offset);
DataExtractor BDE(StringRef(bigleb128data, sizeof(bigleb128data)-1), false,8);
offset = 0;
EXPECT_EQ(42218325750568106ULL, BDE.getULEB128(&offset));
EXPECT_EQ(8U, offset);
offset = 0;
EXPECT_EQ(-29839268287359830LL, BDE.getSLEB128(&offset));
EXPECT_EQ(8U, offset);
}
}