-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang][analyzer] Fix crash in loop unrolling #82089
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: None (huang-me) ChangesStaticAnalyzer didn't check if the variable is declared in Fixes #68819 Full diff: https://github.com/llvm/llvm-project/pull/82089.diff 1 Files Affected:
diff --git a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
index a80352816be613..b91dfa26774aa4 100644
--- a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
+++ b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
@@ -226,6 +226,17 @@ static bool isPossiblyEscaped(ExplodedNode *N, const DeclRefExpr *DR) {
return false;
}
}
+
+ if (const SwitchStmt *SS = dyn_cast<SwitchStmt>(S)) {
+ for(const Stmt *CB: dyn_cast<CompoundStmt>(SS->getBody())->body()) {
+ for (const Decl *D : dyn_cast<DeclStmt>(CB)->decls()) {
+ // Once we reach the declaration of the VD we can return.
+ if (D->getCanonicalDecl() == VD)
+ return false;
+ }
+ }
+ }
+
// Check the usage of the pass-by-ref function calls and adress-of operator
// on VD and reference initialized by VD.
ASTContext &ASTCtx =
|
@llvm/pr-subscribers-clang-static-analyzer-1 Author: None (huang-me) ChangesStaticAnalyzer didn't check if the variable is declared in Fixes #68819 Full diff: https://github.com/llvm/llvm-project/pull/82089.diff 1 Files Affected:
diff --git a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
index a80352816be613..b91dfa26774aa4 100644
--- a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
+++ b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
@@ -226,6 +226,17 @@ static bool isPossiblyEscaped(ExplodedNode *N, const DeclRefExpr *DR) {
return false;
}
}
+
+ if (const SwitchStmt *SS = dyn_cast<SwitchStmt>(S)) {
+ for(const Stmt *CB: dyn_cast<CompoundStmt>(SS->getBody())->body()) {
+ for (const Decl *D : dyn_cast<DeclStmt>(CB)->decls()) {
+ // Once we reach the declaration of the VD we can return.
+ if (D->getCanonicalDecl() == VD)
+ return false;
+ }
+ }
+ }
+
// Check the usage of the pass-by-ref function calls and adress-of operator
// on VD and reference initialized by VD.
ASTContext &ASTCtx =
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
a590abd
to
4ebdf47
Compare
4ebdf47
to
2802ef4
Compare
If I understand it correctly, your change doesn't handle declarations that are in inner statements, e.g. the variable "x" in the following code: switch (get_value()) {
case 42:
do {
int x;
// ...
} while (running);
//...
} Is this compatible with the goals of your commit, or would the original crash remain in a situation like this? |
As far as I understand it, the declaration within the |
That sounds reasonable. Unfortunately I don't know much about the context of this change, so let's wait for a review from @danix800 (or somebody else who's knows enough) before merging this change. Also, perhaps it would be a good idea to add a testcase (or a few of them) in the directory |
I've added the test case to illustrate the goal of this change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this.
I think iterating the direct child nodes of the switch is fine. I can't think of a better way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
EDIT: Sorry for my late reply.
@huang-me Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…308497902 Local branch amd-gfx 9633084 Merged main:55d4816393f897054a4721920502d45c645edf1d into amd-gfx:f95c9bf0b616 Remote branch main 8f68022 [clang][analyzer] Fix crash in loop unrolling (llvm#82089)
StaticAnalyzer didn't check if the variable is declared in
CompoundStmt
underSwitchStmt
, which make static analyzer reach root without finding the declaration.Fixes #68819