public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/100124] New: Why is "flexible array member '...' in an otherwise empty '...'" an issue?
@ 2021-04-16 20:18 gcc at behdad dot org
  2021-04-16 20:50 ` [Bug c++/100124] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: gcc at behdad dot org @ 2021-04-16 20:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100124

            Bug ID: 100124
           Summary: Why is "flexible array member '...' in an otherwise
                    empty '...'" an issue?
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at behdad dot org
  Target Milestone: ---

Hi,

In HarfBuzz we extensively use the Struct Hack [0]. Up until now we've used
"array[1]" for that. This upsets ubsan [1]. So I'm looking to replace it with
flexible arrays on compilers that support it.

However, trying to do that I get error if the flexible-array is the only
member. Clang has no issues with it, but gcc gives me: "flexible array member
'...' in an otherwise empty '...'".


```
struct UnsignedArray {
  int array[];
};
```

I've seen:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69550
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71742

which suggests that disallowing flexible arrays in structs that have no other
named member is intentional. But I cannot find the reason. Can someone please
help me understand, and suggest a solution here?

Note that if I use `int array[0]` instead, then gcc warns everytime in code we
access that array with a constexpr value, like `array[0]` when we know it's
safe to do. That is, gcc seems to treat `int array[0]` literally, not as a
flexible array.

Thank you.


[0] http://c-faq.com/struct/structhack.html
[1] https://github.com/harfbuzz/harfbuzz/issues/2953

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100124] Why is "flexible array member '...' in an otherwise empty '...'" an issue?
  2021-04-16 20:18 [Bug c/100124] New: Why is "flexible array member '...' in an otherwise empty '...'" an issue? gcc at behdad dot org
@ 2021-04-16 20:50 ` pinskia at gcc dot gnu.org
  2021-04-16 21:02 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-04-16 20:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100124

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I will let someone comment on the flexible array extension.

But I will note GCC treats (all) arrays at the end of a POD struct as a
flexible one so the question I have is more about the ubsan issue you are
running into, is that with GCC or with clang/LLVM?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100124] Why is "flexible array member '...' in an otherwise empty '...'" an issue?
  2021-04-16 20:18 [Bug c/100124] New: Why is "flexible array member '...' in an otherwise empty '...'" an issue? gcc at behdad dot org
  2021-04-16 20:50 ` [Bug c++/100124] " pinskia at gcc dot gnu.org
@ 2021-04-16 21:02 ` jakub at gcc dot gnu.org
  2021-04-17  0:03 ` msebor at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-04-16 21:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100124

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
ISO C99 clearly says so:
6.7.2.1/16:
"As a special case, the last element of a structure with more than one named
member may have an incomplete array type; this is called a flexible array
member."

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100124] Why is "flexible array member '...' in an otherwise empty '...'" an issue?
  2021-04-16 20:18 [Bug c/100124] New: Why is "flexible array member '...' in an otherwise empty '...'" an issue? gcc at behdad dot org
  2021-04-16 20:50 ` [Bug c++/100124] " pinskia at gcc dot gnu.org
  2021-04-16 21:02 ` jakub at gcc dot gnu.org
@ 2021-04-17  0:03 ` msebor at gcc dot gnu.org
  2021-04-17  0:51 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-17  0:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100124

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
The reason why G++ rejects structs with a flexible array as the sole member is
because GCC (in C mode) doesn't accept them.  Git log indicates that GCC
started rejecting such code in r38705, which was committed as part of array
member initialization cleanup.  I don't know why the author of the change chose
to reject such structs rather than accepting them with a warning, just like
empty structs are accepted.  If he had, C++ might have followed suit.  At this
point, I don't see GCC (in either mode) making a change unless C itself were to
change first.  That seems highly unlikely to me (the last proposal to relax the
rules for flexible array members didn't go anywhere).

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100124] Why is "flexible array member '...' in an otherwise empty '...'" an issue?
  2021-04-16 20:18 [Bug c/100124] New: Why is "flexible array member '...' in an otherwise empty '...'" an issue? gcc at behdad dot org
                   ` (2 preceding siblings ...)
  2021-04-17  0:03 ` msebor at gcc dot gnu.org
@ 2021-04-17  0:51 ` pinskia at gcc dot gnu.org
  2021-04-17 21:13 ` msebor at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-04-17  0:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100124

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #3)
> The reason why G++ rejects structs with a flexible array as the sole member
> is because GCC (in C mode) doesn't accept them.  Git log indicates that GCC
> started rejecting such code in r38705

Note this change was done long before clang was around so it would be
interesting to know why clang chose differently.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100124] Why is "flexible array member '...' in an otherwise empty '...'" an issue?
  2021-04-16 20:18 [Bug c/100124] New: Why is "flexible array member '...' in an otherwise empty '...'" an issue? gcc at behdad dot org
                   ` (3 preceding siblings ...)
  2021-04-17  0:51 ` pinskia at gcc dot gnu.org
@ 2021-04-17 21:13 ` msebor at gcc dot gnu.org
  2021-04-20 19:32 ` gcc at behdad dot org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-17 21:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100124

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |69698

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
G++ accepted structs where a flexible array is the sole member until version 6.
 G++ 6 tightened up the rules (in r231665) to more closely match the C front
end.  I suspect Clang implemented the more permissive historical G++ behavior
in C++ mode for compatibility with G++ and the stricter GCC behavior in C mode,
and to this day hasn't changed.  My impression is that the historical G++
permissiveness was due to oversights rather than deliberate features (we're
tracking a few such outstanding issues in pr69698 that are yet to be fixed).


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69698
[Bug 69698] [meta-bug] flexible array members

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100124] Why is "flexible array member '...' in an otherwise empty '...'" an issue?
  2021-04-16 20:18 [Bug c/100124] New: Why is "flexible array member '...' in an otherwise empty '...'" an issue? gcc at behdad dot org
                   ` (4 preceding siblings ...)
  2021-04-17 21:13 ` msebor at gcc dot gnu.org
@ 2021-04-20 19:32 ` gcc at behdad dot org
  2021-04-20 20:30 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: gcc at behdad dot org @ 2021-04-20 19:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100124

--- Comment #6 from Behdad Esfahbod <gcc at behdad dot org> ---
Thank you all. I understand it's unlikely to happen at this point.


In reply to Andrew Pinski from comment #1)
> I will let someone comment on the flexible array extension.
> 
> But I will note GCC treats (all) arrays at the end of a POD struct as a
> flexible one so the question I have is more about the ubsan issue you are
> running into, is that with GCC or with clang/LLVM?

The ubsan issue reported to us is with gcc:
https://github.com/harfbuzz/harfbuzz/issues/2953#issuecomment-823460893

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100124] Why is "flexible array member '...' in an otherwise empty '...'" an issue?
  2021-04-16 20:18 [Bug c/100124] New: Why is "flexible array member '...' in an otherwise empty '...'" an issue? gcc at behdad dot org
                   ` (5 preceding siblings ...)
  2021-04-20 19:32 ` gcc at behdad dot org
@ 2021-04-20 20:30 ` pinskia at gcc dot gnu.org
  2021-04-20 22:16 ` gcc at behdad dot org
  2021-04-26 22:26 ` msebor at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-04-20 20:30 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100124

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Behdad Esfahbod from comment #6)
> > But I will note GCC treats (all) arrays at the end of a POD struct as a
> > flexible one so the question I have is more about the ubsan issue you are
> > running into, is that with GCC or with clang/LLVM?
> 
> The ubsan issue reported to us is with gcc:
> https://github.com/harfbuzz/harfbuzz/issues/2953#issuecomment-823460893

Can you report that as a bug as GCC's rule is treat all arrays that end a POD
as a flexiable array?  Please include the full preprocessed source that
produces the problem at runtime and the specific version of GCC that is used. 
This is a GCC bug in mind due to this unless there is another field missing
that is in the source that you are not showing.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100124] Why is "flexible array member '...' in an otherwise empty '...'" an issue?
  2021-04-16 20:18 [Bug c/100124] New: Why is "flexible array member '...' in an otherwise empty '...'" an issue? gcc at behdad dot org
                   ` (6 preceding siblings ...)
  2021-04-20 20:30 ` pinskia at gcc dot gnu.org
@ 2021-04-20 22:16 ` gcc at behdad dot org
  2021-04-26 22:26 ` msebor at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: gcc at behdad dot org @ 2021-04-20 22:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100124

--- Comment #8 from Behdad Esfahbod <gcc at behdad dot org> ---
(In reply to Andrew Pinski from comment #7)
> 
> Can you report that as a bug as GCC's rule is treat all arrays that end a
> POD as a flexiable array?  Please include the full preprocessed source that
> produces the problem at runtime and the specific version of GCC that is
> used.  This is a GCC bug in mind due to this unless there is another field
> missing that is in the source that you are not showing.

Thanks. Will do after reproducing.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100124] Why is "flexible array member '...' in an otherwise empty '...'" an issue?
  2021-04-16 20:18 [Bug c/100124] New: Why is "flexible array member '...' in an otherwise empty '...'" an issue? gcc at behdad dot org
                   ` (7 preceding siblings ...)
  2021-04-20 22:16 ` gcc at behdad dot org
@ 2021-04-26 22:26 ` msebor at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-26 22:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100124

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |WONTFIX

--- Comment #9 from Martin Sebor <msebor at gcc dot gnu.org> ---
Thus resolving as WONTFIX.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-04-26 22:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-16 20:18 [Bug c/100124] New: Why is "flexible array member '...' in an otherwise empty '...'" an issue? gcc at behdad dot org
2021-04-16 20:50 ` [Bug c++/100124] " pinskia at gcc dot gnu.org
2021-04-16 21:02 ` jakub at gcc dot gnu.org
2021-04-17  0:03 ` msebor at gcc dot gnu.org
2021-04-17  0:51 ` pinskia at gcc dot gnu.org
2021-04-17 21:13 ` msebor at gcc dot gnu.org
2021-04-20 19:32 ` gcc at behdad dot org
2021-04-20 20:30 ` pinskia at gcc dot gnu.org
2021-04-20 22:16 ` gcc at behdad dot org
2021-04-26 22:26 ` msebor at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).