[clang] 8f8e450 - [clang] Fix crash on attempt to initialize union with flexible array member

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Mon May 22 06:33:41 PDT 2023


Author: Mariya Podchishchaeva
Date: 2023-05-22T09:32:04-04:00
New Revision: 8f8e450b668219c211284b68c36ba25b3193a2b0

URL: https://github.com/llvm/llvm-project/commit/8f8e450b668219c211284b68c36ba25b3193a2b0
DIFF: https://github.com/llvm/llvm-project/commit/8f8e450b668219c211284b68c36ba25b3193a2b0.diff

LOG: [clang] Fix crash on attempt to initialize union with flexible array member

Due to missing check on union, there was a null expression
added to init list that caused crash later.

Fixes https://github.com/llvm/llvm-project/issues/61746

Reviewed By: aaron.ballman, shafik

Differential Revision: https://reviews.llvm.org/D150435

Added: 
    clang/test/Sema/flexible-array-in-union.c

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaInit.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 911bd9eb6435..c80cc8994f38 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -418,6 +418,8 @@ Bug Fixes in This Version
 - Propagate the value-dependent bit for VAArgExpr. Fixes a crash where a
   __builtin_va_arg call has invalid arguments.
   (`#62711 <https://github.com/llvm/llvm-project/issues/62711>`_).
+- Fix crash on attempt to initialize union with flexible array member.
+  (`#61746 <https://github.com/llvm/llvm-project/issues/61746>`_).
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 4777f2d37b77..eaddba3e7c75 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -811,7 +811,7 @@ InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity,
       // order to leave them uninitialized, the ILE is expanded and the extra
       // fields are then filled with NoInitExpr.
       unsigned NumElems = numStructUnionElements(ILE->getType());
-      if (RDecl->hasFlexibleArrayMember())
+      if (!RDecl->isUnion() && RDecl->hasFlexibleArrayMember())
         ++NumElems;
       if (!VerifyOnly && ILE->getNumInits() < NumElems)
         ILE->resizeInits(SemaRef.Context, NumElems);

diff  --git a/clang/test/Sema/flexible-array-in-union.c b/clang/test/Sema/flexible-array-in-union.c
new file mode 100644
index 000000000000..5fabfbe0b1ea
--- /dev/null
+++ b/clang/test/Sema/flexible-array-in-union.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 %s -verify=c -fsyntax-only
+// RUN: %clang_cc1 %s -verify -fsyntax-only -x c++
+// RUN: %clang_cc1 %s -verify -fsyntax-only -fms-compatibility
+// RUN: %clang_cc1 %s -verify -fsyntax-only -fms-compatibility -x c++
+
+// The test checks that an attempt to initialize union with flexible array
+// member with an initializer list doesn't crash clang.
+
+
+union { char x[]; } r = {0}; // c-error {{flexible array member 'x' in a union is not allowed}}
+
+// expected-no-diagnostics
+


        


More information about the cfe-commits mailing list