-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-tidy] fix crash in altera-id-dependent-backward-branch #113833
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-tidy] fix crash in altera-id-dependent-backward-branch #113833
Conversation
Add some checks for `nullptr` and change some `dyn_cast` to `dyn_cast_if_present` to avoid crashes. Fixes llvm#55408
@llvm/pr-subscribers-clang-tidy Author: Julian Schmidt (5chmidti) ChangesAdd some checks for Fixes #55408 Full diff: https://github.com/llvm/llvm-project/pull/113833.diff 1 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp b/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp
index 09f7d8c5c2f952..94db0a793cf53d 100644
--- a/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp
+++ b/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp
@@ -78,16 +78,22 @@ void IdDependentBackwardBranchCheck::registerMatchers(MatchFinder *Finder) {
IdDependentBackwardBranchCheck::IdDependencyRecord *
IdDependentBackwardBranchCheck::hasIdDepVar(const Expr *Expression) {
+ if (!Expression)
+ return nullptr;
+
if (const auto *Declaration = dyn_cast<DeclRefExpr>(Expression)) {
// It is a DeclRefExpr, so check if it's an ID-dependent variable.
- const auto *CheckVariable = dyn_cast<VarDecl>(Declaration->getDecl());
+ const auto *CheckVariable =
+ dyn_cast_if_present<VarDecl>(Declaration->getDecl());
+ if (!CheckVariable)
+ return nullptr;
auto FoundVariable = IdDepVarsMap.find(CheckVariable);
if (FoundVariable == IdDepVarsMap.end())
return nullptr;
return &(FoundVariable->second);
}
for (const auto *Child : Expression->children())
- if (const auto *ChildExpression = dyn_cast<Expr>(Child))
+ if (const auto *ChildExpression = dyn_cast_if_present<Expr>(Child))
if (IdDependencyRecord *Result = hasIdDepVar(ChildExpression))
return Result;
return nullptr;
@@ -95,16 +101,21 @@ IdDependentBackwardBranchCheck::hasIdDepVar(const Expr *Expression) {
IdDependentBackwardBranchCheck::IdDependencyRecord *
IdDependentBackwardBranchCheck::hasIdDepField(const Expr *Expression) {
+ if (!Expression)
+ return nullptr;
+
if (const auto *MemberExpression = dyn_cast<MemberExpr>(Expression)) {
const auto *CheckField =
- dyn_cast<FieldDecl>(MemberExpression->getMemberDecl());
+ dyn_cast_if_present<FieldDecl>(MemberExpression->getMemberDecl());
+ if (!CheckField)
+ return nullptr;
auto FoundField = IdDepFieldsMap.find(CheckField);
if (FoundField == IdDepFieldsMap.end())
return nullptr;
return &(FoundField->second);
}
for (const auto *Child : Expression->children())
- if (const auto *ChildExpression = dyn_cast<Expr>(Child))
+ if (const auto *ChildExpression = dyn_cast_if_present<Expr>(Child))
if (IdDependencyRecord *Result = hasIdDepField(ChildExpression))
return Result;
return nullptr;
|
@llvm/pr-subscribers-clang-tools-extra Author: Julian Schmidt (5chmidti) ChangesAdd some checks for Fixes #55408 Full diff: https://github.com/llvm/llvm-project/pull/113833.diff 1 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp b/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp
index 09f7d8c5c2f952..94db0a793cf53d 100644
--- a/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp
+++ b/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp
@@ -78,16 +78,22 @@ void IdDependentBackwardBranchCheck::registerMatchers(MatchFinder *Finder) {
IdDependentBackwardBranchCheck::IdDependencyRecord *
IdDependentBackwardBranchCheck::hasIdDepVar(const Expr *Expression) {
+ if (!Expression)
+ return nullptr;
+
if (const auto *Declaration = dyn_cast<DeclRefExpr>(Expression)) {
// It is a DeclRefExpr, so check if it's an ID-dependent variable.
- const auto *CheckVariable = dyn_cast<VarDecl>(Declaration->getDecl());
+ const auto *CheckVariable =
+ dyn_cast_if_present<VarDecl>(Declaration->getDecl());
+ if (!CheckVariable)
+ return nullptr;
auto FoundVariable = IdDepVarsMap.find(CheckVariable);
if (FoundVariable == IdDepVarsMap.end())
return nullptr;
return &(FoundVariable->second);
}
for (const auto *Child : Expression->children())
- if (const auto *ChildExpression = dyn_cast<Expr>(Child))
+ if (const auto *ChildExpression = dyn_cast_if_present<Expr>(Child))
if (IdDependencyRecord *Result = hasIdDepVar(ChildExpression))
return Result;
return nullptr;
@@ -95,16 +101,21 @@ IdDependentBackwardBranchCheck::hasIdDepVar(const Expr *Expression) {
IdDependentBackwardBranchCheck::IdDependencyRecord *
IdDependentBackwardBranchCheck::hasIdDepField(const Expr *Expression) {
+ if (!Expression)
+ return nullptr;
+
if (const auto *MemberExpression = dyn_cast<MemberExpr>(Expression)) {
const auto *CheckField =
- dyn_cast<FieldDecl>(MemberExpression->getMemberDecl());
+ dyn_cast_if_present<FieldDecl>(MemberExpression->getMemberDecl());
+ if (!CheckField)
+ return nullptr;
auto FoundField = IdDepFieldsMap.find(CheckField);
if (FoundField == IdDepFieldsMap.end())
return nullptr;
return &(FoundField->second);
}
for (const auto *Child : Expression->children())
- if (const auto *ChildExpression = dyn_cast<Expr>(Child))
+ if (const auto *ChildExpression = dyn_cast_if_present<Expr>(Child))
if (IdDependencyRecord *Result = hasIdDepField(ChildExpression))
return Result;
return nullptr;
|
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
…13833) Add some checks for `nullptr` and change some `dyn_cast` to `dyn_cast_if_present` to avoid crashes. Fixes llvm#55408
…13833) Add some checks for `nullptr` and change some `dyn_cast` to `dyn_cast_if_present` to avoid crashes. Fixes llvm#55408
Add some checks for
nullptr
and change somedyn_cast
todyn_cast_if_present
to avoid crashes.Fixes #55408