Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not handle fgets returning an error gracefully #68

Closed
mglaman opened this issue Sep 15, 2021 · 6 comments · Fixed by #70
Closed

Does not handle fgets returning an error gracefully #68

mglaman opened this issue Sep 15, 2021 · 6 comments · Fixed by #70
Labels
bug Something isn't working

Comments

@mglaman
Copy link

mglaman commented Sep 15, 2021

If fgets returns false due to an error, this package will crash as it passes a bool to strpos:

TypeError: strpos() expects parameter 1 to be string, bool given in strpos() (line 109 of /var/www/html/jwtest-com/vendor/typo3/phar-stream-wrapper/src/Phar/Reader.php).

I'm still not certain how we got an error from fgets. We were trying to launch the phpstan.phar:

TYPO3\PharStreamWrapper\Phar\Reader->extractData('/var/www/html/jwtest-com/vendor/phpstan/phpstan/phpstan.phar') (Line: 53)

Relevant issue on Drupal.org. This gets triggered with:

    if (!class_exists('PHPStan\ExtensionInstaller\GeneratedConfig')) {

That causes the PHPStan\PharAutoloader to execute:

PHPStan\PharAutoloader::loadClass('PHPStan\ExtensionInstaller\GeneratedConfig')
spl_autoload_call('PHPStan\ExtensionInstaller\GeneratedConfig')
class_exists('PHPStan\ExtensionInstaller\GeneratedConfig') (Line: 589)

And we end up broken. I don't know how many lines it takes to get an error, but it's before feof returns true.

@ohader
Copy link
Member

ohader commented Sep 15, 2021

Thanks for reporting this. Is there a ticket in Drupal's issue tracker? I'm just trying to get some more scenario and context details. Anyway, I'll try to reproduce that behavior soonish.

That's the position that triggers the TypeError:
https://github.com/TYPO3/phar-stream-wrapper/blob/master/src/Phar/Reader.php#L109

If possible, please provide the version of phpstan.phar that has been used and a debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS ) in case $line === false.

@ohader
Copy link
Member

ohader commented Sep 16, 2021

Okay... independent from PharStreamWrapper I could reproduce fgets resolving to false at the end of a file offset, without(!) having feof() === true. Basically I was reading 2-byte chunks from a total of 13 bytes. I was able to reproduce that on macOS with PHP 7.4, but not with PHP 8.0. Well...

I think it's fine to add an additional guard for this case. Still, finding out what really happens with phpstan.phar of course would be interesting.

@ohader ohader added the bug Something isn't working label Sep 16, 2021
ohader added a commit to ohader/phar-stream-wrapper that referenced this issue Sep 16, 2021
ohader added a commit to ohader/phar-stream-wrapper that referenced this issue Sep 16, 2021
@ohader
Copy link
Member

ohader commented Sep 16, 2021

@mglaman Please let me know whether PR #70 fixes the issue for Drupal. Thx

ohader added a commit to ohader/phar-stream-wrapper that referenced this issue Sep 20, 2021
@mglaman
Copy link
Author

mglaman commented Sep 20, 2021

Sorry! I linked this GitHub issue to the Drupal issue, but not here to there. Drupal issue: https://www.drupal.org/project/upgrade_status/issues/3232011

@mglaman
Copy link
Author

mglaman commented Sep 20, 2021

I was able to reproduce that on macOS with PHP 7.4, but not with PHP 8.0. Well...

I had PHP 8.0, locally maybe that's why I couldn't reproduce. I'll see if I can work the user to get a reproduction and more data.

ohader added a commit to ohader/phar-stream-wrapper that referenced this issue Sep 20, 2021
ohader added a commit to ohader/phar-stream-wrapper that referenced this issue Sep 20, 2021
@ohader
Copy link
Member

ohader commented Sep 20, 2021

Let's have a look, when that actually happens - thus, when PHP has problems to read from a stream, without issuing feof.

https://github.com/php/php-src/blob/PHP-7.4.20/ext/standard/file.c#L1122-L1127

false is returned when underlying stream invocation returns null, let's have a look...

https://github.com/php/php-src/blob/PHP-7.4.20/main/streams/streams.c#L991-L996

null is returned nothing could be read from a stream (zero bytes copied to memory).


tl;dr: fgets($resource) returns false, when not a single byte could be read from a string, as indicated by variable total_copied.

I have not able to reproduce this behavior with DDEV and PHP 7.4.20 on macOS.

ohader added a commit that referenced this issue Sep 20, 2021
ohader added a commit that referenced this issue Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants