PHP 4.0 Bug #8962: (8212) If the result of an exec() command is one character the result is empty.
| From: | bram at xspace dot com | Date: | Sun, 28 Jan 2001 05:15:46 +0000 |
| Subject: | PHP 4.0 Bug #8962: (8212) If the result of an exec() command is one character the result is empty. | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-45187@lists.php.net to get a copy of this message | ||
From: bram@xspace.com
Operating system: N/A
PHP version: 4.0.4pl1
PHP Bug Type: Unknown/Other Function
Bug description: (8212) If the result of an exec() command is one character the result is empty.
This is the solution to problem 8212
File: ext/standard/exec.c
Function _Exec:
Contains:
RETVAL_STRINGL(buf,l?l+1:0,1);
However, it should contain:
RETVAL_STRINGL(buf,l?l+1:((isspace((int)buf[0]) || !buf[0])?0:1),1);
The problem lies in code previous which reads:
/* strip trailing spaces */
l = strlen(buf);
t = l;
while (l && isspace((int)buf[--l]));
if (l < t) buf[l + 1] = '\0';
Obviously if the return value ends with the last line being a single character that DOES NOT end
with a \n, then strlen(buf) = 1 and the variable l ends up as 0.
The trim code leaves us in an ambiguous position after it is completed. The problem is that it
operates under the assumption that we end with a newline character. This is not always the case. I
suppose this is the problem with trying to trim whitespace in one line of code!
The suggested codefix simply checks when l==0 if buf[0] was actually trimmable, if not, then we know
we have to return exactly one character (instead of 0.) I have tested my solution and it seems to
work.
An alternate proposal would be to originally compute the trim in a more normal way:
while (l && isspace((int)buf[l-1])) l--;
But this affects more than one line of code.
--
Edit Bug report at: http://bugs.php.net/?id=8962&edit=1