Author: Manna, Soumi
Date: 2023-05-14T20:07:24-07:00
New Revision: 245549c57517de3b8fa478512bb1ae4295215e31

URL: 
https://github.com/llvm/llvm-project/commit/245549c57517de3b8fa478512bb1ae4295215e31
DIFF: 
https://github.com/llvm/llvm-project/commit/245549c57517de3b8fa478512bb1ae4295215e31.diff

LOG: [NFC][CLANG] Fix Static Code Analysis Concerns

Reported by Static Analyzer Tool, Coverity:

  Bad bit shift operation
  The operation may have an undefined behavior or yield an unexpected result.

  In <unnamed>::SVEEmitter::encodeFlag(unsigned long long, llvm::StringRef): A 
bit shift operation has a shift amount which is too large or has a negative 
value.

    // Returns the SVETypeFlags for a given value and mask.
    uint64_t encodeFlag(uint64_t V, StringRef MaskName) const {
      auto It = FlagTypes.find(MaskName);
        //Condition It != llvm::StringMap<unsigned long long, 
llvm::MallocAllocator>::const_iterator const(this->FlagTypes.end()), taking 
true branch.
      if (It != FlagTypes.end()) {
        uint64_t Mask = It->getValue();
        //return_constant: Function call llvm::countr_zero(Mask) may return 64.
        //assignment: Assigning: Shift = llvm::countr_zero(Mask). The value of 
Shift is now 64.
        unsigned Shift = llvm::countr_zero(Mask);

       //Bad bit shift operation (BAD_SHIFT)
       //large_shift: In expression V << Shift, left shifting by more than 63 
bits has undefined behavior. The shift amount, Shift, is 64.
        return (V << Shift) & Mask;
      }
      llvm_unreachable("Unsupported flag");
    }

Asserting Mask != 0 will not suffice to silence Coverity. While Coverity can 
specifically observe that countr_zero might return 0 (because 
TrailingZerosCounter<T, 8>::count() has a return 64 statement), It seems like 
Coverity can not determine that the function can't return 65 or higher. 
Coverity is reporting is that the shift might overflow,
so that is what should be guarded.
assert(Shift < 64 && "Mask value produced an invalid shift value");

Reviewed By: tahonermann, sdesmalen, erichkeane

Differential Revision: https://reviews.llvm.org/D150140

Added: 
    

Modified: 
    clang/utils/TableGen/SveEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/utils/TableGen/SveEmitter.cpp 
b/clang/utils/TableGen/SveEmitter.cpp
index d5d3f5fe558a8..d7f1e5af4db26 100644
--- a/clang/utils/TableGen/SveEmitter.cpp
+++ b/clang/utils/TableGen/SveEmitter.cpp
@@ -299,6 +299,7 @@ class SVEEmitter {
     if (It != FlagTypes.end()) {
       uint64_t Mask = It->getValue();
       unsigned Shift = llvm::countr_zero(Mask);
+      assert(Shift < 64 && "Mask value produced an invalid shift value");
       return (V << Shift) & Mask;
     }
     llvm_unreachable("Unsupported flag");


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to