|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #48803 fprintf(STDOUT, ...) and printf(...) behave differently and affect STDERR
Submitted: 2009-07-05 10:25 UTC Modified: 2010-12-20 12:44 UTC
From: andrey dot vihrov at gmail dot com Assigned: bjori
Status: Closed Package: Output Control
PHP Version: 5.*, 6CVS (2009-07-06) OS: Gentoo Linux amd64
Private report: No CVE-ID:
 [2009-07-05 10:25 UTC] andrey dot vihrov at gmail dot com
These functions behave differently if the underlying I/O operation on STDOUT fails. The difference is that STDERR is affected in one case.

Configure Command =>  './configure'  '--prefix=/usr/lib64/php5'
'--host=x86_64-pc-linux-gnu' '--mandir=/usr/lib64/php5/man'
'--infodir=/usr/lib64/php5/info' '--sysconfdir=/etc'
'--cache-file=./config.cache' '--with-libdir=lib64'
'--with-pcre-regex=/usr' '--enable-cli' '--disable-cgi'
'--without-pear' '--disable-bcmath' '--with-bz2' '--disable-calendar'
'--disable-ctype' '--without-curl' '--without-curlwrappers'
'--disable-dbase' '--enable-exif' '--without-fbsql' '--without-fdftk'
'--disable-filter' '--disable-ftp' '--with-gettext' '--without-gmp'
'--disable-hash' '--disable-ipv6' '--disable-json' '--without-kerberos'
'--enable-mbstring' '--with-mcrypt' '--without-mhash' '--without-msql'
'--without-mssql' '--with-ncurses' '--with-openssl'
'--with-openssl-dir=/usr' '--disable-pcntl' '--disable-pdo'
'--without-pgsql' '--disable-posix' '--with-pspell' '--without-recode'
'--disable-simplexml' '--disable-shmop' '--without-snmp'
'--disable-soap' '--disable-sockets' '--without-sybase'
'--without-sybase-ct' '--disable-sysvmsg' '--disable-sysvsem'
'--disable-sysvshm' '--without-tidy' '--disable-tokenizer'
'--disable-wddx' '--disable-xmlreader' '--disable-xmlwriter'
'--without-xmlrpc' '--without-xsl' '--disable-zip' '--with-zlib'
'--disable-debug' '--enable-dba' '--without-cdb' '--with-db4'
'--disable-flatfile' '--with-gdbm' '--disable-inifile' '--without-qdbm'
'--with-freetype-dir=/usr' '--with-t1lib=/usr' '--disable-gd-jis-conv'
'--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--without-xpm-dir'
'--with-gd' '--without-mysqli' '--with-readline' '--without-libedit'
'--without-mm' '--with-sqlite=/usr' '--enable-sqlite-utf8'

> diff php.ini-dist /etc/php/cli-php5/php.ini 
< ;include_path = ".:/php/includes"
> include_path = ".:/usr/share/php5:/usr/share/php"
< extension_dir = "./"
> extension_dir =
< allow_url_fopen = On
> allow_url_fopen = Off
> ; MySQL extensions default connection charset settings
> ;mysql.connect_charset = utf8
> ;mysqli.connect_charset = utf8
> ;pdo_mysql.connect_charset = utf8

Reproduce code:
Script #1

#! /usr/bin/php
	fprintf(STDOUT, "stdout\n");
	fprintf(STDERR, "stderr\n");

Testcase #2

#! /usr/bin/php
	fprintf(STDERR, "stderr\n");

Expected result:
> ./test1.php > /dev/full
> ./test2.php > /dev/full

Actual result:
> ./test1.php > /dev/full
> ./test2.php > /dev/full


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-05 11:20 UTC] sjoerd-php at linuxonly dot nl
Thank you for your bug report.

The problem is not that STDERR is affected, but that the script is terminated immediately. Furthermore, it is not specific for printf.

time php -r 'echo 'a'; sleep(5);' > /dev/full
Expected: real	0m5.030s
Actual: real	0m0.030s

Your contrived example writes to /dev/full, which obviously fails. Do you have a more real-life example where writing to STDOUT fails?
 [2009-07-06 11:58 UTC] andrey dot vihrov at gmail dot com
Hi, thanks for the explanation.

Quite simply errors can occur when writing to any real media (leaving the terminal aside). For a simple example, consider a PHP script that is being started with its output being redirected to a file. If the target filesystem, e. g., runs out of space or encounters an I/O error (like network fs being disconnected), the script will want to exit unsuccessfully and, optionally, inform the user about it using STDERR. If this is not the case, the user will assume things went good, which does not correspond to reality and may lead to further failures.

The example provided in previous comment is the classical one to check if software handles I/O errors correctly, so I used it for demonstration.
 [2009-07-06 13:00 UTC]
As a workaround, just set 'ignore_user_abort=1' in your CLI ini.
 [2009-07-07 12:59 UTC]
Actually this is by design. Since PHP is meant primarily for web usage, such "weird" things are necessary like using ignore_user_abort(true); to get full "power" for console scripts.

This should be mentioned in relevant parts in documentation. printf() mainly since that is especially tricky if you don't know things works under the hood.
 [2010-01-16 22:18 UTC]
I don't have the slightest idea what is expected to be documented here.

Could you please explain (preferably with a copy&pasteable example) 
what should be documented?
 [2010-01-16 23:27 UTC]
If you really think I can remember what was needed half a year ago, you must be dreaming. Figure it out yourself.
 [2010-01-17 14:45 UTC]
Automatic comment from SVN on behalf of bjori
Log: Hopefully fixed bug#48803
"copy&paste" from an email from Rasmus to internals@ recently
 [2010-01-17 14:46 UTC]
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

 [2010-12-20 12:44 UTC]
-Package: Tidy +Package: Output Control
PHP Copyright © 2001-2016 The PHP Group
All rights reserved.
Last updated: Wed Feb 10 04:01:59 2016 UTC