From e58215e5e271bae899c910f04d646a65105071f6 Mon Sep 17 00:00:00 2001 From: Adam <607975+adam-vessey@users.noreply.github.com> Date: Fri, 13 Mar 2026 19:36:24 -0300 Subject: [PATCH 1/2] Potential read issue. Reading a single `"0"` could potentially be interpreted as a false response per PHP's truth table, resulting in returning a `null`. --- src/SourceResource.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/SourceResource.php b/src/SourceResource.php index 9eaefdd..55d6caf 100644 --- a/src/SourceResource.php +++ b/src/SourceResource.php @@ -21,7 +21,9 @@ public function __construct($resource) parent::__construct(); $this->onRead(static function (int $length) use (&$resource): ?string { - return fread($resource, $length) ?: null; + return (($read = fread($resource, $length)) !== '') ? + $read : + null; }); if (stream_get_meta_data($resource)['seekable']) { From 06066510f9fd2a66479d1d38db27bfad558f97f7 Mon Sep 17 00:00:00 2001 From: Adam <607975+adam-vessey@users.noreply.github.com> Date: Fri, 13 Mar 2026 19:43:48 -0300 Subject: [PATCH 2/2] `fseek()` can fail. Should `fseek()` fail, we probably want to flag the error. https://www.libvips.org/API/8.17/vfunc.Source.seek.html#return-value seems to suggest that a `-1` would be appropriate to return in such a case? --- src/SourceResource.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/SourceResource.php b/src/SourceResource.php index 55d6caf..73f8fd0 100644 --- a/src/SourceResource.php +++ b/src/SourceResource.php @@ -28,8 +28,9 @@ public function __construct($resource) if (stream_get_meta_data($resource)['seekable']) { $this->onSeek(static function (int $offset, int $whence) use (&$resource): int { - fseek($resource, $offset, $whence); - return ftell($resource); + return fseek($resource, $offset, $whence) === 0 ? + ftell($resource) : + -1; }); } }