|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74252 Non-existent STDIN duplicates FD 0 instead of registering it as closed stream
Submitted: 2017-03-15 14:55 UTC Modified: 2019-09-17 15:30 UTC
From: Assigned: nikic (profile)
Status: Wont fix Package: Streams related
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
30 + 35 = ?
Subscribe to this entry?

 [2017-03-15 14:55 UTC]
If no STDIN exists, STDIN is still registered as FD 0 instead of a closed stream. This results in scripts without STDIN reading from /dev/urandom, because that's opened and just uses the next free FD.

Instead of duplicating the FD in it should be registered as closed stream.

See also

Test script:
php -r 'var_dump(!!fstat(STDIN));passthru("ls -o /proc/".getmypid()."/fd");' <&-

Expected result:
STDIN should be a closed stream instead of /dev/urandom

Actual result:
STDIN is /dev/urandom


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-16 11:55 UTC]
In fact, /dev/urandom is opened by some extension. With `./configure --disable-all` PHP segfaults with the same test case.

$ sapi/cli/php -r 'var_dump(!!fstat(STDIN));passthru("ls -o /proc/".getmypid()."/fd");' <&-
insgesamt 0
lr-x------ 1 kelunik 64 Mär 16 12:51 0 -> pipe:[7290202]
lrwx------ 1 kelunik 64 Mär 16 12:51 1 -> /dev/pts/1
lrwx------ 1 kelunik 64 Mär 16 12:51 2 -> /dev/pts/1
Speicherzugriffsfehler (Speicherabzug geschrieben)
 [2019-09-05 15:12 UTC]
-Assigned To: +Assigned To: nikic
 [2019-09-06 12:30 UTC]
The following pull request has been associated:

Patch Name: Handle closed stdio streams
On GitHub:
 [2019-09-17 15:30 UTC]
-Status: Assigned +Status: Wont fix
 [2019-09-17 15:30 UTC]
I've tried to address this in, but failed because stdin already get hijacked prior to main(). I think the only thing left to say here is that closed stdio streams are ill-defined (UB in C), and you should be piping /dev/null instead.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 14 00:01:26 2024 UTC