#50732 [Opn]: exec() adds single byte twice to $output array

From: Date: Tue, 12 Jan 2010 22:00:28 +0000
Subject: #50732 [Opn]: exec() adds single byte twice to $output array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-146591@lists.php.net to get a copy of this message
 ID:               50732
 User updated by:  scope at planetavent dot de
 Reported By:      scope at planetavent dot de
 Status:           Open
 Bug Type:         Program Execution
 Operating System: Linux, Windows
 PHP Version:      5.3.1
 New Comment:

Of course, this applies as well to output that just ends with a newline
followed by a single byte.

Example:

#!/bin/sh
echo "Hello\nWorl\nd"

results in:

Array
(
    [0] => Hello
    [1] => Worl
    [2] => d
    [3] => d
)


Previous Comments:
------------------------------------------------------------------------

[2010-01-12 19:29:26] scope at planetavent dot de

Description:
------------
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)) {
...
continue;

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:
---------------
<?php
$command = "echo x";
exec( $command, $output );
print_r( $output );

Expected result:
----------------
Array
(
    [0] => x
)

Actual result:
--------------
Array
(
    [0] => x
    [1] => x
)


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=50732&edit=1



Thread (4 messages)

« previous php.bugs (#146591) next »