[PATCH] D119525: [clang] Fix crash when array size is missing in initializer

Timm B├Ąder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 10 23:47:50 PST 2022


tbaeder updated this revision to Diff 407785.
tbaeder added a comment.

This is a pretty big gotcha at best and I feel like `getArraySize()` should return `None` in that case instead... thoughts?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D119525/new/

https://reviews.llvm.org/D119525

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/test/AST/issue53742.cpp


Index: clang/test/AST/issue53742.cpp
===================================================================
--- /dev/null
+++ clang/test/AST/issue53742.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fsyntax-only %s -verify
+
+struct Data {
+  char *a;
+  char *b;
+  bool *c;
+};
+
+int main() {
+  Data in;
+  in.a = new char[](); // expected-error {{cannot determine allocated array size from initializer}}
+  in.c = new bool[100]();
+  in.b = new char[100]();
+}
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -9427,7 +9427,8 @@
   bool ValueInit = false;
 
   QualType AllocType = E->getAllocatedType();
-  if (Optional<const Expr*> ArraySize = E->getArraySize()) {
+  Optional<const Expr *> ArraySize = E->getArraySize();
+  if (ArraySize && *ArraySize) {
     const Expr *Stripped = *ArraySize;
     for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped);
          Stripped = ICE->getSubExpr())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119525.407785.patch
Type: text/x-patch
Size: 1027 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220211/30f556fa/attachment.bin>


More information about the cfe-commits mailing list