[php-src] master: Accept null for microseconds argument in stream_select() (#6879)

From: Date: Thu, 22 Apr 2021 08:09:46 +0000
Subject: [php-src] master: Accept null for microseconds argument in stream_select() (#6879)
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-115745@lists.php.net to get a copy of this message
Author: Niklas Keller (kelunik) Committer: GitHub (web-flow) Pusher: nikic Date: 2021-04-22T10:09:55+02:00 Commit: https://github.com/php/php-src/commit/59d5b3dd51ce6d774d5322504e592f447f834f76 Raw diff: https://github.com/php/php-src/commit/59d5b3dd51ce6d774d5322504e592f447f834f76.diff Accept null for microseconds argument in stream_select() (#6879) The deprecation of passing null is thrown otherwise. If the timeout is calculated conditionally before calling stream_select(), passing to avoid the deprecation seems unreasonable, example: https://github.com/amphp/amp/blob/7d4bbc6e0b47c6bb39b6cce1a4b5942e0c5125fb/lib/Loop/NativeDriver.php#L286 Also enforce that if $seconds is null, then $microseconds should be null as well. However 0 is still accepted (with deprecation) for backwards compatibility. Changed paths: A ext/standard/tests/streams/stream_select_null_usec.phpt M ext/standard/basic_functions.stub.php M ext/standard/basic_functions_arginfo.h M ext/standard/streamsfuncs.c M ext/standard/tests/general_functions/proc_open_sockets1.phpt M ext/standard/tests/general_functions/proc_open_sockets2.phpt M ext/standard/tests/general_functions/proc_open_sockets3.phpt Diff: diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index 052a95c4514c4..ac85bce2167c2 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -1209,7 +1209,7 @@ function soundex(string $string): string {} /* streamsfuncs.c */ -function stream_select(?array &$read, ?array &$write, ?array &$except, ?int $seconds, int $microseconds = 0): int|false {} +function stream_select(?array &$read, ?array &$write, ?array &$except, ?int $seconds, ?int $microseconds = null): int|false {} /** @return resource */ function stream_context_create(?array $options = null, ?array $params = null) {} diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index b520fd7c79e12..eef802115d737 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 2d92e992837a61a052eea6d0257837aaccc54be6 */ + * Stub hash: 55f11c2d6cc7ba342b1062104f3838866ad9135d */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0) @@ -1847,7 +1847,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_stream_select, 0, 4, MAY_BE_LONG ZEND_ARG_TYPE_INFO(1, write, IS_ARRAY, 1) ZEND_ARG_TYPE_INFO(1, except, IS_ARRAY, 1) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 1) - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, microseconds, IS_LONG, 0, "0") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, microseconds, IS_LONG, 1, "null") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_stream_context_create, 0, 0, 0) diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c index 128004bfc205c..c78d88cd07ce4 100644 --- a/ext/standard/streamsfuncs.c +++ b/ext/standard/streamsfuncs.c @@ -745,6 +745,7 @@ PHP_FUNCTION(stream_select) int retval, sets = 0; zend_long sec, usec = 0; bool secnull; + bool usecnull = 1; int set_count, max_set_count = 0; ZEND_PARSE_PARAMETERS_START(4, 5) @@ -753,7 +754,7 @@ PHP_FUNCTION(stream_select) Z_PARAM_ARRAY_EX2(e_array, 1, 1, 0) Z_PARAM_LONG_OR_NULL(sec, secnull) Z_PARAM_OPTIONAL - Z_PARAM_LONG(usec) + Z_PARAM_LONG_OR_NULL(usec, usecnull) ZEND_PARSE_PARAMETERS_END(); FD_ZERO(&rfds); @@ -788,6 +789,15 @@ PHP_FUNCTION(stream_select) PHP_SAFE_MAX_FD(max_fd, max_set_count); + if (secnull && !usecnull) { + if (usec == 0) { + php_error_docref(NULL, E_DEPRECATED, "Argument #5 ($microseconds) should be null instead of 0 when argument #4 ($seconds) is null"); + } else { + zend_argument_value_error(5, "must be null when argument #4 ($seconds) is null"); + RETURN_THROWS(); + } + } + /* If seconds is not set to null, build the timeval, else we wait indefinitely */ if (!secnull) { if (sec < 0) { diff --git a/ext/standard/tests/general_functions/proc_open_sockets1.phpt b/ext/standard/tests/general_functions/proc_open_sockets1.phpt index acc8eee617fef..c55a031faef59 100644 --- a/ext/standard/tests/general_functions/proc_open_sockets1.phpt +++ b/ext/standard/tests/general_functions/proc_open_sockets1.phpt @@ -25,7 +25,7 @@ while ($pipes) { $w = null; $e = null; - if (!stream_select($r, $w, $e, null, 0)) { + if (!stream_select($r, $w, $e, null)) { throw new Error("Select failed"); } diff --git a/ext/standard/tests/general_functions/proc_open_sockets2.phpt b/ext/standard/tests/general_functions/proc_open_sockets2.phpt index 7f9e1a85c54f6..e01452e84704e 100644 --- a/ext/standard/tests/general_functions/proc_open_sockets2.phpt +++ b/ext/standard/tests/general_functions/proc_open_sockets2.phpt @@ -9,7 +9,7 @@ function poll($pipe, $read = true) $w = ($read == false) ? [$pipe] : null; $e = null; - if (!stream_select($r, $w, $e, null, 0)) { + if (!stream_select($r, $w, $e, null)) { throw new \Error("Select failed"); } } diff --git a/ext/standard/tests/general_functions/proc_open_sockets3.phpt b/ext/standard/tests/general_functions/proc_open_sockets3.phpt index be2a3712382a3..b2b8329f894af 100644 --- a/ext/standard/tests/general_functions/proc_open_sockets3.phpt +++ b/ext/standard/tests/general_functions/proc_open_sockets3.phpt @@ -9,7 +9,7 @@ function poll($pipe, $read = true) $w = ($read == false) ? [$pipe] : null; $e = null; - if (!stream_select($r, $w, $e, null, 0)) { + if (!stream_select($r, $w, $e, null)) { throw new \Error("Select failed"); } } diff --git a/ext/standard/tests/streams/stream_select_null_usec.phpt b/ext/standard/tests/streams/stream_select_null_usec.phpt new file mode 100644 index 0000000000000..9be73d31bcc2d --- /dev/null +++ b/ext/standard/tests/streams/stream_select_null_usec.phpt @@ -0,0 +1,36 @@ +--TEST-- +stream_select allows null for microsecond timeout if timeout is null +--FILE-- +<?php + +$read = [fopen(__FILE__, 'r')]; +$write = null; +$except = null; + +error_reporting(-1); +set_error_handler(function ($errno, $errstr) { + print $errno . " " . $errstr . "\n"; +}); + +stream_select($read, $write, $except, null, null); +var_dump($read); + +print "\n"; + +stream_select($read, $write, $except, null, 0); + +stream_select($read, $write, $except, null, 1); +?> +--EXPECTF-- +array(1) { + [0]=> + resource(%d) of type (stream) +} + +8192 stream_select(): Argument #5 ($microseconds) should be null instead of 0 when argument #4 ($seconds) is null + +Fatal error: Uncaught ValueError: stream_select(): Argument #5 ($microseconds) must be null when argument #4 ($seconds) is null in %s +Stack trace: +#0 %s stream_select(Array, NULL, NULL, NULL, 1) +#1 {main} +%s

« previous php.cvs (#115745) next »