Skip to content

Commit e2289d4

Browse files
authored
Couple of edge cases in SourceResource. (#288)
* 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`. * `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?
1 parent 97bc570 commit e2289d4

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/SourceResource.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ public function __construct($resource)
2121
parent::__construct();
2222

2323
$this->onRead(static function (int $length) use (&$resource): ?string {
24-
return fread($resource, $length) ?: null;
24+
return (($read = fread($resource, $length)) !== '') ?
25+
$read :
26+
null;
2527
});
2628

2729
if (stream_get_meta_data($resource)['seekable']) {
2830
$this->onSeek(static function (int $offset, int $whence) use (&$resource): int {
29-
fseek($resource, $offset, $whence);
30-
return ftell($resource);
31+
return fseek($resource, $offset, $whence) === 0 ?
32+
ftell($resource) :
33+
-1;
3134
});
3235
}
3336
}

0 commit comments

Comments
 (0)