php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44218 Misdirected output after closing stdout/stderr and working with files.
Submitted: 2008-02-22 17:58 UTC Modified: 2008-02-24 00:35 UTC
From: exe at travian dot org Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.2.5 OS: GNU/Linux Kernel 2.6.18
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: exe at travian dot org
New email:
PHP Version: OS:

 

 [2008-02-22 17:58 UTC] exe at travian dot org
Description:
------------
If STDOUT/STDERR are closed by a script then the first two file handles opened by that script afterwards will take the place of the former STDOUT/STDERR handles. This will cause any output which normaly goes to STDOUT (like in print/echo statements) to be written to the new file handles. This behaviour may corrupt files written by that script.

This is a similar problem as in bug #44217.

Reproduce code:
---------------
<?php
fclose(STDOUT); fclose(STDERR);

$fp1 = fopen('test.log', 'a'); // File handle 1, formerly STDOUT
$fp2 = fopen('test.log', 'a'); // File handle 2, formerly STDERR

print "foo"; // Will be written to test.log
?>

Expected result:
----------------
No output from php, no appended output in test.log

Actual result:
--------------
String "foo" will be appended to test.log

strace shows that php first closes file handle 1 (stdout) and 2 (stderr) and then openes two file handles, which get the numbers 1 and 2, and then print "foo" to file handle 1 which now points to test.log:

read(3, "<?php\nfclose(STDOUT); fclose(STD"..., 8192) = 217
read(3, "", 4096)                       = 0
read(3, "", 8192)                       = 0
close(3)                                = 0
munmap(0x2b97a043e000, 4096)            = 0
close(1)                                = 0
munmap(0x2b97a0440000, 4096)            = 0
close(2)                                = 0
getcwd("/root", 4096)                   = 6
lstat("/root", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/root/test.log", {st_mode=S_IFREG|0644, st_size=290, ...}) = 0
open("/root/test.log", O_WRONLY|O_APPEND|O_CREAT, 0666) = 1
fstat(1, {st_mode=S_IFREG|0644, st_size=290, ...}) = 0
lseek(1, 0, SEEK_CUR)                   = 0
lseek(1, 0, SEEK_CUR)                   = 0
getcwd("/root", 4096)                   = 6
open("/root/test.log", O_WRONLY|O_APPEND|O_CREAT, 0666) = 2
fstat(2, {st_mode=S_IFREG|0644, st_size=290, ...}) = 0
lseek(2, 0, SEEK_CUR)                   = 0
lseek(2, 0, SEEK_CUR)                   = 0
write(1, "foo", 3)                      = 3
close(2)                                = 0
close(1)                                = 0
close(0)                                = 0
munmap(0x2b97a043f000, 4096)            = 0
munmap(0x2b97a03fd000, 266240)          = 0
mmap(NULL, 266240, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b97a03fd000
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
munmap(0x2b97a03fd000, 266240)          = 0
brk(0xd36000)                           = 0xd36000
exit_group(0)                           = ?
Process 19229 detached



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-24 00:35 UTC] jani@php.net
This is same issue. One report per problem.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 21:01:28 2024 UTC