Skip to content

Legacy PHP < 5.4 SEGFAULTs when reading from pipes #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
clue opened this issue Mar 12, 2017 · 2 comments
Closed

Legacy PHP < 5.4 SEGFAULTs when reading from pipes #78

clue opened this issue Mar 12, 2017 · 2 comments

Comments

@clue
Copy link
Member

clue commented Mar 12, 2017

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.

@clue
Copy link
Member Author

clue commented Mar 12, 2017

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.

@davidwdan
Copy link

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
Projects
None yet
Development

No branches or pull requests

2 participants