From 0d4cf47b1884f8a9771383a005cd39325faf62cc Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Tue, 24 Dec 2024 12:46:36 -0800 Subject: [PATCH 1/2] Fix FD getting code on big endian (PHP 8.3) stream casting as FD returns a php_socket_t, which is an int, but zend_long is 64-bit (on those platforms). This works on LE by accidental (unless it forgets to clear the high word), but is fatal on big endian. --- ext/posix/posix.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ext/posix/posix.c b/ext/posix/posix.c index ecbb849408723..f966e360cbc99 100644 --- a/ext/posix/posix.c +++ b/ext/posix/posix.c @@ -23,6 +23,7 @@ #include "ext/standard/info.h" #include "ext/standard/php_string.h" #include "php_posix.h" +#include "main/php_network.h" #ifdef HAVE_POSIX @@ -417,7 +418,7 @@ PHP_FUNCTION(posix_ctermid) /* }}} */ /* Checks if the provides resource is a stream and if it provides a file descriptor */ -static int php_posix_stream_get_fd(zval *zfp, zend_long *fd) /* {{{ */ +static int php_posix_stream_get_fd(zval *zfp, zend_long *ret) /* {{{ */ { php_stream *stream; @@ -427,19 +428,21 @@ static int php_posix_stream_get_fd(zval *zfp, zend_long *fd) /* {{{ */ return 0; } - /* get the fd. + /* get the fd. php_socket_t is used for FDs, and is shorter than zend_long. * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag when casting. * It is only used here so that the buffered data warning is not displayed. */ + php_socket_t fd = -1; if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL) == SUCCESS) { - php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)fd, 0); + php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 0); } else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL) == SUCCESS) { - php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void*)fd, 0); + php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 0); } else { php_error_docref(NULL, E_WARNING, "Could not use stream of type '%s'", stream->ops->label); return 0; } + *ret = fd; return 1; } /* }}} */ From dc7e3112bca6dd7873d0d40a621f3744e5db14b0 Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Wed, 25 Dec 2024 07:06:57 -0800 Subject: [PATCH 2/2] change cast to match sig --- ext/posix/posix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/posix/posix.c b/ext/posix/posix.c index f966e360cbc99..2c87fbd28d981 100644 --- a/ext/posix/posix.c +++ b/ext/posix/posix.c @@ -434,9 +434,9 @@ static int php_posix_stream_get_fd(zval *zfp, zend_long *ret) /* {{{ */ */ php_socket_t fd = -1; if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL) == SUCCESS) { - php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 0); + php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void**)&fd, 0); } else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL) == SUCCESS) { - php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 0); + php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void**)&fd, 0); } else { php_error_docref(NULL, E_WARNING, "Could not use stream of type '%s'", stream->ops->label);