Skip to content

Commit 0d4cf47

Browse files
committed
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.
1 parent a24eada commit 0d4cf47

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Diff for: ext/posix/posix.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "ext/standard/info.h"
2424
#include "ext/standard/php_string.h"
2525
#include "php_posix.h"
26+
#include "main/php_network.h"
2627

2728
#ifdef HAVE_POSIX
2829

@@ -417,7 +418,7 @@ PHP_FUNCTION(posix_ctermid)
417418
/* }}} */
418419

419420
/* Checks if the provides resource is a stream and if it provides a file descriptor */
420-
static int php_posix_stream_get_fd(zval *zfp, zend_long *fd) /* {{{ */
421+
static int php_posix_stream_get_fd(zval *zfp, zend_long *ret) /* {{{ */
421422
{
422423
php_stream *stream;
423424

@@ -427,19 +428,21 @@ static int php_posix_stream_get_fd(zval *zfp, zend_long *fd) /* {{{ */
427428
return 0;
428429
}
429430

430-
/* get the fd.
431+
/* get the fd. php_socket_t is used for FDs, and is shorter than zend_long.
431432
* NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag when casting.
432433
* It is only used here so that the buffered data warning is not displayed.
433434
*/
435+
php_socket_t fd = -1;
434436
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL) == SUCCESS) {
435-
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)fd, 0);
437+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 0);
436438
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL) == SUCCESS) {
437-
php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void*)fd, 0);
439+
php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 0);
438440
} else {
439441
php_error_docref(NULL, E_WARNING, "Could not use stream of type '%s'",
440442
stream->ops->label);
441443
return 0;
442444
}
445+
*ret = fd;
443446
return 1;
444447
}
445448
/* }}} */

0 commit comments

Comments
 (0)