Skip to content
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

[Clang][Parser] Fix crash of clang when trying to convert a cast to … #78840

Closed
wants to merge 1 commit into from

Conversation

ChipsSpectre
Copy link
Contributor

…a nullptr casted to an array of non-constant size to a reference (#76634).

This situation is undefined behavior, and should not lead to a compiler crash. Thus, the problematic cast is only executed on non-null pointers.

Fixes one reason for a crash in #76634.

…a nullptr casted to an array of non-constant size to a reference (llvm#76634).

This situation is undefined behavior, and should not lead to a compiler crash.
Thus, the problematic cast is only executed on non-null pointers.

Fixes one reason for a crash in llvm#76634.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 20, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 20, 2024

@llvm/pr-subscribers-clang

Author: None (ChipsSpectre)

Changes

…a nullptr casted to an array of non-constant size to a reference (#76634).

This situation is undefined behavior, and should not lead to a compiler crash. Thus, the problematic cast is only executed on non-null pointers.

Fixes one reason for a crash in #76634.


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

1 Files Affected:

  • (modified) clang/lib/AST/ExprConstant.cpp (+10-3)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index f1d07d022b2584..165046bd06e92a 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9272,10 +9272,17 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
     }
     // The result is a pointer to the first element of the array.
     auto *AT = Info.Ctx.getAsArrayType(SubExpr->getType());
-    if (auto *CAT = dyn_cast<ConstantArrayType>(AT))
+    if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) {
       Result.addArray(Info, E, CAT);
-    else
-      Result.addUnsizedArray(Info, E, AT->getElementType());
+    }
+    else {
+      if (Result.checkNullPointer(Info, E, CSK_ArrayToPointer)) {
+        // Only add unsized array if there actually is a pointer.
+        return false;        
+      } else {
+        Result.addUnsizedArray(Info, E, AT->getElementType());
+      }
+    }
     return true;
   }
 

@ChipsSpectre ChipsSpectre marked this pull request as draft January 20, 2024 10:34
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff a7d7da6e45992b79fe712c1e228cc57c9f27fa7a 2503669a55f8dae534440a4eacb66a4500f78e3f -- clang/lib/AST/ExprConstant.cpp
View the diff from clang-format here.
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 165046bd06..dc9e1bcbd3 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9274,11 +9274,10 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
     auto *AT = Info.Ctx.getAsArrayType(SubExpr->getType());
     if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) {
       Result.addArray(Info, E, CAT);
-    }
-    else {
+    } else {
       if (Result.checkNullPointer(Info, E, CSK_ArrayToPointer)) {
         // Only add unsized array if there actually is a pointer.
-        return false;        
+        return false;
       } else {
         Result.addUnsizedArray(Info, E, AT->getElementType());
       }

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants