public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* [wish] Flexible array members in unions
@ 2023-05-11 16:07 Alejandro Colomar
  2023-05-11 16:29 ` Alejandro Colomar
  0 siblings, 1 reply; 16+ messages in thread
From: Alejandro Colomar @ 2023-05-11 16:07 UTC (permalink / raw)
  To: GCC; +Cc: Alejandro Colomar


[-- Attachment #1.1: Type: text/plain, Size: 2869 bytes --]

Hi!

Currently, one can have pseudo-flexible array members in unions with
[0] syntax, but it's not allowed with [] syntax.

Here's an example of how it is possible today:

struct s {
	...

	size_t  n;
	union {
		ptrdiff_t  off[0];  // [n]; offsets from s->data.
		char       data[0];
	};
};

which is useful to have a structure with two (or really several)
consecutive flexible arrays: one of offsets, which mark the positions
of data, and another with the actual data.  Below goes an example
program, which works fine with GCC, and I believe rewriting it to
not use the union would make it less clear, since I'd need to add
casts to it.

It works thanks to [0] pseudo-flexible arrays, but it doesn't
compile with C99 flexible arrays.  And of course, [0] arrays have
issues with -fstrict-flex-arrays=3.


$ cat flexi4.c 
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct s {
	size_t     n;
	union {
		ptrdiff_t  off[0];
		char       data[0];
	};
};

int
main(void)
{
	char      *p;
	struct s  *s;

	s = malloc(offsetof(struct s, off) +
	           sizeof(ptrdiff_t) * 2 +
	           sizeof("foobar") + sizeof("baz"));

	s->n = 2;
	p = s->data + sizeof(ptrdiff_t) * s->n;

	s->off[0] = p - s->data;
	p = stpcpy(p, "foobar") + 1;
	s->off[1] = p - s->data;
	p = stpcpy(p, "baz") + 1;

	puts(s->data + s->off[0]);
	puts(s->data + s->off[1]);

	free(s);
}
$ gcc-13 -Wall -Wextra -Werror -fanalyzer \
         -fsanitize=undefined -fsanitize=address \
         -D_FORTIFY_SOURCE=3 -fstrict-flex-arrays=2 \
         flexi4.c 
$ ./a.out 
foobar
baz
$ gcc-13 -Wall -Wextra -Werror -fanalyzer \
         -fsanitize=undefined -fsanitize=address \
         -D_FORTIFY_SOURCE=3 -fstrict-flex-arrays=3 \
         flexi4.c 
$ ./a.out 
flexi4.c:27:8: runtime error: index 0 out of bounds for type 'ptrdiff_t [*]'
flexi4.c:29:8: runtime error: index 1 out of bounds for type 'ptrdiff_t [*]'
flexi4.c:32:23: runtime error: index 0 out of bounds for type 'ptrdiff_t [*]'
foobar
flexi4.c:33:23: runtime error: index 1 out of bounds for type 'ptrdiff_t [*]'
baz


Would you allow flexible array members in unions?  Is there any
strong reason to disallow them?

Currently, I get:

$ gcc-13 -Wall -Wextra -fanalyzer \
         -fsanitize=undefined -fsanitize=address \
         -D_FORTIFY_SOURCE=3 -fstrict-flex-arrays=3 \
         flexi4-true.c 
flexi4-true.c:9:28: error: flexible array member in union
    9 |                 ptrdiff_t  off[];
      |                            ^~~
flexi4-true.c:10:28: error: flexible array member in union
   10 |                 char       data[];
      |                            ^~~~


Cheers,
Alex

-- 
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

end of thread, other threads:[~2023-05-19 12:08 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-11 16:07 [wish] Flexible array members in unions Alejandro Colomar
2023-05-11 16:29 ` Alejandro Colomar
2023-05-11 19:07   ` Kees Cook
2023-05-11 20:53     ` Joseph Myers
2023-05-11 21:13       ` Kees Cook
2023-05-11 21:43         ` Joseph Myers
2023-05-11 22:16           ` Kees Cook
2023-05-11 22:52             ` Joseph Myers
2023-05-12  0:25               ` Alejandro Colomar
2023-05-12  7:49             ` Jonathan Wakely
2023-05-12  6:16         ` Richard Biener
2023-05-12 12:32           ` David Brown
2023-05-15 19:58           ` Qing Zhao
2023-05-18 16:25           ` Martin Uecker
2023-05-18 20:59             ` Qing Zhao
2023-05-19 12:08               ` Martin Uecker

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).