Skip to content

Legacy PHP < 5.4 SEGFAULTs when reading from pipes #78

Description

@clue

While working on react/child-process, I noticed occasional, but reproducible SEGFAULTs for really old PHP versions (< 5.4). I've managed to work around this in reactphp/child-process#29, but this actually boils down to an issue in this component when instantiating a new Stream with a pipe stream. Here's the gist of the broken code:

$s = popen('echo test', 'r');

stream_set_blocking($s, 0);
stream_set_read_buffer($s, 0);

$data = stream_get_contents($s, 8000);
var_dump($data);

Or the same issue when using the STDIN pipe:

$s = STDIN;

stream_set_blocking($s, 0);
stream_set_read_buffer($s, 0);

$data = stream_get_contents($s, 8000);
var_dump($data);

Apparently, this reads beyond the resource limits and returns a corrupted string value where strlen($data) === -1.

Activity

  1. clue commented on Mar 12, 2017

    @clue
    MemberAuthor

    It looks like the first example might in fact exhibit a race condition, while the second one can be used to reproduce this always when this is an interactive terminal. Also, if you explicitly close the input pipe via php second <&-, it crashed reproducible. Once you start piping stuff into the STDIN such as echo test | php second.php things start to work again, while interactive streams such as cat | php second.php keep crashing.

    I've checked for a common denominator between the affected streams with fstat() and stream_get_meta_data(), but couldn't find a reliable source. I have only observed crashs on pipe streams, but while some pipe streams crash, others work just fine.

    As such, we MAY want to look into simply enabling the read buffer again for ALL pipes, as I've not observed any side-effects of this. The read buffer was originally disabled via #20, but I've not managed to reproduce the original issue with pipe streams anyway.

    Keep in mind that this only affects legacy PHP < 5.4, so it's not exactly the most pressing issue.

  2. davidwdan commented on Mar 12, 2017

    @davidwdan

    Sounds like a good reason to drop support for PHP < 5.4 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions