Bug #70717 [NEW]: PHP (cli) loses 1-2 bytes in reading interpreter directive on non-regular files

From: Date: Thu, 15 Oct 2015 10:06:27 +0000
Subject: Bug #70717 [NEW]: PHP (cli) loses 1-2 bytes in reading interpreter directive on non-regular files
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-196618@lists.php.net to get a copy of this message
From: dash dot php at inix dot me Operating system: Linux PHP version: 5.6.14 Package: Unknown/Other Function Bug Type: Bug Bug description:PHP (cli) loses 1-2 bytes in reading interpreter directive on non-regular files 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 bug report at https://bugs.php.net/bug.php?id=70717&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70717&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70717&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70717&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=70717&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=70717&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=70717&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=70717&r=needscript Try newer version: https://bugs.php.net/fix.php?id=70717&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=70717&r=support Expected behavior: https://bugs.php.net/fix.php?id=70717&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=70717&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=70717&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=70717&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70717&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=70717&r=dst IIS Stability: https://bugs.php.net/fix.php?id=70717&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=70717&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=70717&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=70717&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=70717&r=mysqlcfg

« previous php.bugs (#196618) next »