php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Stream Wrapper + Stream Filter cannot longer alter PHP file since PHP 7.4

Open lyrixx opened this issue 4 years ago • 5 comments

Description

I created a minimal gist to reproduce the error since it need at least 3 files.

https://gist.github.com/lyrixx/13df9efcfd08fee999c6b2bac0106117

>…re/dev/labs/php/rename-legacy-function(main *) php7.2 index.php 
/home/gregoire/dev/labs/php/rename-legacy-function/test1.php
/home/gregoire/dev/labs/php/rename-legacy-function/test2.php

>…re/dev/labs/php/rename-legacy-function(main *) php7.3 index.php 
/home/gregoire/dev/labs/php/rename-legacy-function/test1.php
/home/gregoire/dev/labs/php/rename-legacy-function/test2.php

>…re/dev/labs/php/rename-legacy-function(main *) php7.4 index.php 
PHP Parse error:  syntax error, unexpected end of file, expecting ';' in /home/gregoire/dev/labs/php/rename-legacy-function/test1.php on line 8

Parse error: syntax error, unexpected end of file, expecting ';' in /home/gregoire/dev/labs/php/rename-legacy-function/test1.php on line 8

see this part of gist to understand : https://gist.github.com/lyrixx/13df9efcfd08fee999c6b2bac0106117#file-function-fixer-php-L290-L293


The idea is to rename some functions (for dealing with a legacy and dirty codebase) with a stream filter. It used to work with older PHP version, but it seems it's broken since PHP 7.4.

PHP Version

7.4

Operating System

Ubuntu 20.04

lyrixx avatar Mar 18 '22 14:03 lyrixx

PHP 7.4 is no longer actively supported, but I can reproduce the reported behavior with PHP-8.0. However, this is not a bug in PHP, but rather a bug in the userland script. In StreamFilter::filter(), $consumed needs to be increased by the length of the modified stream contents; you can easily verify that by changing StreamFilter::doFilter():

$buffer = str_replace("foobar", "foob_$rand", $buffer);

cmb69 avatar Mar 21 '22 11:03 cmb69

@cmb69 Thanks for the feeback. Indeed I make the test with PHP 8.1 but I tried to find the version where it has broken.

I already tried to update the $consumed variable with the new new length but it did not work. I think something is broken in PHP :/ Of course, if I keep the same content length it works.

Did you manage to make it work?

lyrixx avatar Mar 21 '22 13:03 lyrixx

Okay, I had a closer look, and found that the behavioral change has been introduced by c2c5c9a973559b7ba9f84337c5f8078e59e98a91. So the problem with the existing code is actually the StreamWrapper::stream_stat() implementation. At least the size needs to properly match the filtered output. Quick fix for the given script:

    public function stream_stat(): array
    {
        $stat = fstat($this->resource);
        $stat["size"] = $stat["size"] + 4;
        return $stat;
    }

Of course, that won't work in practice; instead the code needs to track the actual size.

cmb69 avatar Mar 21 '22 16:03 cmb69

So I'm not sure, is it bug in PHP, or something that need to be fixed in my script?

lyrixx avatar Mar 22 '22 10:03 lyrixx

You need to fix your script, so that StreamWrapper::stream_stat() returns the size of the filtered stream (not the original stream).

cmb69 avatar Mar 22 '22 10:03 cmb69