aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/StaticAnalyzer
diff options
context:
space:
mode:
authorFangyi Zhou <me@fangyi.io>2025-05-15 18:29:58 +0100
committerGitHub <noreply@github.com>2025-05-15 19:29:58 +0200
commit81d48e0f61f3e78cd6d6be9d3c8e48e7761a5ed5 (patch)
treed65339a68eff0898a47af8abb740474f10753486 /clang/lib/StaticAnalyzer
parent113898326b9cf3d9bb9edc3023e75fb3e4f82422 (diff)
downloadllvm-81d48e0f61f3e78cd6d6be9d3c8e48e7761a5ed5.zip
llvm-81d48e0f61f3e78cd6d6be9d3c8e48e7761a5ed5.tar.gz
llvm-81d48e0f61f3e78cd6d6be9d3c8e48e7761a5ed5.tar.bz2
[clang][analyzer] Fix a nullptr dereference when -ftime-trace is used (Reland) (#139980)
Fixes #139779. The bug was introduced in #137355 in `SymbolConjured::getStmt`, when trying to obtain a statement for a CFG initializer without an initializer. This commit adds a null check before access. Previous PR #139820, Revert #139936 Additional notes since previous PR: When conjuring a symbol, sometimes there is no valid CFG element, e.g. in the file causing the crash, there is no element at all in the CFG. In these cases, the CFG element reference in the expression engine will be invalid. As a consequence, there needs to be extra checks to ensure the validity of the CFG element reference.
Diffstat (limited to 'clang/lib/StaticAnalyzer')
-rw-r--r--clang/lib/StaticAnalyzer/Core/SymbolManager.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
index a6ade66..a469df4 100644
--- a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
+++ b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
@@ -80,6 +80,49 @@ void UnarySymExpr::dumpToStream(raw_ostream &os) const {
os << ')';
}
+const Stmt *SymbolConjured::getStmt() const {
+ // Sometimes the CFG element is invalid, avoid dereferencing it.
+ if (Elem.getParent() == nullptr ||
+ Elem.getIndexInBlock() >= Elem.getParent()->size())
+ return nullptr;
+ switch (Elem->getKind()) {
+ case CFGElement::Initializer:
+ if (const auto *Init = Elem->castAs<CFGInitializer>().getInitializer()) {
+ return Init->getInit();
+ }
+ return nullptr;
+ case CFGElement::ScopeBegin:
+ return Elem->castAs<CFGScopeBegin>().getTriggerStmt();
+ case CFGElement::ScopeEnd:
+ return Elem->castAs<CFGScopeEnd>().getTriggerStmt();
+ case CFGElement::NewAllocator:
+ return Elem->castAs<CFGNewAllocator>().getAllocatorExpr();
+ case CFGElement::LifetimeEnds:
+ return Elem->castAs<CFGLifetimeEnds>().getTriggerStmt();
+ case CFGElement::LoopExit:
+ return Elem->castAs<CFGLoopExit>().getLoopStmt();
+ case CFGElement::Statement:
+ return Elem->castAs<CFGStmt>().getStmt();
+ case CFGElement::Constructor:
+ return Elem->castAs<CFGConstructor>().getStmt();
+ case CFGElement::CXXRecordTypedCall:
+ return Elem->castAs<CFGCXXRecordTypedCall>().getStmt();
+ case CFGElement::AutomaticObjectDtor:
+ return Elem->castAs<CFGAutomaticObjDtor>().getTriggerStmt();
+ case CFGElement::DeleteDtor:
+ return Elem->castAs<CFGDeleteDtor>().getDeleteExpr();
+ case CFGElement::BaseDtor:
+ return nullptr;
+ case CFGElement::MemberDtor:
+ return nullptr;
+ case CFGElement::TemporaryDtor:
+ return Elem->castAs<CFGTemporaryDtor>().getBindTemporaryExpr();
+ case CFGElement::CleanupFunction:
+ return nullptr;
+ }
+ return nullptr;
+}
+
void SymbolConjured::dumpToStream(raw_ostream &os) const {
os << getKindStr() << getSymbolID() << '{' << T << ", LC" << LCtx->getID();
if (auto *S = getStmt())