From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id B56DB3857730 for ; Fri, 19 May 2023 12:08:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B56DB3857730 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at Received: from vra-174-180.tugraz.at (vra-174-180.tugraz.at [129.27.174.180]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4QN5Fz2mgpz3wh8; Fri, 19 May 2023 14:08:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1684498088; bh=OFw3des+l9kCwLuB9r/ATMHVoY26GgxFvDNn1MDuFW0=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=eQjnsC5odfcvISy9qnAIutF7qMlppco+QLM1DaC8xfztY2i1BnU+2VATt50i/NVtg S+9P5fMEr6G9bGcJFkTOBmxLYZLm9mtBgpyaowDwz0MzEZC0pasRxFwh/UsJfLCrAZ 2br1geMIyGHAMOF2B85s8M0Wkt2z5NsQjtoSZfNg= Message-ID: <469d153cacf438e7cdc282710ad57e386c49c074.camel@tugraz.at> Subject: Re: [wish] Flexible array members in unions From: Martin Uecker To: Qing Zhao , Richard Biener , Joseph Myers Cc: GCC , Kees Cook Date: Fri, 19 May 2023 14:08:06 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.38.3-1+deb11u1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -0.4 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.117 X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,BODY_8BITS,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_SHORT,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Am Donnerstag, dem 18.05.2023 um 20:59 +0000 schrieb Qing Zhao: > > > On May 18, 2023, at 12:25 PM, Martin Uecker via Gcc wrote: > > > > > > > > > On Thu, May 11, 2023 at 11:14 PM Kees Cook via Gcc wrote: > > > > > > > > On Thu, May 11, 2023 at 08:53:52PM +0000, Joseph Myers wrote: > > > > > On Thu, 11 May 2023, Kees Cook via Gcc wrote: > > > > > > > > > > > On Thu, May 11, 2023 at 06:29:10PM +0200, Alejandro Colomar wrote: > > > > > > > On 5/11/23 18:07, Alejandro Colomar wrote: > > > > > > > [...] > > > > > > > > Would you allow flexible array members in unions? Is there any > > > > > > > > strong reason to disallow them? > > > > > > > > > > > > Yes please!! And alone in a struct, too. > > > > > > > > > > > > AFAICT, there is no mechanical/architectural reason to disallow them > > > > > > (especially since they _can_ be constructed with some fancy tricks, > > > > > > and they behave as expected.) My understanding is that it's disallowed > > > > > > due to an overly strict reading of the very terse language that created > > > > > > flexible arrays in C99. > > > > > > > > > > Standard C has no such thing as a zero-size object or type, which would > > > > > lead to problems with a struct or union that only contains a flexible > > > > > array member there. > > > > (I think it is fundamentally not too problematic to have zero-size > > objects, although it would take some work to specify the semantics > > exactly.) > > > > But my preference would be to make structs / unions with FAM an > > incomplete type which would then restrict their use (for the cases > > now supported we would need backwards compatible exceptions). > > We could then allow such a struct / union as the last member > > of another struct / union which would make this an incomplete > > type too. > > Yes, I like this approach. > And we can make them GCC extensions first, promote to C standard later. > > My proposed patch sets (originally targeted on GCC13, now might need to target on GCC14) will > make one part of the above a GCC extension: >     Allowing the struct with FAM as the last member of another struct. (See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101832) > > https://gcc.gnu.org/pipermail/gcc-patches/2023-March/614794.html > https://gcc.gnu.org/pipermail/gcc-patches/2023-March/614793.html > https://gcc.gnu.org/pipermail/gcc-patches/2023-March/614790.html > > I’d like these changes going into GCC first to improve this area. Seems reasonable to me. Thanks! Martin > > > > > We then would need a special macro (based on a builtin) instead > > of sizeof to get the size, but this would be safer anyway. > > > > In principle, an even better solution would be to allow dynamic > > arrays because then it has a dynamic bound where the type with > > the bound could propagate to some user. Bounds checking would > > work as expected and more cases. > > > > struct foo { > >  int len; > >  char buf[.len]; > > }; > > > > But this takes a bit more work to get right. > > > > > > > > > > Ah-ha, okay. That root cause makes sense now. > > > > > > Hmm. but then the workaround > > > > > > struct X { > > >  int n; > > >  union u { > > >      char at_least_size_one; > > >      int iarr[]; > > >      short sarr[]; > > >  }; > > > }; > > > > > > doesn't work either. We could make that a GNU extension without > > > adverse effects? > > > > I think we could allow this even without the "at_least_size_one" > > without a problem when allowing the use of such unions only as > > a last member of some structure. Allowing it elsewhere seems > > questionable anyway. > > Yes, Such an union can be treated as an flexible array member > (just multiple flexible arrays sharing the same storage). Therefore it’s reasonable > To only allow it as the last field of a structure. > > thanks. > > Qing. > > > > > > Richard. > > > > > > > Why are zero-sized objects missing in Standard C? Or, perhaps, the better > > > > question is: what's needed to support the idea of a zero-sized object? > > > > Probably a lot of convincing that it actually does not cause problems, > > and is useful. Also a lot of work in making sure the standard is revised > > everywhere where it is necessary. I think zero sized objects and > > especially arrays are very useful also to avoid special code for corner > > cases in numerical algorithms. But I think here some restrictions on > > the use of the FAM will do. > > > > > > Martin >