Bug #78038 [Fbk]: Socket_Select fails when resource array contains non-sequential keys
| From: | nikic@php.net | Date: | Wed, 22 May 2019 07:44:17 +0000 |
| Subject: | Bug #78038 [Fbk]: Socket_Select fails when resource array contains non-sequential keys | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-220935@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78038&edit=1
ID: 78038
Updated by: nikic@php.net
Reported by: ogdg at protonmail dot com
Summary: Socket_Select fails when resource array contains
non-sequential keys
Status: Feedback
Type: Bug
Package: Sockets related
Operating System: Fedora 30
PHP Version: 7.3.5
Block user comment: N
Private report: N
New Comment:
One recent change comes to mind here: https://github.com/php/php-src/commit/bdac9ef10d90997f9576564dde693cbef6594142#diff-9df410db21c230cd2ec04a4c95ccd20f
However, it is in stream_select(), not socket_select().
When you say "previous versions" do you mean 7.3.4 or some older version?
Previous Comments:
------------------------------------------------------------------------
[2019-05-20 10:00:10] daverandom@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves.
A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external
resources such as databases, etc. If the script requires a
database to demonstrate the issue, please make sure it creates
all necessary tables, stored procedures etc.
Please avoid embedding huge scripts into the report.
I cannot reproduce this in 7.3.5 or any other version. Additionally, none of the relevant code in
php-src has been changed for several years in 7.3[1] or master[2].
Please recheck your code, var_dump($r) and verify that it contains at least one valid socket
resource, and if so please provide a complete reproducing script.
There is a lot of code in the wild which utilises a pattern roughly like below - it's likely
that if it were broken then a lot of people would be shouting about it :-)
$socket = socket_create(/* stuff */); // ...set up the socket
$sockets[(int)$socket] = $socket; // Add socket to array indexed by resource ID
$w = $e = NULL;
$r = $sockets;
$check = socket_select($r, $w, $e, 0);
[1] https://github.com/php/php-src/blame/PHP-7.3/ext/sockets/sockets.c#L915-L977
[2] https://github.com/php/php-src/blame/master/ext/sockets/sockets.c#L921-L983
------------------------------------------------------------------------
[2019-05-20 09:27:50] ogdg at protonmail dot com
Description:
------------
This bug occurs in PHP CLI 7.3.5!
When socket connection resources are indexed in an array by non-sequential numeric keys,
socket_select throws a warning and does not actually accept the array even though it contains valid
socket resources. By manually re-indexing the array to contain sequentially numbered keys, the
problem disappears.
This could a problem with large key values, or non sequential keys, or both. The problem did not
exist in previous versions of PHP, and it is not valid for socket_select to require arrays that are
sequentially numbered, as there are reasons to index connection arrays by process ID or some other
meaningful number.
Test script:
---------------
# Assume each $connN variable is a valid socket resource
# Note that array keys are INT and not string type
$connections = array(1235 => $conn1, 2084 => $conn2, 3509 => $conn3);
$w = $e = NULL;
$r = $connections;
$check = socket_select($r, $w, $e, 0);
# The ABOVE code fails with bogus warnings in PHP 7.3.5
# The BELOW code works
$w = $e = NULL;
foreach($connections as $v) $r[] = $v;
$check = socket_select($r, $w, $e, 0);
Expected result:
----------------
Expect socket_select to work as intended and return a value for $check and a modified connection
resource array if appropriate.
Actual result:
--------------
Result is two warnings:
socket_select(): supplied argument is not a valid Socket resource
socket_select(): no resource arrays were passed to select
Previous versions of PHP, including PHP 7 did not have this problem.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78038&edit=1