]> granicus.if.org Git - llvm/commitdiff
Bitcode: Fix short read implementation.
authorPeter Collingbourne <peter@pcc.me.uk>
Wed, 2 Nov 2016 02:58:47 +0000 (02:58 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Wed, 2 Nov 2016 02:58:47 +0000 (02:58 +0000)
We need to zero extend the byte in order to correctly shift it into a
64-bit value.

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

include/llvm/Bitcode/BitstreamReader.h
unittests/Bitcode/BitstreamReaderTest.cpp

index 8808935837abb3b65fa85f5f690b43ed7a35a715..b258e806e676a7ac2f81212ab2d23c22ab5d1d6c 100644 (file)
@@ -232,7 +232,7 @@ public:
       BytesRead = Buf.size() - NextChar;
       CurWord = 0;
       for (unsigned B = 0; B != BytesRead; ++B)
-        CurWord |= NextCharPtr[B] << (B * 8);
+        CurWord |= uint64_t(NextCharPtr[B]) << (B * 8);
     }
     NextChar += BytesRead;
     BitsInCurWord = BytesRead * 8;
index b5bd4a6fb5a9c16a61b3456eba7c35bc9824b14d..986023ee3e9310280730911a0a4a83ff5e68a4c9 100644 (file)
@@ -158,4 +158,13 @@ TEST(BitstreamReaderTest, readRecordWithBlobWhileStreaming) {
   }
 }
 
+TEST(BitstreamReaderTest, shortRead) {
+  uint8_t Bytes[] = {8, 7, 6, 5, 4, 3, 2, 1};
+  for (unsigned I = 1; I != 8; ++I) {
+    BitstreamReader Reader(ArrayRef<uint8_t>(Bytes, I));
+    SimpleBitstreamCursor Cursor(Reader);
+    EXPECT_EQ(8ull, Cursor.Read(8));
+  }
+}
+
 } // end anonymous namespace