[Support] Fix the overflow bug in ULEB128 decoding.
authorAlex Lorenz <arphaman@gmail.com>
Fri, 22 Aug 2014 16:29:45 +0000 (16:29 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Fri, 22 Aug 2014 16:29:45 +0000 (16:29 +0000)
Differential Revision: http://reviews.llvm.org/D5029

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216268 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/LEB128.h
unittests/Support/LEB128Test.cpp

index ea76c9b58922b311ef1ede89a53b89c5a9f6d246..4dbc5e91a2cdcd9b0a161aaa755b6ba63a5534c6 100644 (file)
@@ -82,7 +82,7 @@ inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr) {
   uint64_t Value = 0;
   unsigned Shift = 0;
   do {
-    Value += (*p & 0x7f) << Shift;
+    Value += uint64_t(*p & 0x7f) << Shift;
     Shift += 7;
   } while (*p++ >= 128);
   if (n)
index b1ca13ef24164918eb42753cf24b8de5d869d5b6..14a6d3ff5ad764504f9e8a62ee5c174a73ef1536 100644 (file)
@@ -106,6 +106,7 @@ TEST(LEB128Test, DecodeULEB128) {
   EXPECT_DECODE_ULEB128_EQ(0xffu, "\xff\x01");
   EXPECT_DECODE_ULEB128_EQ(0x100u, "\x80\x02");
   EXPECT_DECODE_ULEB128_EQ(0x101u, "\x81\x02");
+  EXPECT_DECODE_ULEB128_EQ(4294975616ULL, "\x80\xc1\x80\x80\x10");
 
   // Decode ULEB128 with extra padding bytes
   EXPECT_DECODE_ULEB128_EQ(0u, "\x80\x00");