PHP incorrectly closes file handles when used to replace STDIN/STDOUT/STDERR
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
Description
The following code:
<?php
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
fopen('/dev/null', 'r');
fopen('/dev/null', 'w');
fopen('/dev/null', 'w');
exec("dig +noall +answer -t ns -q 'www.google.com'", $out, $ret);
file_put_contents('ret.txt', $ret . "\n");
Resulted in exec failing with the return code 134 as seen in ret.txt
PHP's garbage collection seems to have not taken into account that the file handles opened are now in use as stdin/stdout/stderr. Obviously this can be worked around by storing the result of fopen into some global vars, but IMO this is a bug.
PHP Version
PHP 8.2.6
Operating System
Debian 11
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the PHP 8.2.6 example on Debian 11, focusing on the replacement of STDIN, STDOUT, and STDERR and the later garbage collection. Trace how those standard streams are handled before exec runs. Done means the command no longer fails with return code 134 when the replacement handles are not retained in global variables.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100