[php-src] PR #26026: Declare variable-length trailing arrays as C99 flexible array members

From: Date: Wed, 02 Dec 2026 05:44:45 +0000
Subject: [php-src] PR #26026: Declare variable-length trailing arrays as C99 flexible array members
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-41357@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/26026 Author: sebastianbergmann PHP declares the last member of a number of structures as an array with one element and uses it with as many elements as were allocated for it ("struct hack"). A compiler that is asked to treat only real flexible array members as such, for instance with -fstrict-flex-arrays=3, takes the declared size literally. In combination with -D_FORTIFY_SOURCE=3, valid code is then aborted: ``` $ sapi/cli/php -n -r 'echo __DIR__, PHP_EOL;' *** buffer overflow detected ***: terminated ``` #25665 proposed to annotate these members with __attribute__((strict_flex_array(1))). In https://github.com/php/php-src/pull/25665#issuecomment-7098674037, @TimWolla asked for them to be declared as C99 flexible array members instead. This pull request does that for 17 of the 18 members of #25665. The exception is zend_object.properties_table, see below. ## Commits 1. **Compute sizes of structs with trailing arrays using offsetof().** 11 of the 17 structures have their allocation size computed as sizeof(T) - sizeof(elem) + n * sizeof(elem) or as sizeof(T) + (n - 1) * sizeof(elem). Both depend on the array being declared with one element. This commit changes them to offsetof(T, member) + n * sizeof(elem), which is what _ZSTR_HEADER_SIZE and zend_ast_size() (#11955) already do. It does not change any declaration. zend_ast_list_size() moves to zend_ast.h so that opcache no longer duplicates the calculation. 2. **phpdbg: Do not create a phpdbg_file_source on the stack.** This was the only instance of one of these structures that is not on the heap. 3. **Declare variable-length trailing arrays as flexible array members.** [1] becomes [] for zend_string.val, zend_type_list.types, zend_property_info_list.ptr, zend_ast.child, zend_ast_list.child, zend_attribute.args, zend_trait_precedence.exclude_class_names, zend_inheritance_cache_entry.traits_and_interfaces, zend_llist_element.data, zend_call_info.arg_info, and for seven structures that are private to Zend/zend_partial.c, Zend/zend_strtod.c, the JIT, main/fastcgi.c and phpdbg. 4. **Use flexible array members in sysvmsg and for tracked arenas.** Two more cases of the same pattern that are not part of #25665: struct php_msgbuf in ext/sysvmsg, and the GNU zero-length array of zend_arena under ZEND_TRACK_ARENA_ALLOC. ## What changes for extensions - The offsets of the members do not change. sizeof() of the structures no longer includes one element, for instance sizeof(zend_string) is 24 instead of 32 on x86-64. - Code that computes an allocation size from sizeof() of one of these structures has to use _ZSTR_STRUCT_SIZE(), ZEND_TYPE_LIST_SIZE(), ZEND_ATTRIBUTE_SIZE(), zend_ast_size(), zend_ast_list_size(), or offsetof(). php-src itself does not use sizeof(zend_string) anywhere. - sizeof can no longer be applied to the members themselves. In the extensions I can build this affects one line: a curl_easy_setopt() call in ext/curl that passes ZSTR_VAL() directly, because the type-checking macro of libcurl applies sizeof to its argument. - These structures can no longer be used for static or automatic variables, as members of other structures, or as array elements. Nothing in php-src did that, apart from the phpdbg case above. This is documented in UPGRADING.INTERNALS. ## C++ C++ has been the reason for keeping the struct hack: [nikic in #3527](https://github.com/php/php-src/pull/3527#issuecomment-422035959), [morrisonlevi in #12650](https://github.com/php/php-src/pull/12650#issuecomment-1808955549), [bukka in #13324](https://github.com/php/php-src/pull/13324#issuecomment-1925889808), [iluuu1994 in #17564](https://github.com/php/php-src/issues/17564#issuecomment-2613607348). With GCC 16.2.1 and Clang 23.1.5, a structure that ends in a flexible array member is accepted by g++ and clang++ with -std=c++11 and -std=c++17 and -Wall -Wextra without a diagnostic; only -Wpedantic reports it. ext/intl builds with --enable-werror with both compilers. A flexible array member in an installed header already exists in ext/mysqlnd/mysqlnd_structs.h (#18246). ## zend_object.properties_table is not changed zend_object is the first member of zend_object_iterator, zend_closure, zend_generator, zend_fiber and others. A structure with a flexible array member that is a member of another structure, and not the last one, is a hard error for g++ and a warning that is enabled by default for Clang. Some of these structures also use the one element that zend_object has today, for instance Generator for its guard. The details are in https://github.com/php/php-src/pull/25665#issuecomment-7103273623. I think that this needs its own discussion and its own pull request. ## Verification GCC 16.2.1, glibc 2.44, x86-64 (Fedora 45). Debug build with --enable-werror, with the extensions of #25665 plus cgi, fpm, phpdbg, sockets and sysvmsg: - Every commit builds without a compiler warning. - The phpdbg tests pass for the second commit. The complete test suite was run for the first, third and fourth commit, and for the fourth commit also with the tracing JIT, with the function JIT, and with the opcache file cache (priming it and using it). In each run, the only test that fails is ext/intl/tests/timezone_IDforWindowsID_basic_icu76_1.phpt, which also fails without these changes on my machine. - The debug information shows the same offset for each of the members before and after, and the smaller sizeof(). - The gdb pretty-printers for zend_string, zend_type and zend_ast in scripts/gdb/php_gdb.py and print_zstr of .gdbinit work without changes. Clang 23.1.5, debug build with --enable-werror, with the extensions above plus calendar, dba, exif, ftp, gd, gettext, mysqli, pdo_mysql, shmop, sysvsem, sysvshm and zend_test: - The last commit builds without a compiler warning, including the C++ code of ext/intl. - Of 21,851 executed tests, the only one that fails is the one mentioned above. Clang 23.1.5 with AddressSanitizer and UndefinedBehaviorSanitizer, with the flags of the Alpine job (-fsanitize=undefined,address -fno-sanitize=function -DZEND_TRACK_ARENA_ALLOC), same extensions, tests run with --asan: - Of 21,710 executed tests, the only one that fails is the one mentioned above. - The same is true for the 21,715 tests that are executed with the tracing JIT. Build with the flags that the OpenSSF Compiler Options Hardening Guide recommends, with the configure options and flags of #25665 plus --enable-sysvmsg, for master at f09dd61f03b and for this branch: | | master | this branch | |---|---:|---:| | php -r 'echo __DIR__;' | aborts | prints the directory | | distinct -Warray-bounds warnings | 513 | 62 | | distinct -Wstringop-overflow warnings | 8 | 1 | | -Wattribute-warning from fortified getcwd() and inet_ntop() | 2 | 0 | | failing tests (of 20,131 executed) | 8 | 1 | | of those, aborted with "buffer overflow detected" | 7 | 0 | The 62 remaining -Warray-bounds warnings are all for zval[1], which is zend_object.properties_table. The remaining -Wstringop-overflow warning is in the bundled PCRE2 (sljitNativeX86_common.c). The test that still fails is the one mentioned above. Not verified: 32-bit, MSVC, and extensions I do not have the dependencies for.

« previous php.git-pulls (#41357) next »