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

Tom Honermann via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 16 11:11:00 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() &&
----------------
tahonermann wrote:

I'm uncertain about this change. On the one hand, the assignment to `Init` looks to me like it must produce a non-null result due to the prior check to `Result.isInvalid()`. However, the following uses of `Init` were already guarded by a check for a non-null value, so the static analysis tool should not have complained about those.

Was the static analysis tool perhaps complaining about later uses of `Init`? Note that the assignment at line 13683 above is conditional (on `!VDecl->isInvalidDecl()`) and therefore might not suffice to ensure a definite non-null value. I haven't checked exhaustively if that is the case though.

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


More information about the cfe-commits mailing list