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

[BUG] PHP Fatal error: Uncaught TypeError: strspn(): Argument #1 ($string) must be of type string, null given in /framework/simplepie/src/Content/Type/Sniffer.php:153 #810

Open
thE-iNviNciblE opened this issue Mar 18, 2023 · 3 comments
Assignees
Labels

Comments

@thE-iNviNciblE
Copy link

thE-iNviNciblE commented Mar 18, 2023

IMPORTANT: If you are posting code or output, please make sure you wrap it with fenced code blocks so that it is readable.

Description

$ws = strspn($this->file->body, "\x09\x0A\x0B\x0C\x0D\x20"); - simplepie/src/Content/Type/Sniffer.php:153

$this->file->body is empty.

Steps to Reproduce

it's maybe a empty resource where the DOM HTML is empty.

Expected Behavior

skip invalid RSS-Source.

Actual Behavior

Fatal error cron job exits.

Possible Solutions

strlen() Check should work to avoid strspn() to crash.

       if(!is_null($this->file->body)) {
        
            $ws = strspn($this->file->body, "\x09\x0A\x0B\x0C\x0D\x20");
            if (strtolower(substr($this->file->body, $ws, 14)) === '<!doctype html'
                || strtolower(substr($this->file->body, $ws, 5)) === '<html'
                || strtolower(substr($this->file->body, $ws, 7)) === '<script') {
                return 'text/html';
            } elseif (substr($this->file->body, 0, 5) === '%PDF-') {
                return 'application/pdf';
            } elseif (substr($this->file->body, 0, 11) === '%!PS-Adobe-') {
                return 'application/postscript';
            } elseif (substr($this->file->body, 0, 6) === 'GIF87a'
                || substr($this->file->body, 0, 6) === 'GIF89a') {
                return 'image/gif';
            } elseif (substr($this->file->body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") {
                return 'image/png';
            } elseif (substr($this->file->body, 0, 3) === "\xFF\xD8\xFF") {
                return 'image/jpeg';
            } elseif (substr($this->file->body, 0, 2) === "\x42\x4D") {
                return 'image/bmp';
            } elseif (substr($this->file->body, 0, 4) === "\x00\x00\x01\x00") {
                return 'image/vnd.microsoft.icon';
            }
            
            return $this->text_or_binary();
        }

Environment

Debian 10 with PHP 8.2

@jtojnar
Copy link
Contributor

jtojnar commented Mar 18, 2023

Thanks for reporting. Do you have the error message? strspn should be fine with an empty string.

@jtojnar
Copy link
Contributor

jtojnar commented Mar 18, 2023

Oh, never mind, it is in the title. Ugh, we should really have checked the codebase with PHPStan before releasing with strict_deps.

Perhaps initializing the body to an empty string here will work as a stop gap:

public $body;

This part of the code will be re-done with #774 so that will hopefully fix it properly.

@thE-iNviNciblE thE-iNviNciblE changed the title [BUG] PHP Fatal error: Uncaught TypeError: strspn(): Argument #1 ($string) must be of type string, null given in /var/www/vhosts/xxx/framework/simplepie/src/Content/Type/Sniffer.php:153 [BUG] PHP Fatal error: Uncaught TypeError: strspn(): Argument #1 ($string) must be of type string, null given in /framework/simplepie/src/Content/Type/Sniffer.php:153 Mar 18, 2023
@thE-iNviNciblE
Copy link
Author

Oh, never mind, it is in the title. Ugh, we should really have checked the codebase with PHPStan before releasing with strict_deps.

Perhaps initializing the body to an empty string here will work as a stop gap:

public $body;

This part of the code will be re-done with #774 so that will hopefully fix it properly.

it's a PHP 8.2 thing, It should work with public $body=""; it's a better solution.

PHP Fatal error: Uncaught TypeError: strspn(): Argument #1 ($string) must be of type string, null given in /simplepie/src/Content/Type/Sniffer.php:153
Stack trace:
#0 /simplepie/src/Content/Type/Sniffer.php(153): strspn()
#1 /simplepie/src/Content/Type/Sniffer.php(124): SimplePie\Content\Type\Sniffer->unknown()
#2 /simplepie/src/Locator.php(148): SimplePie\Content\Type\Sniffer->get_type()
#3 /simplepie/src/SimplePie.php(1834): SimplePie\Locator->is_feed()
#4 /simplepie/src/SimplePie.php(1591): SimplePie\SimplePie->fetch_data()
#5 /simplepie/src/SimplePie.php(1564): SimplePie\SimplePie->init()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants