[clang] [clang][Parser] Fix crash of clang when trying to convert a cast to a nullptr casted to an array of non-constant size to a reference (#78841). (PR #78889)

via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 21 00:17:19 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (ChipsSpectre)

<details>
<summary>Changes</summary>

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 the crash in #<!-- -->78841.

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


1 Files Affected:

- (modified) clang/lib/AST/ExprConstant.cpp (+6) 


``````````diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index f1d07d022b2584..ced1e72f845e10 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -1718,6 +1718,12 @@ namespace {
         Designator.setInvalid();
         return;
       }
+      if (!Base) {
+        // Can not perform cast if there is no underlying type.
+        Info.CCEDiag(E, diag::err_cast_selector_expr);
+        Designator.setInvalid();
+        return;
+      }
       if (checkSubobject(Info, E, CSK_ArrayToPointer)) {
         assert(getType(Base)->isPointerType() || getType(Base)->isArrayType());
         Designator.FirstEntryIsAnUnsizedArray = true;

``````````

</details>


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


More information about the cfe-commits mailing list