[PATCH] D150140: [NFC][CLANG] Fix Static Code Analysis Concerns

Soumi Manna via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 8 13:06:28 PDT 2023


Manna created this revision.
Manna added reviewers: erichkeane, tahonermann.
Herald added a subscriber: ctetreau.
Herald added a project: All.
Manna requested review of this revision.
Herald added a project: clang.

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");
    }

This patch adds an assert to resolve the bug.

  


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150140

Files:
  clang/utils/TableGen/SveEmitter.cpp


Index: clang/utils/TableGen/SveEmitter.cpp
===================================================================
--- clang/utils/TableGen/SveEmitter.cpp
+++ clang/utils/TableGen/SveEmitter.cpp
@@ -299,6 +299,7 @@
     if (It != FlagTypes.end()) {
       uint64_t Mask = It->getValue();
       unsigned Shift = llvm::countr_zero(Mask);
+      assert(Shift >= 64 && "Shift is out of encodable range");
       return (V << Shift) & Mask;
     }
     llvm_unreachable("Unsupported flag");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150140.520467.patch
Type: text/x-patch
Size: 483 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230508/cb1c2d89/attachment.bin>


More information about the cfe-commits mailing list