https://github.com/tahonermann updated https://github.com/llvm/llvm-project/pull/106235
>From 787efaec5a565d118cacd306c91ed002bea7a992 Mon Sep 17 00:00:00 2001 From: Tom Honermann <tom.honerm...@intel.com> Date: Sun, 25 Aug 2024 11:33:29 -0700 Subject: [PATCH 1/2] [NFC][Clang] Avoid potential null pointer dereferences in Sema::AddInitializerToDecl(). Control flow analysis performed by a static analysis tool revealed the potential for null pointer dereferences to occur in conjunction with the `Init` parameter in Sema::AddInitializerToDecl(). On entry to the function, `Init` is required to be non-null as there are multiple potential branches that unconditionally dereference it. However, there were two places where `Init` is compared to null thus implying that `Init` is expected to be null in some cases. These checks appear to be purely defensive checks and thus unnecessary. Further, there were several cases where code checked `Result`, a variable of type `ExprResult`, for an invalid value, but did not check for a valid but null value and then proceeded to unconditionally dereference the potential null result. This change elides the unnecessary defensive checks and changes some checks for an invalid result to instead branch on an unusable result (either an invalid result or a valid but null result). --- clang/lib/Sema/SemaDecl.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index b0ccbbe34b70c3..c643d4fbbba0eb 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13319,7 +13319,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { } // WebAssembly tables can't be used to initialise a variable. - if (Init && !Init->getType().isNull() && + if (!Init->getType().isNull() && Init->getType()->isWebAssemblyTableType()) { Diag(Init->getExprLoc(), diag::err_wasm_table_art) << 0; VDecl->setInvalidDecl(); @@ -13463,7 +13463,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { if (getLangOpts().DebuggerCastResultToId && DclT->isObjCObjectPointerType() && Init->getType() == Context.UnknownAnyTy) { ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); - if (Result.isInvalid()) { + if (!Result.isUsable()) { VDecl->setInvalidDecl(); return; } @@ -13491,7 +13491,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { InitializationSequence Init(*this, Entity, Kind, MultiExprArg(E)); return Init.Failed() ? ExprError() : E; }); - if (Res.isInvalid()) { + if (!Res.isUsable()) { VDecl->setInvalidDecl(); } else if (Res.get() != Args[Idx]) { Args[Idx] = Res.get(); @@ -13504,7 +13504,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { /*TopLevelOfInitList=*/false, /*TreatUnavailableAsInvalid=*/false); ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT); - if (Result.isInvalid()) { + if (!Result.isUsable()) { // If the provided initializer fails to initialize the var decl, // we attach a recovery expr for better recovery. auto RecoveryExpr = @@ -13528,7 +13528,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { InitSeq.step_begin()->Kind == InitializationSequence::SK_ParenthesizedListInit; QualType VDeclType = VDecl->getType(); - if (Init && !Init->getType().isNull() && + if (!Init->getType().isNull() && !Init->getType()->isDependentType() && !VDeclType->isDependentType() && Context.getAsIncompleteArrayType(VDeclType) && Context.getAsIncompleteArrayType(Init->getType())) { @@ -13592,7 +13592,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { ExprResult Result = ActOnFinishFullExpr(Init, VDecl->getLocation(), /*DiscardedValue*/ false, VDecl->isConstexpr()); - if (Result.isInvalid()) { + if (!Result.isUsable()) { VDecl->setInvalidDecl(); return; } >From 6758e11992078d882ae96b18841ccd7aeaea6aac Mon Sep 17 00:00:00 2001 From: Tom Honermann <tom.honerm...@intel.com> Date: Tue, 27 Aug 2024 08:57:21 -0700 Subject: [PATCH 2/2] Address clang-format complaints. --- clang/lib/Sema/SemaDecl.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index c643d4fbbba0eb..281c335f8e5066 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13319,8 +13319,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { } // WebAssembly tables can't be used to initialise a variable. - if (!Init->getType().isNull() && - Init->getType()->isWebAssemblyTableType()) { + if (!Init->getType().isNull() && Init->getType()->isWebAssemblyTableType()) { Diag(Init->getExprLoc(), diag::err_wasm_table_art) << 0; VDecl->setInvalidDecl(); return; @@ -13528,8 +13527,8 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { InitSeq.step_begin()->Kind == InitializationSequence::SK_ParenthesizedListInit; QualType VDeclType = VDecl->getType(); - if (!Init->getType().isNull() && - !Init->getType()->isDependentType() && !VDeclType->isDependentType() && + if (!Init->getType().isNull() && !Init->getType()->isDependentType() && + !VDeclType->isDependentType() && Context.getAsIncompleteArrayType(VDeclType) && Context.getAsIncompleteArrayType(Init->getType())) { // Bail out if it is not possible to deduce array size from the _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits