Bug #70717 [Opn->Csd]: PHP (cli) loses 1-2 bytes in reading interpreter directive on non-regular files
| From: | nikic@php.net | Date: | Thu, 05 Sep 2019 15:05:48 +0000 |
| Subject: | Bug #70717 [Opn->Csd]: PHP (cli) loses 1-2 bytes in reading interpreter directive on non-regular files | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-222571@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70717&edit=1
ID: 70717
Updated by: nikic@php.net
Reported by: dash dot php at inix dot me
Summary: PHP (cli) loses 1-2 bytes in reading interpreter
directive on non-regular files
-Status: Open
+Status: Closed
Type: Bug
Package: Unknown/Other Function
Operating System: Linux
PHP Version: 5.6.14
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
This issue is fixed in 7.4.
Previous Comments:
------------------------------------------------------------------------
[2015-10-15 10:06:22] dash dot php at inix dot me
Description:
------------
I am having an issue with PHP (cli) when using /dev/tty as the file. To run PHP code from STDIN, I
am able to run the command as follows,
$ php
<?php
echo "hello world\n";
?>
<CTRL-D>
hello world
$
However, when run through /dev/tty, the following happens,
$ php /dev/tty
<?php
echo "hello world\n";
?>
<CTRL-D>
?php
echo "hello world\n";
?>
$
NOTE: The first character < is simply lost and php processes the input as plain text.
Problem:-
The problem lies in sapi/cli/php_cli.c where interpreter directive is removed,
/* #!php support */
c = fgetc(file_handle->handle.fp);
if (c == '#' && (c = fgetc(file_handle->handle.fp)) == '!') {
while (c != '\n' && c != '\r' && c != EOF) {
c = fgetc(file_handle->handle.fp); /* skip to end of line */
}
/* handle situations where line is terminated by \r\n */
if (c == '\r') {
if (fgetc(file_handle->handle.fp) != '\n') {
long pos = ftell(file_handle->handle.fp);
fseek(file_handle->handle.fp, pos - 1, SEEK_SET);
}
}
*lineno = 2;
} else {
rewind(file_handle->handle.fp);
}
rewind() on /dev/tty or any named pipe would fail, causing the first 1-2 bytes to be lost.
Solution:-
We should test if the file is seekable, if it is not, we can process it similar to a pipe.
--- php_cli.c.orig 2015-10-15 14:39:33.875560414 +0530
+++ php_cli.c 2015-10-15 15:01:38.422751462 +0530
@@ -634,6 +634,10 @@
}
file_handle->filename = script_file;
+ if (fseek(file_handle->handle.fp, 0L, SEEK_SET) < 0) {
+ return SUCCESS;
+ }
+
/* #!php support */
c = fgetc(file_handle->handle.fp);
if (c == '#' && (c = fgetc(file_handle->handle.fp)) == '!') {
Additional Note:-
PHP Versions: 5.3.3 (cli) and 5.5.20 (cli)
OS: Linux (CentOS 6.4 and Fedora 19)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70717&edit=1