Skip to content

Commit 0edaba1

Browse files
authored
[clang-tidy] fix crash in altera-id-dependent-backward-branch (#113833)
Add some checks for `nullptr` and change some `dyn_cast` to `dyn_cast_if_present` to avoid crashes. Fixes #55408
1 parent 917b3d1 commit 0edaba1

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,44 @@ void IdDependentBackwardBranchCheck::registerMatchers(MatchFinder *Finder) {
7878

7979
IdDependentBackwardBranchCheck::IdDependencyRecord *
8080
IdDependentBackwardBranchCheck::hasIdDepVar(const Expr *Expression) {
81+
if (!Expression)
82+
return nullptr;
83+
8184
if (const auto *Declaration = dyn_cast<DeclRefExpr>(Expression)) {
8285
// It is a DeclRefExpr, so check if it's an ID-dependent variable.
83-
const auto *CheckVariable = dyn_cast<VarDecl>(Declaration->getDecl());
86+
const auto *CheckVariable =
87+
dyn_cast_if_present<VarDecl>(Declaration->getDecl());
88+
if (!CheckVariable)
89+
return nullptr;
8490
auto FoundVariable = IdDepVarsMap.find(CheckVariable);
8591
if (FoundVariable == IdDepVarsMap.end())
8692
return nullptr;
8793
return &(FoundVariable->second);
8894
}
8995
for (const auto *Child : Expression->children())
90-
if (const auto *ChildExpression = dyn_cast<Expr>(Child))
96+
if (const auto *ChildExpression = dyn_cast_if_present<Expr>(Child))
9197
if (IdDependencyRecord *Result = hasIdDepVar(ChildExpression))
9298
return Result;
9399
return nullptr;
94100
}
95101

96102
IdDependentBackwardBranchCheck::IdDependencyRecord *
97103
IdDependentBackwardBranchCheck::hasIdDepField(const Expr *Expression) {
104+
if (!Expression)
105+
return nullptr;
106+
98107
if (const auto *MemberExpression = dyn_cast<MemberExpr>(Expression)) {
99108
const auto *CheckField =
100-
dyn_cast<FieldDecl>(MemberExpression->getMemberDecl());
109+
dyn_cast_if_present<FieldDecl>(MemberExpression->getMemberDecl());
110+
if (!CheckField)
111+
return nullptr;
101112
auto FoundField = IdDepFieldsMap.find(CheckField);
102113
if (FoundField == IdDepFieldsMap.end())
103114
return nullptr;
104115
return &(FoundField->second);
105116
}
106117
for (const auto *Child : Expression->children())
107-
if (const auto *ChildExpression = dyn_cast<Expr>(Child))
118+
if (const auto *ChildExpression = dyn_cast_if_present<Expr>(Child))
108119
if (IdDependencyRecord *Result = hasIdDepField(ChildExpression))
109120
return Result;
110121
return nullptr;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ New check aliases
147147
Changes in existing checks
148148
^^^^^^^^^^^^^^^^^^^^^^^^^^
149149

150+
- Improved :doc:`altera-id-dependent-backward-branch
151+
<clang-tidy/checks/altera/id-dependent-backward-branch>` check by fixing
152+
crashes from invalid code.
153+
150154
- Improved :doc:`bugprone-casting-through-void
151155
<clang-tidy/checks/bugprone/casting-through-void>` check to suggest replacing
152156
the offending code with ``reinterpret_cast``, to more clearly express intent.

clang-tools-extra/test/clang-tidy/checkers/altera/id-dependent-backward-branch.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy %s altera-id-dependent-backward-branch %t -- -header-filter=.* "--" -cl-std=CL1.2 -c
1+
// RUN: %check_clang_tidy %s altera-id-dependent-backward-branch %t -- -header-filter=.* "--" -cl-std=CLC++1.0 -c
22

33
void error() {
44
// ==== Conditional Expressions ====
@@ -80,3 +80,9 @@ void success() {
8080
}
8181
}
8282
}
83+
84+
template<char... STOP>
85+
void gh55408(char const input[], int pos) {
86+
while (((input[pos] != STOP) && ...));
87+
}
88+

0 commit comments

Comments
 (0)