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

2023-05-22 Thread Mariya Podchishchaeva via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG8f8e450b6682: [clang] Fix crash on attempt to initialize 
union with flexible array member (authored by Fznamznon).

Changed prior to commit:
  https://reviews.llvm.org/D150435?vs=523723=524278#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D150435

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/flexible-array-in-union.c


Index: clang/test/Sema/flexible-array-in-union.c
===
--- /dev/null
+++ 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
+
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -811,7 +811,7 @@
   // 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);
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -418,6 +418,8 @@
 - Propagate the value-dependent bit for VAArgExpr. Fixes a crash where a
   __builtin_va_arg call has invalid arguments.
   (`#62711 `_).
+- Fix crash on attempt to initialize union with flexible array member.
+  (`#61746 `_).
 
 Bug Fixes to Compiler Builtins
 ^^


Index: clang/test/Sema/flexible-array-in-union.c
===
--- /dev/null
+++ 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
+
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -811,7 +811,7 @@
   // 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);
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -418,6 +418,8 @@
 - Propagate the value-dependent bit for VAArgExpr. Fixes a crash where a
   __builtin_va_arg call has invalid arguments.
   (`#62711 `_).
+- Fix crash on attempt to initialize union with flexible array member.
+  (`#61746 `_).
 
 Bug Fixes to Compiler Builtins
 ^^
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-05-22 Thread Mariya Podchishchaeva via Phabricator via cfe-commits
Fznamznon added inline comments.



Comment at: clang/test/Sema/flexible-array-in-union.c:1-16
+// RUN: %clang_cc1 %s -verify -fsyntax-only -DERROR
+// 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.

aaron.ballman wrote:
> Slight tweak to get rid of the preprocessor stuff and simplify the test a 
> bit; NFC.
Neat! Thank you.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D150435

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-05-22 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman accepted this revision.
aaron.ballman added a comment.

LGTM with a minor tweak to the test, thanks!




Comment at: clang/test/Sema/flexible-array-in-union.c:1-16
+// RUN: %clang_cc1 %s -verify -fsyntax-only -DERROR
+// 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.

Slight tweak to get rid of the preprocessor stuff and simplify the test a bit; 
NFC.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D150435

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-05-22 Thread Mariya Podchishchaeva via Phabricator via cfe-commits
Fznamznon added a comment.

@aaron.ballman , WDYT?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D150435

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-05-19 Thread Shafik Yaghmour via Phabricator via cfe-commits
shafik accepted this revision.
shafik added a comment.
This revision is now accepted and ready to land.

LGTM after addressing Aaron's comment


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D150435

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-05-19 Thread Mariya Podchishchaeva via Phabricator via cfe-commits
Fznamznon updated this revision to Diff 523723.
Fznamznon added a comment.

Move the test to a separate file, test C++ and MSVC compatibility mode


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D150435

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/flexible-array-in-union.c


Index: clang/test/Sema/flexible-array-in-union.c
===
--- /dev/null
+++ clang/test/Sema/flexible-array-in-union.c
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only -DERROR
+// 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.
+
+#ifdef ERROR
+// expected-error@+2 {{flexible array member 'x' in a union is not allowed}}
+#endif // ERROR
+union { char x[]; } r = {0};
+
+#ifndef ERROR
+// expected-no-diagnostics
+#endif // ERROR
+
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -811,7 +811,7 @@
   // 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);
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -406,6 +406,8 @@
   when it had been instantiated from a partial template specialization with 
different
   template arguments on the containing class. This fixes:
   (`#60778 `_).
+- Fix crash on attempt to initialize union with flexible array member.
+  (`#61746 `_).
 
 Bug Fixes to Compiler Builtins
 ^^


Index: clang/test/Sema/flexible-array-in-union.c
===
--- /dev/null
+++ clang/test/Sema/flexible-array-in-union.c
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only -DERROR
+// 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.
+
+#ifdef ERROR
+// expected-error@+2 {{flexible array member 'x' in a union is not allowed}}
+#endif // ERROR
+union { char x[]; } r = {0};
+
+#ifndef ERROR
+// expected-no-diagnostics
+#endif // ERROR
+
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -811,7 +811,7 @@
   // 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);
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -406,6 +406,8 @@
   when it had been instantiated from a partial template specialization with different
   template arguments on the containing class. This fixes:
   (`#60778 `_).
+- Fix crash on attempt to initialize union with flexible array member.
+  (`#61746 `_).
 
 Bug Fixes to Compiler Builtins
 ^^
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-05-16 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

The precommit CI failures seem unrelated to these changes.




Comment at: clang/test/Sema/init.c:168-169
+
+// GH61746
+union { char x[]; } r = {0}; // expected-error {{flexible array member 'x' in 
a union is not allowed}}

I think we might want to move this to its own test file so we can test the 
various edge cases. I'd like to see a RUN line that tests explicitly in 
`-fms-compatibility` mode to demonstrate that we do not issue this diagnostic, 
and RUN lines for C++ mode as well.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D150435

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-05-12 Thread Mariya Podchishchaeva via Phabricator via cfe-commits
Fznamznon created this revision.
Herald added a project: All.
Fznamznon requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150435

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/init.c


Index: clang/test/Sema/init.c
===
--- clang/test/Sema/init.c
+++ clang/test/Sema/init.c
@@ -164,3 +164,6 @@
 
 typedef struct { uintptr_t x : 2; } StructWithBitfield;
 StructWithBitfield bitfieldvar = { (uintptr_t) }; // 
expected-error {{initializer element is not a compile-time constant}}
+
+// GH61746
+union { char x[]; } r = {0}; // expected-error {{flexible array member 'x' in 
a union is not allowed}}
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -811,7 +811,7 @@
   // 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);
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -395,6 +395,8 @@
   when it had been instantiated from a partial template specialization with 
different
   template arguments on the containing class. This fixes:
   (`#60778 `_).
+- Fix crash on attempt to initialize union with flexible array member.
+  (`#61746 `_).
 
 Bug Fixes to Compiler Builtins
 ^^


Index: clang/test/Sema/init.c
===
--- clang/test/Sema/init.c
+++ clang/test/Sema/init.c
@@ -164,3 +164,6 @@
 
 typedef struct { uintptr_t x : 2; } StructWithBitfield;
 StructWithBitfield bitfieldvar = { (uintptr_t) }; // expected-error {{initializer element is not a compile-time constant}}
+
+// GH61746
+union { char x[]; } r = {0}; // expected-error {{flexible array member 'x' in a union is not allowed}}
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -811,7 +811,7 @@
   // 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);
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -395,6 +395,8 @@
   when it had been instantiated from a partial template specialization with different
   template arguments on the containing class. This fixes:
   (`#60778 `_).
+- Fix crash on attempt to initialize union with flexible array member.
+  (`#61746 `_).
 
 Bug Fixes to Compiler Builtins
 ^^
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits