Re: Likely undefined behavior with some flexible arrays

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Likely undefined behavior with some flexible arrays
Date: 2026-01-21 22:07:04
Message-ID: 2432554.1769033224@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Andres Freund <andres(at)anarazel(dot)de> writes:
> I got a - I thought - spurious warning in a development patch. A simplified
> reproducer of the warning is [1], which triggers:

> <source>: In function 'trigger_warning':
> <source>:19:9: warning: array subscript 'struct foo[0]' is partly outside array bounds of 'unsigned char[13]' [-Warray-bounds=]
> 19 | foop->len = len;
> | ^~
> <source>:18:12: note: object of size 13 allocated by 'allocme'
> 18 | foop = allocme(offsetof(struct foo, data) + sizeof(char) * len);
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Compiler returned: 0

Considering that palloc() is going to round up the request to a
maxalign boundary, I think the chances of actual trouble are
precisely zero. However, if we start getting such warnings on
common compilers, maybe the way to fix it is to put the maxaligns
into the calls?

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Sami Imseih 2026-01-21 22:22:11 Re: POC: Parallel processing of indexes in autovacuum
Previous Message Andres Freund 2026-01-21 21:49:39 Likely undefined behavior with some flexible arrays