PVS-Studio noticed that EmitVBR64 would perform undefined behaviour if the
[oota-llvm.git] / include / llvm / Bitcode / BitstreamWriter.h
index 55e3cd390b1289dcf4b518768cfb6f82ea3a964c..65933a28e43d3bb5b2c640e9fbfb8674b0472fec 100644 (file)
@@ -74,10 +74,12 @@ class BitstreamWriter {
   }
 
   void WriteWord(unsigned Value) {
-    Out.push_back((unsigned char)(Value >>  0));
-    Out.push_back((unsigned char)(Value >>  8));
-    Out.push_back((unsigned char)(Value >> 16));
-    Out.push_back((unsigned char)(Value >> 24));
+    unsigned char Bytes[4] = {
+      (unsigned char)(Value >>  0),
+      (unsigned char)(Value >>  8),
+      (unsigned char)(Value >> 16),
+      (unsigned char)(Value >> 24) };
+    Out.append(&Bytes[0], &Bytes[4]);
   }
 
   unsigned GetBufferOffset() const {
@@ -153,6 +155,7 @@ public:
   }
 
   void EmitVBR(uint32_t Val, unsigned NumBits) {
+    assert(NumBits <= 32 && "Too many bits to emit!");
     uint32_t Threshold = 1U << (NumBits-1);
 
     // Emit the bits with VBR encoding, NumBits-1 bits at a time.
@@ -165,10 +168,11 @@ public:
   }
 
   void EmitVBR64(uint64_t Val, unsigned NumBits) {
+    assert(NumBits <= 32 && "Too many bits to emit!");
     if ((uint32_t)Val == Val)
       return EmitVBR((uint32_t)Val, NumBits);
 
-    uint64_t Threshold = 1U << (NumBits-1);
+    uint64_t Threshold = uint64_t(1U << (NumBits-1));
 
     // Emit the bits with VBR encoding, NumBits-1 bits at a time.
     while (Val >= Threshold) {
@@ -499,7 +503,7 @@ public:
   /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
   void EnterBlockInfoBlock(unsigned CodeWidth) {
     EnterSubblock(bitc::BLOCKINFO_BLOCK_ID, CodeWidth);
-    BlockInfoCurBID = -1U;
+    BlockInfoCurBID = ~0U;
   }
 private:
   /// SwitchToBlockID - If we aren't already talking about the specified block