php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70717 PHP (cli) loses 1-2 bytes in reading interpreter directive on non-regular files
Submitted: 2015-10-15 10:06 UTC Modified: 2019-09-05 15:05 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: dash dot php at inix dot me Assigned: nikic (profile)
Status: Closed Package: Unknown/Other Function
PHP Version: 5.6.14 OS: Linux
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.
Password:
Status:
Package:
Bug Type:
Summary:
From: dash dot php at inix dot me
New email:
PHP Version: OS:

 

 [2015-10-15 10:06 UTC] 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)



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-09-05 15:05 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-09-05 15:05 UTC] nikic@php.net
This issue is fixed in 7.4.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC