Skip to content

Conversation

@5chmidti
Copy link
Contributor

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

Fixes #55408

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

Fixes llvm#55408
@llvmbot
Copy link
Member

llvmbot commented Oct 27, 2024

@llvm/pr-subscribers-clang-tidy

Author: Julian Schmidt (5chmidti)

Changes

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 --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;

@llvmbot
Copy link
Member

llvmbot commented Oct 27, 2024

@llvm/pr-subscribers-clang-tools-extra

Author: Julian Schmidt (5chmidti)

Changes

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 --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;

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@5chmidti 5chmidti merged commit 0edaba1 into llvm:main Nov 2, 2024
9 checks passed
@5chmidti 5chmidti deleted the clang_tidy_altera_id_dependent_nullptr branch November 2, 2024 11:08
smallp-o-p pushed a commit to smallp-o-p/llvm-project that referenced this pull request Nov 3, 2024
…13833)

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

Fixes llvm#55408
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
…13833)

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

Fixes llvm#55408
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang-tidy] altera-id-dependent-backward-branch crash

3 participants