Re: undersized unions

2023-02-06 Thread Robert Haas
On Mon, Feb 6, 2023 at 1:28 PM Andres Freund wrote: > Perhaps something like Yeah, that'd work. You'd want a big ol' warning comment here: > typedef struct NumericData > { > int32 vl_len_;/* varlena header (do not > touch directly!) */ > NumericBase dat

Re: undersized unions

2023-02-06 Thread Andres Freund
Hi, On 2023-02-06 11:55:40 -0500, Tom Lane wrote: > I am, however, very dubious that Andres is correct that there's a > problem here. Given that two of the variants of union NumericChoice > are structs ending with a flexible array, any compiler that thinks > it knows the size of the union precise

Re: undersized unions

2023-02-06 Thread Andres Freund
Hi On 2023-02-06 11:42:57 -0500, Robert Haas wrote: > On Sun, Feb 5, 2023 at 6:28 AM Andres Freund wrote: > > On the other hand, it also just seems risky from a code writing > > perspective. It's not immediate obvious that it'd be unsafe to create an > > on-stack Numeric by assigning *ptr. But

Re: undersized unions

2023-02-06 Thread Tom Lane
Robert Haas writes: > I do not really know exactly how to do that, though. Our usual pattern > is to just have a struct and end with a variable-length array, or > alternatively add a comment says "other stuff follows!" at the end of > the struct definition, without doing anything that C knows abou

Re: undersized unions

2023-02-06 Thread Robert Haas
On Sun, Feb 5, 2023 at 6:28 AM Andres Freund wrote: > On the other hand, it also just seems risky from a code writing perspective. > It's not immediate obvious that it'd be unsafe to create an on-stack Numeric > by assigning *ptr. But it is. Well, I think that is pretty obvious: we have lots of

Re: undersized unions

2023-02-05 Thread Andres Freund
Hi, On 2023-02-05 10:18:14 +0900, Michael Paquier wrote: > On Sat, Feb 04, 2023 at 05:07:08AM -0800, Andres Freund wrote: > > : In function 'assign': > > :9:6: warning: array subscript 'foo[0]' is partly outside array > > bounds of 'unsigned char[4]' [-Warray-bounds=] > > 9 | p->i = i; >

Re: undersized unions

2023-02-05 Thread Andres Freund
Hi, On February 5, 2023 6:16:55 AM GMT+01:00, Tom Lane wrote: >Michael Paquier writes: >> On Sat, Feb 04, 2023 at 05:07:08AM -0800, Andres Freund wrote: >>> We actually have a fair amount of code like that, but currently are >>> escaping most of the warnings, because gcc doesn't know that pallo

Re: undersized unions

2023-02-04 Thread Tom Lane
Michael Paquier writes: > On Sat, Feb 04, 2023 at 05:07:08AM -0800, Andres Freund wrote: >> We actually have a fair amount of code like that, but currently are >> escaping most of the warnings, because gcc doesn't know that palloc() is >> an allocator. With more optimizations (particularly with LT

Re: undersized unions

2023-02-04 Thread Michael Paquier
On Sat, Feb 04, 2023 at 05:07:08AM -0800, Andres Freund wrote: > : In function 'assign': > :9:6: warning: array subscript 'foo[0]' is partly outside array > bounds of 'unsigned char[4]' [-Warray-bounds=] > 9 | p->i = i; > | ^~ > :8:22: note: object of size 4 allocated by '__buil