Skip to content

Commit

Permalink
[clang-tidy] fix crash in altera-id-dependent-backward-branch (llvm#1…
Browse files Browse the repository at this point in the history
…13833)

Add some checks for `nullptr` and change some `dyn_cast` to
`dyn_cast_if_present` to avoid crashes.

Fixes llvm#55408
  • Loading branch information
5chmidti authored Nov 2, 2024
1 parent 917b3d1 commit 0edaba1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,44 @@ 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;
}

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;
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ New check aliases
Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^

- Improved :doc:`altera-id-dependent-backward-branch
<clang-tidy/checks/altera/id-dependent-backward-branch>` check by fixing
crashes from invalid code.

- Improved :doc:`bugprone-casting-through-void
<clang-tidy/checks/bugprone/casting-through-void>` check to suggest replacing
the offending code with ``reinterpret_cast``, to more clearly express intent.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %s altera-id-dependent-backward-branch %t -- -header-filter=.* "--" -cl-std=CL1.2 -c
// RUN: %check_clang_tidy %s altera-id-dependent-backward-branch %t -- -header-filter=.* "--" -cl-std=CLC++1.0 -c

void error() {
// ==== Conditional Expressions ====
Expand Down Expand Up @@ -80,3 +80,9 @@ void success() {
}
}
}

template<char... STOP>
void gh55408(char const input[], int pos) {
while (((input[pos] != STOP) && ...));
}

0 comments on commit 0edaba1

Please sign in to comment.