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

via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 20 02:33:16 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (ChipsSpectre)

<details>
<summary>Changes</summary>

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

``````````

</details>


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


More information about the cfe-commits mailing list