[clang] [Clang] Fix crash on improper use of __array_extent (PR #94173)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 3 06:22:11 PDT 2024


https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/94173

>From b4f5d6d43d369649711cece6057c8fe2758a5a89 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Mon, 3 Jun 2024 00:22:02 +0300
Subject: [PATCH 1/2] fix(80474): use expression error on incomplete
 __array_extent

---
 clang/docs/ReleaseNotes.rst                    | 1 +
 clang/lib/Parse/ParseExprCXX.cpp               | 3 +++
 clang/test/SemaCXX/incomplete-array-extent.cpp | 5 +++++
 3 files changed, 9 insertions(+)
 create mode 100644 clang/test/SemaCXX/incomplete-array-extent.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0c700d23257bf..32515fbac64f6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -823,6 +823,7 @@ Bug Fixes to C++ Support
   differering by their constraints when only one of these function was variadic.
 - Fix a crash when a variable is captured by a block nested inside a lambda. (Fixes #GH93625).
 - Fixed a type constraint substitution issue involving a generic lambda expression. (#GH93821)
+- Fix a crash caused by improper use of ``__array_extent``. (#GH80474)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp
index 1558e3dcb8974..6f21a4f9bd826 100644
--- a/clang/lib/Parse/ParseExprCXX.cpp
+++ b/clang/lib/Parse/ParseExprCXX.cpp
@@ -4011,6 +4011,9 @@ ExprResult Parser::ParseArrayTypeTrait() {
     ExprResult DimExpr = ParseExpression();
     T.consumeClose();
 
+    if (DimExpr.isInvalid())
+      return ExprError();
+
     return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
                                        T.getCloseLocation());
   }
diff --git a/clang/test/SemaCXX/incomplete-array-extent.cpp b/clang/test/SemaCXX/incomplete-array-extent.cpp
new file mode 100644
index 0000000000000..d59800f67a6ae
--- /dev/null
+++ b/clang/test/SemaCXX/incomplete-array-extent.cpp
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -verify -std=c++11 %s
+
+auto f() { // expected-error {{'auto' return without trailing return type; deduced return types are a C++14 extension}}
+  return __array_extent(int, ); // expected-error {{expected expression}}
+}

>From 5415766441ece7516cfea6d577988e73047873de Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Mon, 3 Jun 2024 09:58:00 +0300
Subject: [PATCH 2/2] use c++14 to reduce unnecessary diagnostics

---
 clang/test/SemaCXX/incomplete-array-extent.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/SemaCXX/incomplete-array-extent.cpp b/clang/test/SemaCXX/incomplete-array-extent.cpp
index d59800f67a6ae..8134af6b9251b 100644
--- a/clang/test/SemaCXX/incomplete-array-extent.cpp
+++ b/clang/test/SemaCXX/incomplete-array-extent.cpp
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -verify -std=c++11 %s
+// RUN: %clang_cc1 -verify -std=c++14 %s
 
-auto f() { // expected-error {{'auto' return without trailing return type; deduced return types are a C++14 extension}}
+auto f() {
   return __array_extent(int, ); // expected-error {{expected expression}}
 }



More information about the cfe-commits mailing list