Skip to content

Conversation

@smanna12
Copy link
Contributor

This commit addresses several null pointer issues identified by static analysis by replacing dyn_cast<> with cast<> and getAs<> with castAs<> in various parts of the Clang codebase. The cast and castAs method is used to ensure that the type is correctly cast, which helps prevent potential null pointer dereferences.

Changes:

  1. ASTContext.cpp:
    Replaced dyn_cast with cast to ensure that the type is correctly cast to AttributedType.

  2. SemaFunctionEffects.cpp:
    Replaced getAs with castAs to ensure that the type is correctly cast to FunctionProtoType.

  3. SemaHLSL.cpp:
    Replaced getAs with castAs to ensure that the type is correctly cast to VectorType.

@smanna12 smanna12 requested a review from Fznamznon November 21, 2024 15:27
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support labels Nov 21, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2024

@llvm/pr-subscribers-clang

Author: None (smanna12)

Changes

This commit addresses several null pointer issues identified by static analysis by replacing dyn_cast<> with cast<> and getAs<> with castAs<> in various parts of the Clang codebase. The cast and castAs method is used to ensure that the type is correctly cast, which helps prevent potential null pointer dereferences.

Changes:

  1. ASTContext.cpp:
    Replaced dyn_cast with cast to ensure that the type is correctly cast to AttributedType.

  2. SemaFunctionEffects.cpp:
    Replaced getAs with castAs to ensure that the type is correctly cast to FunctionProtoType.

  3. SemaHLSL.cpp:
    Replaced getAs with castAs to ensure that the type is correctly cast to VectorType.


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

3 Files Affected:

  • (modified) clang/lib/AST/ASTContext.cpp (+1-1)
  • (modified) clang/lib/Sema/SemaFunctionEffects.cpp (+1-1)
  • (modified) clang/lib/Sema/SemaHLSL.cpp (+2-2)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 14fbadbc35ae5d..23df7878a3bf29 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -3558,7 +3558,7 @@ ASTContext::adjustType(QualType Orig,
                        llvm::function_ref<QualType(QualType)> Adjust) const {
   switch (Orig->getTypeClass()) {
   case Type::Attributed: {
-    const auto *AT = dyn_cast<AttributedType>(Orig);
+    const auto *AT = cast<AttributedType>(Orig);
     return getAttributedType(AT->getAttrKind(),
                              adjustType(AT->getModifiedType(), Adjust),
                              adjustType(AT->getEquivalentType(), Adjust),
diff --git a/clang/lib/Sema/SemaFunctionEffects.cpp b/clang/lib/Sema/SemaFunctionEffects.cpp
index 6fe4d2353a2282..c5c1e3fb41a2ff 100644
--- a/clang/lib/Sema/SemaFunctionEffects.cpp
+++ b/clang/lib/Sema/SemaFunctionEffects.cpp
@@ -627,7 +627,7 @@ class Analyzer {
           IsNoexcept = isNoexcept(FD);
         } else if (auto *BD = dyn_cast<BlockDecl>(D)) {
           if (auto *TSI = BD->getSignatureAsWritten()) {
-            auto *FPT = TSI->getType()->getAs<FunctionProtoType>();
+            auto *FPT = TSI->getType()->castAs<FunctionProtoType>();
             IsNoexcept = FPT->isNothrow() || BD->hasAttr<NoThrowAttr>();
           }
         }
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index f4fc0f2ddc27a6..a1adc66ddb9ce9 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1908,9 +1908,9 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
       return true;
     // ensure both args have 3 elements
     int NumElementsArg1 =
-        TheCall->getArg(0)->getType()->getAs<VectorType>()->getNumElements();
+        TheCall->getArg(0)->getType()->casAs<VectorType>()->getNumElements();
     int NumElementsArg2 =
-        TheCall->getArg(1)->getType()->getAs<VectorType>()->getNumElements();
+        TheCall->getArg(1)->getType()->castAs<VectorType>()->getNumElements();
 
     if (NumElementsArg1 != 3) {
       int LessOrMore = NumElementsArg1 > 3 ? 1 : 0;

@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2024

@llvm/pr-subscribers-hlsl

Author: None (smanna12)

Changes

This commit addresses several null pointer issues identified by static analysis by replacing dyn_cast<> with cast<> and getAs<> with castAs<> in various parts of the Clang codebase. The cast and castAs method is used to ensure that the type is correctly cast, which helps prevent potential null pointer dereferences.

Changes:

  1. ASTContext.cpp:
    Replaced dyn_cast with cast to ensure that the type is correctly cast to AttributedType.

  2. SemaFunctionEffects.cpp:
    Replaced getAs with castAs to ensure that the type is correctly cast to FunctionProtoType.

  3. SemaHLSL.cpp:
    Replaced getAs with castAs to ensure that the type is correctly cast to VectorType.


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

3 Files Affected:

  • (modified) clang/lib/AST/ASTContext.cpp (+1-1)
  • (modified) clang/lib/Sema/SemaFunctionEffects.cpp (+1-1)
  • (modified) clang/lib/Sema/SemaHLSL.cpp (+2-2)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 14fbadbc35ae5d..23df7878a3bf29 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -3558,7 +3558,7 @@ ASTContext::adjustType(QualType Orig,
                        llvm::function_ref<QualType(QualType)> Adjust) const {
   switch (Orig->getTypeClass()) {
   case Type::Attributed: {
-    const auto *AT = dyn_cast<AttributedType>(Orig);
+    const auto *AT = cast<AttributedType>(Orig);
     return getAttributedType(AT->getAttrKind(),
                              adjustType(AT->getModifiedType(), Adjust),
                              adjustType(AT->getEquivalentType(), Adjust),
diff --git a/clang/lib/Sema/SemaFunctionEffects.cpp b/clang/lib/Sema/SemaFunctionEffects.cpp
index 6fe4d2353a2282..c5c1e3fb41a2ff 100644
--- a/clang/lib/Sema/SemaFunctionEffects.cpp
+++ b/clang/lib/Sema/SemaFunctionEffects.cpp
@@ -627,7 +627,7 @@ class Analyzer {
           IsNoexcept = isNoexcept(FD);
         } else if (auto *BD = dyn_cast<BlockDecl>(D)) {
           if (auto *TSI = BD->getSignatureAsWritten()) {
-            auto *FPT = TSI->getType()->getAs<FunctionProtoType>();
+            auto *FPT = TSI->getType()->castAs<FunctionProtoType>();
             IsNoexcept = FPT->isNothrow() || BD->hasAttr<NoThrowAttr>();
           }
         }
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index f4fc0f2ddc27a6..a1adc66ddb9ce9 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1908,9 +1908,9 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
       return true;
     // ensure both args have 3 elements
     int NumElementsArg1 =
-        TheCall->getArg(0)->getType()->getAs<VectorType>()->getNumElements();
+        TheCall->getArg(0)->getType()->casAs<VectorType>()->getNumElements();
     int NumElementsArg2 =
-        TheCall->getArg(1)->getType()->getAs<VectorType>()->getNumElements();
+        TheCall->getArg(1)->getType()->castAs<VectorType>()->getNumElements();
 
     if (NumElementsArg1 != 3) {
       int LessOrMore = NumElementsArg1 > 3 ? 1 : 0;

@smanna12 smanna12 requested a review from s-perron November 21, 2024 15:51
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@Fznamznon Fznamznon left a comment

Choose a reason for hiding this comment

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

My concerns are resolved, sorry for holding this!

@smanna12
Copy link
Contributor Author

Thank you everyone for reviews.

@smanna12
Copy link
Contributor Author

My concerns are resolved, sorry for holding this!

No worries. Thank you

@smanna12 smanna12 merged commit 4389220 into llvm:main Nov 22, 2024
8 checks passed
@smanna12 smanna12 deleted the null_bugs_fixes branch November 22, 2024 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category HLSL HLSL Language Support

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

7 participants