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

Fix error handling in Stream::getContents() #294

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use Throwable;

use function fclose;
use function feof;
Expand All @@ -25,7 +26,9 @@
use function is_resource;
use function is_string;
use function pclose;
use function restore_error_handler;
use function rewind;
use function set_error_handler;
use function stream_get_contents;
use function stream_get_meta_data;
use function strstr;
Expand Down Expand Up @@ -354,7 +357,7 @@
$contents = false;

if ($this->stream) {
$contents = stream_get_contents($this->stream);
$contents = $this->getStreamContents();
}

if (is_string($contents)) {
Expand Down Expand Up @@ -393,4 +396,36 @@

return $this->isPipe;
}

/**
* Reads stream into a string.
*
* @throws RuntimeException
*
* return string The contents
*/
protected function getStreamContents(): string
{
$contents = false;
$exception = null;

set_error_handler(static function ($type, $message) use (&$exception) {
throw $exception = new RuntimeException('Unable to read stream contents: ' . $message);
});

try {
$contents = stream_get_contents($this->stream);

Check failure on line 417 in src/Stream.php

View workflow job for this annotation

GitHub Actions / Tests PHP 8.2

Parameter #1 $stream of function stream_get_contents expects resource, resource|null given.
Copy link

@piotr-cz piotr-cz Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value $this->resource in fact may be null in following edge case:

class StreamExtended extends Stream
{
    public function brokenMethod()
    {
        // This sets $this->resource to null...
        $this->detach();
        // ...and in effect this calls stream_get_contents with null parameter
        $this->getStreamContents();
    }
}

I see folowing possible solutions:

  • move method body exactly where the $contents = $this->getStreamContents(); was
  • throw an Exception then $this->resource === null
  • force stream to be a type of resource by changing new method's signature to protected static getStreamContents(resource $stream): string;

} catch (Throwable $e) {
throw $e === $exception ? $e :
new RuntimeException('Unable to read stream contents: ' . $e->getMessage(), 0, $e);
} finally {
restore_error_handler();
}

if (is_string($contents)) {
return $contents;
}

throw new RuntimeException('Could not get contents of stream.');
}
}
Loading