Re: undersized unions

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: undersized unions
Date: 2023-02-05 01:18:14
Message-ID: Y98D1rL496qL8q35@paquier.xyz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Feb 04, 2023 at 05:07:08AM -0800, Andres Freund wrote:
> <source>: In function 'assign':
> <source>:9:6: warning: array subscript 'foo[0]' is partly outside array bounds of 'unsigned char[4]' [-Warray-bounds=]
> 9 | p->i = i;
> | ^~
> <source>:8:22: note: object of size 4 allocated by '__builtin_malloc'
> 8 | foo *p = (foo *) __builtin_malloc(sizeof(int));
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Compiler returned: 0
>
> I can't really tell if gcc is right or wrong wrong to warn about
> this. On the one hand it's a union, and we only access the element that
> is actually backed by memory, on the other hand, the standard does say
> that the size of a union is the largest element, so we are pointing to
> something undersized.

Something I have noticed, related to that.. meson reports a set of
warnings here, not ./configure, still I apply the same set of CFLAGS
to both. What's the difference in the meson setup that creates that,
if I may ask? There is a link to the way -Warray-bound is handled?

> 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 LTO), we end up
> with more of such warnings. I'd like to annotate palloc so gcc
> understands the size, as that does help to catch bugs when confusing the
> type. It also helps static analyzers.

Ah, that seems like a good idea in the long run.
--
Michael

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2023-02-05 01:30:25 Re: pg_stat_statements and "IN" conditions
Previous Message Michael Paquier 2023-02-05 00:49:57 Re: Weird failure with latches in curculio on v15