[clang] Initialize member variable; NFC (PR #135167)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 10 04:56:41 PDT 2025


https://github.com/AaronBallman created https://github.com/llvm/llvm-project/pull/135167

This was found via a Coverity static analysis pass. There's no indication this was being used incorrectly in practice, but there are public interfaces which require `BR` to be non-null and valid, and `BR` was not being initialized by the constructor.

This adds an in-class initializer for `BR` and some asserts, to be safe.

>From 36b8b35b317de60c6250fd8b6c4c4bba07e740da Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Thu, 10 Apr 2025 07:53:43 -0400
Subject: [PATCH] Initialize member variable; NFC

This was found via a Coverity static analysis pass. There's no
indication this was being used incorrectly in practice, but there are
public interfaces which require `BR` to be non-null and valid, and `BR`
was not being initialized by the constructor.

This adds an in-class initializer for `BR` and some asserts, to be safe.
---
 .../Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp           | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
index 1f9a4712cedf6..fc3f510d1d499 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
@@ -31,7 +31,7 @@ class RetainPtrCtorAdoptChecker
     : public Checker<check::ASTDecl<TranslationUnitDecl>> {
 private:
   BugType Bug;
-  mutable BugReporter *BR;
+  mutable BugReporter *BR = nullptr;
   mutable std::unique_ptr<RetainSummaryManager> Summaries;
   mutable llvm::DenseSet<const ValueDecl *> CreateOrCopyOutArguments;
   mutable RetainTypeChecker RTC;
@@ -111,6 +111,7 @@ class RetainPtrCtorAdoptChecker
   }
 
   void visitCallExpr(const CallExpr *CE, const Decl *DeclWithIssue) const {
+    assert(BR && "expected nonnull BugReporter");
     if (BR->getSourceManager().isInSystemHeader(CE->getExprLoc()))
       return;
 
@@ -169,6 +170,7 @@ class RetainPtrCtorAdoptChecker
 
   void visitConstructExpr(const CXXConstructExpr *CE,
                           const Decl *DeclWithIssue) const {
+    assert(BR && "expected nonnull BugReporter");
     if (BR->getSourceManager().isInSystemHeader(CE->getExprLoc()))
       return;
 
@@ -356,6 +358,7 @@ class RetainPtrCtorAdoptChecker
       Os << " " << condition;
     Os << ".";
 
+    assert(BR && "expected nonnull BugReporter");
     PathDiagnosticLocation BSLoc(CE->getSourceRange().getBegin(),
                                  BR->getSourceManager());
     auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
@@ -376,6 +379,7 @@ class RetainPtrCtorAdoptChecker
       Os << " " << condition;
     Os << ".";
 
+    assert(BR && "expected nonnull BugReporter");
     PathDiagnosticLocation BSLoc(CE->getSourceRange().getBegin(),
                                  BR->getSourceManager());
     auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);



More information about the cfe-commits mailing list