php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69009 socket_read() shouldn't trigger a warning error with PHP_NORMAL_READ
Submitted: 2015-02-07 23:54 UTC Modified: -
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: sjaillet at gmail dot com Assigned:
Status: Open Package: Sockets related
PHP Version: 5.6.5 OS: Ubuntu 14.04.1
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: sjaillet at gmail dot com
New email:
PHP Version: OS:

 

 [2015-02-07 23:54 UTC] sjaillet at gmail dot com
Description:
------------
According to the documentation socket_read() is supposed to return false
on error so the warning message is therefore unnecessary and makes the @socket_read() syntax mandatory.

Test script:
---------------
<?php
error_reporting(E_ALL);
set_time_limit(0);

ob_implicit_flush();

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo socket_strerror(socket_last_error()) . "\n";
    exit(-1);
}

if (socket_bind($sock, '127.0.0.1', 10000) === false) {
    echo socket_strerror(socket_last_error($sock)) . "\n";
    exit(-1);
}

if (socket_listen($sock, 5) === false) {
    echo socket_strerror(socket_last_error($sock)) . "\n";
    exit(-1);
}

$clients = [];

do {
    $read = [];
    $read[] = $sock;

    $read = array_merge($read, $clients);

    if (socket_select($read, $write, $except, 10) < 1)
    {
        continue;
    }

    if (in_array($sock, $read)) {
        if (($client = socket_accept($sock)) === false) {
            echo socket_strerror(socket_last_error($sock)) . "\n";
            break;
        }
        $clients[] = $client;
    }

    foreach ($clients as $key => $client) {
        if (in_array($client, $read)) {
            if (($buffer = socket_read($client, 1024)) === false) {
                unset($clients[$key]);
                echo socket_strerror(socket_last_error($client)) . "\n";
            }
            var_dump($buffer);
        }
    }
} while (true);

socket_close($sock);
?>

You can reproduce the bug by executing the following command in two different terminals.
Terminal 1:
php server.php

Terminal 2:
nc localhost 10000

Then hit Ctrl+C in the terminal 2 to close the process

Expected result:
----------------
Connection reset by peer
bool(false)


Actual result:
--------------
PHP Warning:  socket_read(): unable to read from socket [104]: Connection reset by peer in /home/jaillet/test.php on line 45
PHP Stack trace:
PHP   1. {main}() /home/jaillet/test.php:0
PHP   2. socket_read() /home/jaillet/test.php:45
Connection reset by peer
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Sep 20 07:01:26 2019 UTC