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: 2021-03-15 15:57 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: sjaillet at gmail dot com Assigned: cmb (profile)
Status: Not a bug 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

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-03-15 15:57 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-03-15 15:57 UTC] cmb@php.net
Returning false on failure plus raising a warning is pretty common
for many PHP functions, and certainly not a bug.  If you want to
change this behavior, please pursue the RFC process[1].

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu May 13 16:01:23 2021 UTC