php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80803 socket_read() reference to global socket fails in user function
Submitted: 2021-02-26 11:02 UTC Modified: 2021-03-01 18:05 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: janui at onmat dot com Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 8.0.2 OS: Linux 5.10.14-2-ARCH armv7l
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: janui at onmat dot com
New email:
PHP Version: OS:

 

 [2021-02-26 11:02 UTC] janui at onmat dot com
Description:
------------
---
From manual page: https://php.net/function.socket-read
---
When a user function contains a socket_read() which references a globally defined socket object it fails to connect to and read the socket. When the same code used in the top-level script it works correctly. The problem is not present in the socket_connect(), socket_write() and socket_close() functions.

function user_function_read_socket($socketVariableName)
{
$buff = 4096;
global $$socketVariableName;
echo $socketVariableName."\n";
var_dump($$socketVariableName);
$response = socket_read($$socketVariableName, $buff, PHP_NORMAL_READ);
echo $response."\n";
echo socket_strerror(socket_last_error($$socketVariableName))."\n";
}

The following similar construction has no effect on the result:
$response = socket_read($GLOBALS[$socketVariableName], $buff, PHP_NORMAL_READ);


Test script:
---------------
See description. A full test script can be delivered., something like this:
<?php
function user_function_open_socket($socketVariableName)
{...}
function user_function_write_socket($socketVariableName, $command)
{...}
function user_function_read_socket($socketVariableName)
{...see description...}
function user_function_close_socket($socketVariableName)
{...}

$socketVariableName = 'testSocketName';
$command = 'a valid command';
user_function_open_socket($socketVariableName);
user_function_write_socket($socketVariableName, $command);
user_function_read_socket($socketVariableName);
// paste the code from the read function here
user_function_close_socket($socketVariableName);


Expected result:
----------------
I would expect the code to work in the same way regardless of being used in the top level script as in a user function.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-26 15:39 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2021-02-26 15:39 UTC] requinix@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.


 [2021-02-26 16:09 UTC] janui at onmat dot com
-Status: Feedback +Status: Open
 [2021-02-26 16:09 UTC] janui at onmat dot com
The text and script provided should be enough to explain the problem. I can provide you with a full example, but this will be far in excess of the 20 lines of code limit which you specify as a limit. You will also need to have a server process which will initiate a socket and reply to the test requests. Any socket server process will suffice. My current solution is fully tested and works with PHP7.4.13-1 (syntactically & run-time correct). As I already specified, the problem relates only to socket_read(), the other PHP8 socket functions work without problems. And the identical code for the socket_read() problem works correctly when executed directly in the calling script (also syntactically & run-time correct). Please specify which server proces you wish to use for testing/debugging and advise on the 20 lines limit.
 [2021-02-26 16:44 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2021-02-26 16:44 UTC] requinix@php.net
> The text and script provided should be enough to explain the problem.
Explain, yes. Reproduce, no.

My own implementation (which ran correctly) was about 40 lines or so. That's not too much to put here. If yours is much longer than that, create a gist or put it on Pastebin or wherever.
 [2021-03-01 16:31 UTC] janui at onmat dot com
-Status: Feedback +Status: Closed
 [2021-03-01 16:31 UTC] janui at onmat dot com
Hi, I spent a day putting together a script which tests the problem. I expected to be able to send it and the results to you today, but it proves that I have a problem and PHP8 works fine. I will save a copy of the script. If anyone wants a copy they can contact me.

The issue can be closed.
 [2021-03-01 18:05 UTC] requinix@php.net
-Status: Closed +Status: Not a bug -Package: *Data Exchange functions +Package: Sockets related
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Jan 25 05:03:35 2022 UTC