[php-src] PR #15013: fix: unsafe function casts

From: Date: Thu, 18 Jul 2024 17:17:07 +0000
Subject: [php-src] PR #15013: fix: unsafe function casts
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-27349@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/15013 Author: morrisonlevi This is a weird one. The actual error message talks about array bounds: In file included from /usr/local/src/php/Zend/zend.h:32, from /usr/local/src/php/Zend/zend_API.c:22: In function 'zend_check_magic_method_implementation', inlined from 'zend_register_functions' at /usr/local/src/php/Zend/zend_API.c:3182:4: /usr/local/src/php/Zend/zend_API.c:2759:34: error: array subscript 'zend_function {aka const union _zend_function}[0]' is partly outside array bounds of 'unsigned char[160]' [-Werror=array-bounds] 2759 | if (ZSTR_VAL(fptr->common.function_name)[0] != '_' /usr/local/src/php/Zend/zend_string.h:68:26: note: in definition of macro 'ZSTR_VAL' 68 | #define ZSTR_VAL(zstr) (zstr)->val | ^~~~ /usr/local/src/php/Zend/zend_API.c: In function 'zend_register_functions': /usr/local/src/php/Zend/zend_API.c:3052:32: note: object of size 160 allocated by 'malloc' 3052 | reg_function = malloc(sizeof(zend_internal_function)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The core issue is that you cannot make a zend_internal_function *, and then cast it to a wider zend_function *. There are two ways to fix this: 1. Malloc zend_function instead and initialize the zend_internal_function member. 2. Cast zend_internal_function * to just the narrower common struct, zend_function_common *, and work with that. The first option wastes memory. The second option seems better and conceptually helpful to reason about. Note that if we needed to cast from zend_function_common * back out to zend_internal_function *, that would be safe.

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