|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50732 exec() adds single byte twice to $output array
Submitted: 2010-01-12 19:29 UTC Modified: 2010-01-13 13:47 UTC
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: scope at planetavent dot de Assigned: iliaa (profile)
Status: Closed Package: Program Execution
PHP Version: 5.*, 6 OS: *
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: scope at planetavent dot de
New email:
PHP Version: OS:


 [2010-01-12 19:29 UTC] scope at planetavent dot de
If exec is used to start a command that outputs only a single byte, and if the $output array is used, the byte is added twice to the output array.

Tested with php 5.3.1 in cli mode on Windows 2003 Server and Ubuntu 9.10 Server.

This behaviour was introduced with bugfix to #49847:

From exec.c:125
while (php_stream_get_line(stream, b, EXEC_INPUT_BUF, &bufl)) {
if (b[bufl - 1] != '\n' && !php_stream_eof(stream)) {

If only a single byte is read, php_stream_eof(stream) returns true, thus no second loop will take place.

After line 149:
while (l-- && isspace(((unsigned char *)buf)[l]));

buflen is 1 and l is 0, therefor

line 160:
if ((type == 2 && bufl && !l) || type != 2) {

yields true and the buffer is added to the output array, again.

Reproduce code:
$command = "echo x";
exec( $command, $output );
print_r( $output );

Expected result:
    [0] => x

Actual result:
    [0] => x
    [1] => x


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-12 22:00 UTC] scope at planetavent dot de
Of course, this applies as well to output that just ends with a newline followed by a single byte.


echo "Hello\nWorl\nd"

results in:

    [0] => Hello
    [1] => Worl
    [2] => d
    [3] => d
 [2010-01-13 12:10 UTC]
Ilia, fix the fix please? (all branches of course)
 [2010-01-13 13:44 UTC]
Automatic comment from SVN on behalf of iliaa
Log: Fixed bug #50732 (exec() adds single byte twice to $output array).
 [2010-01-13 13:47 UTC]
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

 [2010-02-03 18:11 UTC]
Automatic comment from SVN on behalf of pajoye
Log: - merge: Fixed bug #50732 (exec() adds single byte twice to  array).
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 16 03:01:27 2024 UTC