[clang] [Clang] Fix potential null pointer dereferences in Sema::AddInitializerToDecl (PR #94368)

Mike Rice via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 25 11:22:23 PDT 2024


================
@@ -13681,12 +13681,13 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
     }
 
     Init = Result.getAs<Expr>();
+    assert(Init && "Init must not be null");
+
     IsParenListInit = !InitSeq.steps().empty() &&
                       InitSeq.step_begin()->Kind ==
                           InitializationSequence::SK_ParenthesizedListInit;
     QualType VDeclType = VDecl->getType();
-    if (Init && !Init->getType().isNull() &&
-        !Init->getType()->isDependentType() && !VDeclType->isDependentType() &&
+    if (!Init->getType()->isDependentType() && !VDeclType->isDependentType() &&
----------------
mikerice1969 wrote:

> Was the static analysis tool perhaps complaining about later uses of `Init`?

Yes. FWIW Here is the logic:

```
if (!VDecl->isInvalidDecl()) {

  ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT);
  if (Result.isInvalid()) {
    return 
  }
  Init = Result.getAs<Expr>();

  if (Init && !Init->getType().isNull() && // verifier expects Init can be null.
}

…

if (!VDecl->isInvalidDecl()) {
  
  .. Init->getBeginLoc()))  // Deref of Init without check
}
```


https://github.com/llvm/llvm-project/pull/94368


More information about the cfe-commits mailing list