[llvm-commits] [llvm] r162277 - /llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h

Duncan Sands baldrick at free.fr
Tue Aug 21 06:47:25 PDT 2012


Author: baldrick
Date: Tue Aug 21 08:47:25 2012
New Revision: 162277

URL: http://llvm.org/viewvc/llvm-project?rev=162277&view=rev
Log:
PVS-Studio noticed that EmitVBR64 would perform undefined behaviour if the
number of bits was bigger than 32.  I checked every use of this function
that I could find and it looks like the maximum number of bits is 32, so I've
added an assertion checking this property, and a type cast to (hopefully) stop
PVS-Studio from warning about this in the future.

Modified:
    llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h

Modified: llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h?rev=162277&r1=162276&r2=162277&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h (original)
+++ llvm/trunk/include/llvm/Bitcode/BitstreamWriter.h Tue Aug 21 08:47:25 2012
@@ -155,6 +155,7 @@
   }
 
   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.
@@ -167,10 +168,11 @@
   }
 
   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) {





More information about the llvm-commits mailing list