[clang][analyzer] Fix a nullptr dereference when -ftime-trace is used#139820
Merged
[clang][analyzer] Fix a nullptr dereference when -ftime-trace is used#139820
-ftime-trace is used#139820Conversation
Fixes llvm#139779. The bug was introduced in llvm#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.
Member
|
@llvm/pr-subscribers-clang-static-analyzer-1 @llvm/pr-subscribers-clang Author: Fangyi Zhou (fangyi-zhou) ChangesFixes #139779. The bug was introduced in #137355 in Full diff: https://github.com/llvm/llvm-project/pull/139820.diff 2 Files Affected:
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
index 9e7c98fdded17..00159971fd7b5 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
@@ -103,6 +103,8 @@ class SymbolConjured : public SymbolData {
const Stmt *getStmt() const {
switch (Elem->getKind()) {
case CFGElement::Initializer:
+ if (Elem->castAs<CFGInitializer>().getInitializer() == nullptr)
+ return nullptr;
return Elem->castAs<CFGInitializer>().getInitializer()->getInit();
case CFGElement::ScopeBegin:
return Elem->castAs<CFGScopeBegin>().getTriggerStmt();
diff --git a/clang/test/Analysis/ftime-trace-no-init.cpp b/clang/test/Analysis/ftime-trace-no-init.cpp
new file mode 100644
index 0000000000000..db62aa8a56ed7
--- /dev/null
+++ b/clang/test/Analysis/ftime-trace-no-init.cpp
@@ -0,0 +1,5 @@
+// RUN: %clang --analyze %s -ftime-trace -Xclang -verify
+// expected-no-diagnostics
+
+// GitHub issue 139779
+struct {} a; // no-crash
|
Collaborator
AaronBallman
left a comment
There was a problem hiding this comment.
Generally LGTM but a suggestion for a perhaps more clear way to do it
clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
Outdated
Show resolved
Hide resolved
steakhal
reviewed
May 14, 2025
Contributor
steakhal
left a comment
There was a problem hiding this comment.
I can only agree with Aaron. It looks nice.
Thank you!
clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
Outdated
Show resolved
Hide resolved
Contributor
Author
|
This seems to cause asan test failures in buildbot, I'll create a PR to revert. |
llvm-sync bot
pushed a commit
to arm/arm-toolchain
that referenced
this pull request
May 14, 2025
…-ftime-trace` is used" (#139936) Reverts llvm/llvm-project#139820 Reverting due to buildbot failures in asan
steakhal
pushed a commit
that referenced
this pull request
May 15, 2025
… (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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.