[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:16:48 PST 2024


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

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.

>From 2adcab3c99c2f10371516d211912d612ec6b59a4 Mon Sep 17 00:00:00 2001
From: ChipsSpectre <maximilian.hornung at tum.de>
Date: Sun, 21 Jan 2024 09:15:37 +0100
Subject: [PATCH] [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).

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.
---
 clang/lib/AST/ExprConstant.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

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;



More information about the cfe-commits mailing list