php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74090 stream_get_contents maxlength>-1 returns empty string on windows
Submitted: 2017-02-13 05:05 UTC Modified: 2017-02-13 16:54 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: me at micksam7 dot com Assigned:
Status: Closed Package: Streams related
PHP Version: 7.1.1 OS: Windows 10 v1607
Private report: No CVE-ID:
 [2017-02-13 05:05 UTC] me at micksam7 dot com
Description:
------------
stream_get_contents() with any maxlength greater than -1 returns an empty string,
This only appears to occur with UDP streams opened via stream_socket_client(), specifically on Windows 10.

This problem doesn't occur on Linux and may function fine on older versions of Windows.


The problem appears to reside somewhere in the php_stream_eof call within _php_stream_copy_to_mem - php_stream_eof seems to always return true on UDP streams on Windows 10 systems. More digging required.

Test script:
---------------
//Submit DNS Request
$data = base64_decode("1oIBAAABAAAAAAAAB2V4YW1wbGUDb3JnAAABAAE=");
$fd = stream_socket_client("udp://8.8.8.8:53", $errno, $errstr, 0, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);
stream_set_blocking($fd, 0);
stream_socket_sendto($fd,$data);
sleep(1);
var_dump(stream_get_contents($fd,65565)); //Does not work
var_dump(stream_get_contents($fd));       //No maxlength, works fine
stream_socket_shutdown($fd,STREAM_SHUT_RDWR);

Expected result:
----------------
Returns a valid DNS response roughly 45 bytes long, then an empty string.

string(45) "<>"
string(0) ""

Actual result:
--------------
Returns an empty string, then a valid DNS response.

string(0) ""
string(45) "<>"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-02-13 07:56 UTC] ab@php.net
-Status: Open +Status: Feedback
 [2017-02-13 07:56 UTC] ab@php.net
Thanks for the report. I'm not reproducing this, here 7.0 through master

string(0) ""
string(45) "ւ�� ☺ ☺    example♥org  ☺ ☺�♀ ☺ ☺  J� ♦]��""

Either it's some environment issue, or indeed some more investigation is needed.

Thanks.
 [2017-02-13 08:02 UTC] ab@php.net
-Status: Feedback +Status: Verified
 [2017-02-13 08:02 UTC] ab@php.net
Ah, err, to few coffee in the morning. The exact actual result, so verified.

Thanks.
 [2017-02-13 11:47 UTC] ab@php.net
-Status: Verified +Status: Feedback
 [2017-02-13 11:47 UTC] ab@php.net
Btw. question - why don't you use stream_socket_recvfrom()? The reason to fail here is a possible bug in the stream liveness check, need further tests.

Thanks.
 [2017-02-13 16:54 UTC] me at micksam7 dot com
-Status: Feedback +Status: Open
 [2017-02-13 16:54 UTC] me at micksam7 dot com
stream_socket_recvfrom() seems to work just fine in this case

I submitted this bug after tracking down a problem with reactphp's dns resolver which uses a generic socket class and tracked it down to this function not behaving correctly on Windows. I've already reported it to reactphp and offered a few workarounds, but figured I should still submit this report given this function behaves differently between *nix and Windows with maxlength set.
 [2017-02-15 13:52 UTC] ab@php.net
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=89a5bd67500510f30a729ff97048fa230c9f9bd3
Log: Fixed bug #74090 stream_get_contents maxlength&gt;-1 returns empty string
 [2017-02-15 13:52 UTC] ab@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC