Bug 72754 - pointer to an unnamed struct with a sole flexible array member accepted
Summary: pointer to an unnamed struct with a sole flexible array member accepted
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: flexmembers
  Show dependency treegraph
 
Reported: 2016-07-29 21:44 UTC by Martin Sebor
Modified: 2021-03-26 23:21 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.1.3, 4.2.2, 4.3.2, 4.6.0, 5.4.0, 6.3.0, 7.2.0, 8.0
Last reconfirmed: 2018-11-28 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2016-07-29 21:44:52 UTC
The following program defines two pointers to unnamed structs containing a flexible array as their sole member.  The structs are invalid and are rejected with an error in C mode but silently accepted by G++.

$ cat t.C && /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -S -Wall -Wextra -Wpedantic t.C
struct { int a[]; } *p;

struct S {
  struct { int a[]; } *q;
};

t.C:1:22: warning: ‘p’ defined but not used [-Wunused-variable]
 struct { int a[]; } *p;
                      ^

Clang produces the following output:

t.C:1:14: warning: flexible array member 'a' in otherwise empty struct is a GNU
      extension [-Wgnu-empty-struct]
struct { int a[]; } *p;
             ^
t.C:1:14: warning: flexible array members are a C99 feature [-Wc99-extensions]
t.C:4:16: warning: flexible array member 'a' in otherwise empty struct is a GNU
      extension [-Wgnu-empty-struct]
  struct { int a[]; } *q;
               ^
t.C:4:16: warning: flexible array members are a C99 feature [-Wc99-extensions]
t.C:1:22: warning: unused variable 'p' [-Wunused-variable]
struct { int a[]; } *p;
                     ^
5 warnings generated.
Comment 1 Eric Gallager 2017-08-28 15:13:19 UTC
gcc now produces at least 1 error in c++ mode:

$ /usr/local/bin/gcc -c -S -Wall -Wextra -Wpedantic -xc 72754.cc
72754.cc:1:14: error: flexible array member in a struct with no named members
 struct { int a[]; } *p;
              ^
72754.cc:4:15: error: flexible array member in a struct with no named members
  struct { int a[]; } *q;
               ^
$ /usr/local/bin/g++ -c -S -Wall -Wextra -Wpedantic -xc++ 72754.cc
72754.cc:1:16: warning: ISO C++ forbids flexible array member ‘a’ [-Wpedantic]
 struct { int a[]; } *p;
                ^
72754.cc:4:17: warning: ISO C++ forbids flexible array member ‘a’ [-Wpedantic]
  struct { int a[]; } *q;
                 ^
72754.cc:4:17: error: flexible array member ‘S::<unnamed struct>::a’ in an otherwise empty ‘struct S’
72754.cc:1:22: warning: ‘p’ defined but not used [-Wunused-variable]
 struct { int a[]; } *p;
                      ^
$ /sw/opt/llvm-3.1/bin/clang++ -c -S -Wall -Wextra -pedantic -xc++ 72754.cc
72754.cc:1:14: warning: flexible array member 'a' in otherwise empty struct is a GNU extension [-pedantic,-Wgnu]
struct { int a[]; } *p;
             ^
72754.cc:4:15: warning: flexible array member 'a' in otherwise empty struct is a GNU extension [-pedantic,-Wgnu]
        struct { int a[]; } *q;
                     ^
72754.cc:1:22: warning: unused variable 'p' [-Wunused-variable]
struct { int a[]; } *p;
                     ^
3 warnings generated.
$ 

Any idea when this was half-fixed?
Comment 2 Martin Sebor 2017-08-28 15:43:25 UTC
The second case has been diagnosed since r241143 (bug 71912) but the first one is still accepted.
Comment 3 Martin Sebor 2017-08-28 15:48:57 UTC
Bug 68489 tracks the failure to reject the corresponding array case.
Comment 4 Eric Gallager 2018-11-28 03:41:17 UTC
(In reply to Eric Gallager from comment #1)
> gcc now produces at least 1 error in c++ mode:
> 
> $ /usr/local/bin/gcc -c -S -Wall -Wextra -Wpedantic -xc 72754.cc
> 72754.cc:1:14: error: flexible array member in a struct with no named members
>  struct { int a[]; } *p;
>               ^
> 72754.cc:4:15: error: flexible array member in a struct with no named members
>   struct { int a[]; } *q;
>                ^
> $ /usr/local/bin/g++ -c -S -Wall -Wextra -Wpedantic -xc++ 72754.cc
> 72754.cc:1:16: warning: ISO C++ forbids flexible array member ‘a’
> [-Wpedantic]
>  struct { int a[]; } *p;
>                 ^
> 72754.cc:4:17: warning: ISO C++ forbids flexible array member ‘a’
> [-Wpedantic]
>   struct { int a[]; } *q;
>                  ^
> 72754.cc:4:17: error: flexible array member ‘S::<unnamed struct>::a’ in an
> otherwise empty ‘struct S’
> 72754.cc:1:22: warning: ‘p’ defined but not used [-Wunused-variable]
>  struct { int a[]; } *p;
>                       ^
> $ /sw/opt/llvm-3.1/bin/clang++ -c -S -Wall -Wextra -pedantic -xc++ 72754.cc
> 72754.cc:1:14: warning: flexible array member 'a' in otherwise empty struct
> is a GNU extension [-pedantic,-Wgnu]
> struct { int a[]; } *p;
>              ^
> 72754.cc:4:15: warning: flexible array member 'a' in otherwise empty struct
> is a GNU extension [-pedantic,-Wgnu]
>         struct { int a[]; } *q;
>                      ^
> 72754.cc:1:22: warning: unused variable 'p' [-Wunused-variable]
> struct { int a[]; } *p;
>                      ^
> 3 warnings generated.
> $ 
> 

Let me update the comparison output for clang to a newer version of it (6):

$ clang++ -c -S -Wall -Wextra -pedantic -xc++ 72754.cc
72754.cc:1:14: warning: flexible array member 'a' in otherwise empty struct is a GNU extension [-Wgnu-empty-struct]
struct { int a[]; } *p;
             ^
72754.cc:1:14: warning: flexible array members are a C99 feature [-Wc99-extensions]
72754.cc:4:15: warning: flexible array member 'a' in otherwise empty struct is a GNU extension [-Wgnu-empty-struct]
        struct { int a[]; } *q;
                     ^
72754.cc:4:15: warning: flexible array members are a C99 feature [-Wc99-extensions]
72754.cc:1:22: warning: unused variable 'p' [-Wunused-variable]
struct { int a[]; } *p;
                     ^
5 warnings generated.
$

(In reply to Martin Sebor from comment #2)
> The second case has been diagnosed since r241143 (bug 71912) but the first
> one is still accepted.

Guess I should mark this as confirmed then.