[PATCH] D149922: Fix nullptr dereference found by Coverity static analysis tool

Soumi Manna via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu May 4 18:50:03 PDT 2023


Manna created this revision.
Manna added a reviewer: erichkeane.
Herald added a project: All.
Manna requested review of this revision.
Herald added a project: clang.

Reported by Coverity:

  In clang::​ASTContext::​hasUniqueObjectRepresentations(clang::​QualType, bool): Return value of function which returns null is dereferenced without checking.

  (Ty->isMemberPointerType()) {
      	//returned_null: getAs returns nullptr.
      	//var_assigned: Assigning: MPT = nullptr return value from getAs.
      const auto *MPT = Ty->getAs<MemberPointerType>();
      	
     //dereference: Dereferencing a pointer that might be nullptr MPT when calling getMemberPointerInfo. (The virtual call resolves to 
     <unnamed>::ItaniumCXXABI::getMemberPointerInfo.)
      return !ABI->getMemberPointerInfo(MPT).HasPadding;
    }

This patch uses castAs instead of getAs.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149922

Files:
  clang/lib/AST/ASTContext.cpp


Index: clang/lib/AST/ASTContext.cpp
===================================================================
--- clang/lib/AST/ASTContext.cpp
+++ clang/lib/AST/ASTContext.cpp
@@ -2860,7 +2860,7 @@
     return true;
 
   if (Ty->isMemberPointerType()) {
-    const auto *MPT = Ty->getAs<MemberPointerType>();
+    const auto *MPT = Ty->castAs<MemberPointerType>();
     return !ABI->getMemberPointerInfo(MPT).HasPadding;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149922.519715.patch
Type: text/x-patch
Size: 422 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230505/b3812b33/attachment.bin>


More information about the cfe-commits mailing list