Skip to content

[NFC][Clang] Fix potential dereferencing of nullptr #86759

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

Merged
merged 1 commit into from
Mar 28, 2024

Conversation

smanna12
Copy link
Contributor

This patch replaces dyn_cast<> with cast<> to resolve potential static analyzer bugs for

  1. Dereferencing a pointer issue with nullptr GVar when calling addAttribute() in AIXTargetCodeGenInfo::setTargetAttributes(clang::Decl const *, llvm::GlobalValue *, clang::CodeGen::CodeGenModule &).

  2. Dereferencing a pointer issue with nullptr GG when calling getCorrespondingConstructor() in DeclareImplicitDeductionGuidesForTypeAlias(clang::Sema &, clang::TypeAliasTemplateDecl *, clang::SourceLocation).

  3. Dereferencing a pointer issue with nullptr CurrentBT when calling getKind() in ComplexExprEmitter::GetHigherPrecisionFPType(clang::QualType).

This patch replaces dyn_cast<> with cast<> to resolve potential static
analyzer bugs for

1. Dereferencing a pointer issue with nullptr GVar when calling addAttribute()
   in AIXTargetCodeGenInfo::setTargetAttributes(clang::Decl const *, llvm::GlobalValue *, clang::CodeGen::CodeGenModule &).

2. Dereferencing a pointer issue with nullptr GG when calling getCorrespondingConstructor() in DeclareImplicitDeductionGuidesForTypeAlias(clang::Sema &, clang::TypeAliasTemplateDecl *, clang::SourceLocation).

3. Dereferencing a pointer issue with nullptr CurrentBT when calling
   getKind() in ComplexExprEmitter::GetHigherPrecisionFPType(clang::QualType).
@smanna12 smanna12 requested a review from tahonermann March 27, 2024 02:32
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:PowerPC clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:codegen IR generation bugs: mangling, exceptions, etc. labels Mar 27, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 27, 2024

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: None (smanna12)

Changes

This patch replaces dyn_cast<> with cast<> to resolve potential static analyzer bugs for

  1. Dereferencing a pointer issue with nullptr GVar when calling addAttribute() in AIXTargetCodeGenInfo::setTargetAttributes(clang::Decl const *, llvm::GlobalValue *, clang::CodeGen::CodeGenModule &).

  2. Dereferencing a pointer issue with nullptr GG when calling getCorrespondingConstructor() in DeclareImplicitDeductionGuidesForTypeAlias(clang::Sema &, clang::TypeAliasTemplateDecl *, clang::SourceLocation).

  3. Dereferencing a pointer issue with nullptr CurrentBT when calling getKind() in ComplexExprEmitter::GetHigherPrecisionFPType(clang::QualType).


Full diff: https://github.com/llvm/llvm-project/pull/86759.diff

3 Files Affected:

  • (modified) clang/lib/CodeGen/CGExprComplex.cpp (+1-1)
  • (modified) clang/lib/CodeGen/Targets/PPC.cpp (+1-1)
  • (modified) clang/lib/Sema/SemaTemplate.cpp (+1-1)
diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index b873bc6737bb0a..c3774d0cb75edc 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -289,7 +289,7 @@ class ComplexExprEmitter
                                         const BinOpInfo &Op);
 
   QualType GetHigherPrecisionFPType(QualType ElementType) {
-    const auto *CurrentBT = dyn_cast<BuiltinType>(ElementType);
+    const auto *CurrentBT = cast<BuiltinType>(ElementType);
     switch (CurrentBT->getKind()) {
     case BuiltinType::Kind::Float16:
       return CGF.getContext().FloatTy;
diff --git a/clang/lib/CodeGen/Targets/PPC.cpp b/clang/lib/CodeGen/Targets/PPC.cpp
index 00b04723f17dd2..3eadb19bd2058f 100644
--- a/clang/lib/CodeGen/Targets/PPC.cpp
+++ b/clang/lib/CodeGen/Targets/PPC.cpp
@@ -274,7 +274,7 @@ void AIXTargetCodeGenInfo::setTargetAttributes(
   if (!isa<llvm::GlobalVariable>(GV))
     return;
 
-  auto *GVar = dyn_cast<llvm::GlobalVariable>(GV);
+  auto *GVar = cast<llvm::GlobalVariable>(GV);
   auto GVId = GV->getName();
 
   // Is this a global variable specified by the user as toc-data?
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 005529a53270c3..aab72dbaf48c46 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -2974,7 +2974,7 @@ void DeclareImplicitDeductionGuidesForTypeAlias(
     if (auto *FPrime = SemaRef.InstantiateFunctionDeclaration(
             F, TemplateArgListForBuildingFPrime, AliasTemplate->getLocation(),
             Sema::CodeSynthesisContext::BuildingDeductionGuides)) {
-      auto *GG = dyn_cast<CXXDeductionGuideDecl>(FPrime);
+      auto *GG = cast<CXXDeductionGuideDecl>(FPrime);
       buildDeductionGuide(SemaRef, AliasTemplate, FPrimeTemplateParamList,
                           GG->getCorrespondingConstructor(),
                           GG->getExplicitSpecifier(), GG->getTypeSourceInfo(),

@llvmbot
Copy link
Member

llvmbot commented Mar 27, 2024

@llvm/pr-subscribers-backend-powerpc

Author: None (smanna12)

Changes

This patch replaces dyn_cast<> with cast<> to resolve potential static analyzer bugs for

  1. Dereferencing a pointer issue with nullptr GVar when calling addAttribute() in AIXTargetCodeGenInfo::setTargetAttributes(clang::Decl const *, llvm::GlobalValue *, clang::CodeGen::CodeGenModule &).

  2. Dereferencing a pointer issue with nullptr GG when calling getCorrespondingConstructor() in DeclareImplicitDeductionGuidesForTypeAlias(clang::Sema &, clang::TypeAliasTemplateDecl *, clang::SourceLocation).

  3. Dereferencing a pointer issue with nullptr CurrentBT when calling getKind() in ComplexExprEmitter::GetHigherPrecisionFPType(clang::QualType).


Full diff: https://github.com/llvm/llvm-project/pull/86759.diff

3 Files Affected:

  • (modified) clang/lib/CodeGen/CGExprComplex.cpp (+1-1)
  • (modified) clang/lib/CodeGen/Targets/PPC.cpp (+1-1)
  • (modified) clang/lib/Sema/SemaTemplate.cpp (+1-1)
diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index b873bc6737bb0a..c3774d0cb75edc 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -289,7 +289,7 @@ class ComplexExprEmitter
                                         const BinOpInfo &Op);
 
   QualType GetHigherPrecisionFPType(QualType ElementType) {
-    const auto *CurrentBT = dyn_cast<BuiltinType>(ElementType);
+    const auto *CurrentBT = cast<BuiltinType>(ElementType);
     switch (CurrentBT->getKind()) {
     case BuiltinType::Kind::Float16:
       return CGF.getContext().FloatTy;
diff --git a/clang/lib/CodeGen/Targets/PPC.cpp b/clang/lib/CodeGen/Targets/PPC.cpp
index 00b04723f17dd2..3eadb19bd2058f 100644
--- a/clang/lib/CodeGen/Targets/PPC.cpp
+++ b/clang/lib/CodeGen/Targets/PPC.cpp
@@ -274,7 +274,7 @@ void AIXTargetCodeGenInfo::setTargetAttributes(
   if (!isa<llvm::GlobalVariable>(GV))
     return;
 
-  auto *GVar = dyn_cast<llvm::GlobalVariable>(GV);
+  auto *GVar = cast<llvm::GlobalVariable>(GV);
   auto GVId = GV->getName();
 
   // Is this a global variable specified by the user as toc-data?
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 005529a53270c3..aab72dbaf48c46 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -2974,7 +2974,7 @@ void DeclareImplicitDeductionGuidesForTypeAlias(
     if (auto *FPrime = SemaRef.InstantiateFunctionDeclaration(
             F, TemplateArgListForBuildingFPrime, AliasTemplate->getLocation(),
             Sema::CodeSynthesisContext::BuildingDeductionGuides)) {
-      auto *GG = dyn_cast<CXXDeductionGuideDecl>(FPrime);
+      auto *GG = cast<CXXDeductionGuideDecl>(FPrime);
       buildDeductionGuide(SemaRef, AliasTemplate, FPrimeTemplateParamList,
                           GG->getCorrespondingConstructor(),
                           GG->getExplicitSpecifier(), GG->getTypeSourceInfo(),

Copy link
Contributor

@tahonermann tahonermann left a comment

Choose a reason for hiding this comment

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

Looks good, thanks @smanna12!

const auto *CurrentBT = dyn_cast<BuiltinType>(ElementType);
const auto *CurrentBT = cast<BuiltinType>(ElementType);
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks fine and is consistent with other type assumptions elsewhere in the same source file.

auto *GG = dyn_cast<CXXDeductionGuideDecl>(FPrime);
auto *GG = cast<CXXDeductionGuideDecl>(FPrime);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this looks fine too. It is not trivial to ensure, but seems reasonable.

@smanna12
Copy link
Contributor Author

Thank you @tahonermann for reviews!

@smanna12 smanna12 merged commit 1095f71 into llvm:main Mar 28, 2024
@smanna12 smanna12 deleted the FixStaticAnalyzerBugs branch March 28, 2024 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:PowerPC clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants