[php-src] PR #25665: Mark struct-hack members with `strict_flex_array` so that PHP works with `-fstrict-flex-arrays=3`
| From: | sebastianbergmann | Date: | Mon, 23 Nov 2026 10:26:59 +0000 |
| Subject: | [php-src] PR #25665: Mark struct-hack members with `strict_flex_array` so that PHP works with `-fstrict-flex-arrays=3` | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40919@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25665
Author: sebastianbergmann
PHP uses one-element trailing arrays as flexible arrays ("struct-hack members") in a
number of structures (
zend_string.val, zend_object.properties_table,
zend_ast.child, ...).
With -fstrict-flex-arrays=3, which the [OpenSSF Compiler Options Hardening
Guide](https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html)
recommends, the compiler takes the declared size of 1 literally. Combined with
-D_FORTIFY_SOURCE=3, valid code then aborts:
```
$ ./buildconf
$ CFLAGS="-O2 -g -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -fstrict-flex-arrays=3" ./configure
--disable-all
$ make -j"$(nproc)"
$ sapi/cli/php -n -r 'echo __DIR__, PHP_EOL;'
*** buffer overflow detected ***: terminated
```
GCC announces it at compile time:
```
In function 'getcwd',
inlined from 'zend_try_ct_eval_magic_const' at Zend/zend_compile.c:10669:5:
/usr/include/bits/unistd.h:116:10: warning: call to '__getcwd_chk_warn' declared with
attribute warning:
getcwd caller with bigger length than size of destination buffer [-Wattribute-warning]
```
This is not a buffer overflow. zend_string ends in char val[1] and is
allocated with room for the whole string. With -fstrict-flex-arrays=3,
__builtin_dynamic_object_size(ZSTR_VAL(s), 1) is 1, and the fortified
getcwd() rejects a buffer of MAXPATHLEN bytes.
This pull request adds ZEND_ATTRIBUTE_FLEX_ARRAY, which expands to
__attribute__((strict_flex_array(1))) where the compiler has that attribute (GCC 13 and
later), and puts it on the 18 declarations that use this code construct. The attribute states for a
single member what -fstrict-flex-arrays=1 states for the whole program: a trailing
array declared as [], [0] or [1] is a flexible array. It
changes nothing in a build without -fstrict-flex-arrays.
Measured on master at 9af77c680e0 with GCC 16.2.1 and glibc 2.44 on x86_64 (Fedora 45),
with the complete set of flags the guide recommends (listed below):
| | before | after |
|---|---:|---:|
| php -r 'echo __DIR__;' | aborts | prints the directory |
| distinct -Warray-bounds warnings | 429 | 0 |
| distinct -Wstringop-overflow warnings | 7 | 1 (bundled PCRE2,
sljitNativeX86_common.c) |
| -Wattribute-warning from fortified getcwd() and inet_ntop()
| 2 | 0 |
| failing tests (of 20,006 executed) | 8 | 1 |
| of those, aborted with "buffer overflow detected" | 7 | 0 |
The seven tests that abort before the change are Zend/tests/arginfo_zpp_mismatch.phpt,
Zend/tests/arginfo_zpp_mismatch_strict.phpt,
ext/standard/tests/network/net_get_interfaces_001.phpt,
ext/xml/tests/bug30875.phpt, ext/xml/tests/bug35447.phpt,
ext/xml/tests/bug70962.phpt and ext/xml/tests/xml011.phpt.
The test that still fails, ext/intl/tests/timezone_IDforWindowsID_basic_icu76_1.phpt,
also fails in a build without any of these flags on my machine.
Clang 23 does not have the attribute, so the macro is empty there. I have not built php-src with
Clang and -fstrict-flex-arrays=3; I expect the same aborts.
The fix that I expect would cover both compilers is to declare these members as C99 flexible array
members, which changes sizeof for every one of these structures and is a much larger
change than this one. I looked for one-element trailing arrays with grep and by
following the -Warray-bounds warnings, in Zend/, main/,
ext/opcache/ and sapi/phpdbg/. Bundled libraries are untouched.
<details>
<summary>Configure options and flags of the measurement</summary>
```
./configure --disable-cgi --disable-phpdbg --with-zlib --with-xsl=/usr --enable-mbstring
--enable-bcmath \
--with-gmp --with-openssl --with-bz2 --enable-soap --enable-intl --enable-pcntl --with-tidy
--with-curl \
--with-readline --without-pear --with-ffi --with-zip
CFLAGS="-O2 -g -Wall -Wformat -Wformat=2 -Wconversion -Wimplicit-fallthrough
-Werror=format-security
-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS -fstrict-flex-arrays=3
-fstack-clash-protection -fstack-protector-strong -Wtrampolines -fzero-init-padding-bits=all
-Wbidi-chars=any -fPIE -fcf-protection=full -fzero-call-used-regs=used-gpr
-fno-delete-null-pointer-checks -fno-strict-overflow -fno-strict-aliasing
-ftrivial-auto-var-init=zero -fexceptions"
LDFLAGS="-pie -Wl,-z,nodlopen -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--as-needed
-Wl,--no-copy-dt-needed-entries"
```
</details>