[clang-tools-extra] [clang-tidy] fix crash in altera-id-dependent-backward-branch (PR #113833)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 27 12:50:17 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: Julian Schmidt (5chmidti)
<details>
<summary>Changes</summary>
Add some checks for `nullptr` and change some `dyn_cast` to
`dyn_cast_if_present` to avoid crashes.
Fixes #<!-- -->55408
---
Full diff: https://github.com/llvm/llvm-project/pull/113833.diff
1 Files Affected:
- (modified) clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp (+15-4)
``````````diff
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/113833
More information about the cfe-commits
mailing list