Add compiler option -fstrict-flex-arrays=1

Поиск
Список
Период
Сортировка
Искать

Add compiler option -fstrict-flex-arrays=1

От:
Peter Eisentraut <peter@eisentraut.org>
Дата:
Вложения:
(I already posted this patch in another thread [0], but since this was 
under another topic, I'm posting it again here with a clearer subject.)

[0]: 
https://www.postgresql.org/message-id/flat/DLRI3Q0C0MR2.1V3TOIS3N16FJ%40partin.io

Compilers can check object sizes in various ways.  For example, there is 
-Warray-bounds, __builtin_object_size, which drives _FORTIFY_SOURCE, and 
also -fsanitize=bounds, which is part of ubsan.

One perhaps surprising aspect of that is that both gcc and clang by 
default assume that any trailing array in a struct could be used as a 
flexible array member, independent of its declared size, and therefore 
don't believe the size declaration and thus exempt that array from all 
those checks.

This patch fixes that.  Below is a relatively elaborate explanation, but 
the patch is actually small.

Here is an example of such a struct:

typedef struct
{
     ...

     char       *server_final_message;
     uint8       ServerSignature[SCRAM_MAX_KEY_LEN];
} fe_scram_state;

(SCRAM_MAX_KEY_LEN is 32.)

Because of the default assumption mentioned above, the compilers don't 
check that code doesn't overrun the ServerSignature field.

There are many other structs in our code that are similarly affected.

I suppose this behavior is to accommodate legacy code, but our code uses 
C99 flexible array members correctly (mostly, see below), so we can make 
these checks more strict and get more benefit from the various bounds 
checking mechanisms.

gcc and clang have the option -fstrict-flex-arrays [1] for this.

[1]: 
https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#index-fstrict-flex-arrays

Ideally, we'd want to use the maximum level -fstrict-flex-arrays=3, 
which would only allow flexible array members declared with the C 
standard "[]".  But we do need to permit flexible array members declared 
with "[1]" in a few cases because standard C does not allow flexible 
array members as the only element in a struct.  Affected are:

- struct RevmapContents
- struct ReplicationSlotCtlData
- union pgresult_data

(Some of these have comments "It should be declared 
[FLEXIBLE_ARRAY_MEMBER], but for some reason you can't do that in an 
otherwise-empty struct.")

There is also:

- struct sqlda_struct

which, as I understand it, is bound by external API specification, so 
not easy to change.

If we turned on -fstrict-flex-arrays=3, these would become undefined 
behavior and trigger the ubsan sanitizer.

If we use the lower level -fstrict-flex-arrays=1, then flexible-array 
members declared as "[]", "[0]", and "[1]" are permitted, but all the 
obviously-not-flexible-array-members like the fe_scram_state example 
above can now be bounds checked properly.  The only struct in the code 
that genuinely has a trailing one-element array is WordEntryPosVector1, 
and so we would miss bounds checking on that one, but I think we can 
live with that.

There is one piece of code that still needs fixing to permit 
-fstrict-flex-arrays=1, which was the topic of the [0] thread: 
JsonValueList has an array declared as "[2]" (via BASE_JVL_ITEMS) but 
addresses it as a flexible-length array in some circumstances.  My 
proposal included here is to change this to an anonymous union with one 
branch having the original size "[2]", to keep local variables sized the 
same as before, and one branch of size "[1]", which effectively makes 
this a flexible-array member under -fstrict-flex-arrays=1.

Re: Add compiler option -fstrict-flex-arrays=1

От:
David Geier <geidav.pg@gmail.com>
Дата:
Hi Peter!

> Ideally, we'd want to use the maximum level -fstrict-flex-arrays=3,
> which would only allow flexible array members declared with the C
> standard "[]".  But we do need to permit flexible array members declared
> with "[1]" in a few cases because standard C does not allow flexible
> array members as the only element in a struct.  Affected are:
> 
> - struct RevmapContents
> - struct ReplicationSlotCtlData
> - union pgresult_data
> 
> (Some of these have comments "It should be declared
> [FLEXIBLE_ARRAY_MEMBER], but for some reason you can't do that in an
> otherwise-empty struct.")

I think that's because sizeof any struct in C must be > 0. If a struct
contains no other member than an [] array, sizeof would be 0.

Can we maybe use typedefs instead of structs for the cases you
mentioned? At least it's valid C to write typedef char Foo[].

> There is also:
> 
> - struct sqlda_struct
> 
> which, as I understand it, is bound by external API specification, so
> not easy to change.
> 
> If we turned on -fstrict-flex-arrays=3, these would become undefined
> behavior and trigger the ubsan sanitizer.

The compiler setting can be changed on a per-struct basis via

__attribute__((strict_flex_arrays(N)));

--
David Geier


Re: Add compiler option -fstrict-flex-arrays=1

От:
Peter Eisentraut <peter@eisentraut.org>
Дата:
On 19.10.26 15:47, David Geier wrote:
> Hi Peter!
> 
>> Ideally, we'd want to use the maximum level -fstrict-flex-arrays=3,
>> which would only allow flexible array members declared with the C
>> standard "[]".  But we do need to permit flexible array members declared
>> with "[1]" in a few cases because standard C does not allow flexible
>> array members as the only element in a struct.  Affected are:
>>
>> - struct RevmapContents
>> - struct ReplicationSlotCtlData
>> - union pgresult_data
>>
>> (Some of these have comments "It should be declared
>> [FLEXIBLE_ARRAY_MEMBER], but for some reason you can't do that in an
>> otherwise-empty struct.")
> 
> I think that's because sizeof any struct in C must be > 0. If a struct
> contains no other member than an [] array, sizeof would be 0.

Yes, that's probably why.

> Can we maybe use typedefs instead of structs for the cases you
> mentioned? At least it's valid C to write typedef char Foo[].

Yeah, maybe some of this code could be rewritten to avoid this, but it 
doesn't seem very urgent.

>> There is also:
>>
>> - struct sqlda_struct
>>
>> which, as I understand it, is bound by external API specification, so
>> not easy to change.
>>
>> If we turned on -fstrict-flex-arrays=3, these would become undefined
>> behavior and trigger the ubsan sanitizer.
> 
> The compiler setting can be changed on a per-struct basis via
> 
> __attribute__((strict_flex_arrays(N)));

This only works with gcc, not clang, so it's not a viable solution.



Re: Add compiler option -fstrict-flex-arrays=1

От:
David Geier <geidav.pg@gmail.com>
Дата:
>>> If we turned on -fstrict-flex-arrays=3, these would become undefined
>>> behavior and trigger the ubsan sanitizer.
>>
>> The compiler setting can be changed on a per-struct basis via
>>
>> __attribute__((strict_flex_arrays(N)));
> 
> This only works with gcc, not clang, so it's not a viable solution.

clang has support for it starting with version 16.0, see [1]. Is that
clang version too recent to require it?

--
David Geier

[1] https://releases.llvm.org/16.0.0/tools/clang/docs/ReleaseNotes.html


Re: Add compiler option -fstrict-flex-arrays=1

От:
Peter Eisentraut <peter@eisentraut.org>
Дата:
On 20.10.26 14:37, David Geier wrote:
>>>> If we turned on -fstrict-flex-arrays=3, these would become undefined
>>>> behavior and trigger the ubsan sanitizer.
>>>
>>> The compiler setting can be changed on a per-struct basis via
>>>
>>> __attribute__((strict_flex_arrays(N)));
>>
>> This only works with gcc, not clang, so it's not a viable solution.
> 
> clang has support for it starting with version 16.0, see [1]. Is that
> clang version too recent to require it?
> 
> --
> David Geier
> 
> [1] https://releases.llvm.org/16.0.0/tools/clang/docs/ReleaseNotes.html

I don't see the attribute mentioned in the release notes.  The only 
thing related that I see in there is that that release now supports the 
level 3 of -fstrict-flex-arrays where the previous versions only 
supported up to 2.  My testing confirms that no version of clang 
supports the attribute.


Re: Add compiler option -fstrict-flex-arrays=1

От:
David Geier <geidav.pg@gmail.com>
Дата:
>>> This only works with gcc, not clang, so it's not a viable solution.
>>
>> clang has support for it starting with version 16.0, see [1]. Is that
>> clang version too recent to require it?
>>
> I don't see the attribute mentioned in the release notes.  The only
> thing related that I see in there is that that release now supports the
> level 3 of -fstrict-flex-arrays where the previous versions only
> supported up to 2.  My testing confirms that no version of clang
> supports the attribute.

You're right, Peter. I incorrectly inferred that support for the command
line argument would also mean support for the attribute. This is not the
case for Clang. What a pity.

--
David Geier


Re: Add compiler option -fstrict-flex-arrays=1

От:
"Tristan Partin" <tristan@partin.io>
Дата:
On Mon Oct 19, 2026 at 10:27 AM UTC, Peter Eisentraut wrote:
> (I already posted this patch in another thread [0], but since this was 
> under another topic, I'm posting it again here with a clearer subject.)
>
> [0]: 
> https://www.postgresql.org/message-id/flat/DLRI3Q0C0MR2.1V3TOIS3N16FJ%40partin.io
>
> Compilers can check object sizes in various ways.  For example, there is 
> -Warray-bounds, __builtin_object_size, which drives _FORTIFY_SOURCE, and 
> also -fsanitize=bounds, which is part of ubsan.
>
> One perhaps surprising aspect of that is that both gcc and clang by 
> default assume that any trailing array in a struct could be used as a 
> flexible array member, independent of its declared size, and therefore 
> don't believe the size declaration and thus exempt that array from all 
> those checks.
>
> This patch fixes that.  Below is a relatively elaborate explanation, but 
> the patch is actually small.
>
> Here is an example of such a struct:
>
> typedef struct
> {
>      ...
>
>      char       *server_final_message;
>      uint8       ServerSignature[SCRAM_MAX_KEY_LEN];
> } fe_scram_state;
>
> (SCRAM_MAX_KEY_LEN is 32.)
>
> Because of the default assumption mentioned above, the compilers don't 
> check that code doesn't overrun the ServerSignature field.
>
> There are many other structs in our code that are similarly affected.
>
> I suppose this behavior is to accommodate legacy code, but our code uses 
> C99 flexible array members correctly (mostly, see below), so we can make 
> these checks more strict and get more benefit from the various bounds 
> checking mechanisms.
>
> gcc and clang have the option -fstrict-flex-arrays [1] for this.
>
> [1]: 
> https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#index-fstrict-flex-arrays
>
> Ideally, we'd want to use the maximum level -fstrict-flex-arrays=3, 
> which would only allow flexible array members declared with the C 
> standard "[]".  But we do need to permit flexible array members declared 
> with "[1]" in a few cases because standard C does not allow flexible 
> array members as the only element in a struct.  Affected are:
>
> - struct RevmapContents
> - struct ReplicationSlotCtlData
> - union pgresult_data
>
> (Some of these have comments "It should be declared 
> [FLEXIBLE_ARRAY_MEMBER], but for some reason you can't do that in an 
> otherwise-empty struct.")
>
> There is also:
>
> - struct sqlda_struct
>
> which, as I understand it, is bound by external API specification, so 
> not easy to change.
>
> If we turned on -fstrict-flex-arrays=3, these would become undefined 
> behavior and trigger the ubsan sanitizer.
>
> If we use the lower level -fstrict-flex-arrays=1, then flexible-array 
> members declared as "[]", "[0]", and "[1]" are permitted, but all the 
> obviously-not-flexible-array-members like the fe_scram_state example 
> above can now be bounds checked properly.  The only struct in the code 
> that genuinely has a trailing one-element array is WordEntryPosVector1, 
> and so we would miss bounds checking on that one, but I think we can 
> live with that.

We could define a pg_attribute_strict_flex_array() macro and add it to 
the entry in WordEntryPosVector1. We can add a comment to the macro 
indicating how and why it should be used since it'll likely be a little 
non-obvious, but we could continue to keep bounds checking on 
WordEntryPosVector1.

> There is one piece of code that still needs fixing to permit 
> -fstrict-flex-arrays=1, which was the topic of the [0] thread: 
> JsonValueList has an array declared as "[2]" (via BASE_JVL_ITEMS) but 
> addresses it as a flexible-length array in some circumstances.  My 
> proposal included here is to change this to an anonymous union with one 
> branch having the original size "[2]", to keep local variables sized the 
> same as before, and one branch of size "[1]", which effectively makes 
> this a flexible-array member under -fstrict-flex-arrays=1.

Assuming you think my previous suggestion is reasonable, I think the 
patch with my proposed change is good to go. Otherwise, the patch as is 
will is also good.

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)


Re: Add compiler option -fstrict-flex-arrays=1

От:
Peter Eisentraut <peter@eisentraut.org>
Дата:
Вложения:
On 27.10.26 16:51, Tristan Partin wrote:
> We could define a pg_attribute_strict_flex_array() macro and add it to
> the entry in WordEntryPosVector1. We can add a comment to the macro
> indicating how and why it should be used since it'll likely be a little
> non-obvious, but we could continue to keep bounds checking on
> WordEntryPosVector1.

Something like in the attached patch 0002?

(Patch 0001 is unchanged.)

Re: Add compiler option -fstrict-flex-arrays=1

От:
"Tristan Partin" <tristan@partin.io>
Дата:
On Wed Oct 28, 2026 at 8:04 AM UTC, Peter Eisentraut wrote:
> On 27.10.26 16:51, Tristan Partin wrote:
>> We could define a pg_attribute_strict_flex_array() macro and add it to
>> the entry in WordEntryPosVector1. We can add a comment to the macro
>> indicating how and why it should be used since it'll likely be a little
>> non-obvious, but we could continue to keep bounds checking on
>> WordEntryPosVector1.
>
> Something like in the attached patch 0002?
>
> (Patch 0001 is unchanged.)

Yep, that is exactly what I had in mind.

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)


Re: Add compiler option -fstrict-flex-arrays=1

От:
Peter Eisentraut <peter@eisentraut.org>
Дата:
On 03.11.26 06:28, Tristan Partin wrote:
> On Wed Oct 28, 2026 at 8:04 AM UTC, Peter Eisentraut wrote:
>> On 27.10.26 16:51, Tristan Partin wrote:
>>> We could define a pg_attribute_strict_flex_array() macro and add it to
>>> the entry in WordEntryPosVector1. We can add a comment to the macro
>>> indicating how and why it should be used since it'll likely be a little
>>> non-obvious, but we could continue to keep bounds checking on
>>> WordEntryPosVector1.
>>
>> Something like in the attached patch 0002?
>>
>> (Patch 0001 is unchanged.)
> 
> Yep, that is exactly what I had in mind.

Committed, thanks.



FAQ