From 4ef54c3f85f296ea5c80969ce47a1d80856ebc8a Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Fri, 22 Aug 2014 16:29:45 +0000 Subject: [PATCH] [Support] Fix the overflow bug in ULEB128 decoding. 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 | 2 +- unittests/Support/LEB128Test.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/llvm/Support/LEB128.h b/include/llvm/Support/LEB128.h index ea76c9b5892..4dbc5e91a2c 100644 --- a/include/llvm/Support/LEB128.h +++ b/include/llvm/Support/LEB128.h @@ -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) diff --git a/unittests/Support/LEB128Test.cpp b/unittests/Support/LEB128Test.cpp index b1ca13ef241..14a6d3ff5ad 100644 --- a/unittests/Support/LEB128Test.cpp +++ b/unittests/Support/LEB128Test.cpp @@ -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"); -- 2.34.1