Skip to content

Commit

Permalink
TmpStreamSerializerTrait::serialize(): harden against empty graph
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Aug 29, 2023
1 parent 5d12a52 commit 2ecaf20
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/quickRdfIo/TmpStreamSerializerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
*/
trait TmpStreamSerializerTrait {

public function serialize(iQuadIterator | iQuadIteratorAggregate $graph, ?iRdfNamespace $nmsp = null): string {
public function serialize(iQuadIterator | iQuadIteratorAggregate $graph,
?iRdfNamespace $nmsp = null): string {
$output = '';
$stream = fopen('php://memory', 'r+');
if ($stream === false) {
Expand All @@ -48,10 +49,13 @@ public function serialize(iQuadIterator | iQuadIteratorAggregate $graph, ?iRdfNa
if ($len === false || $len < 0) {
throw new RdfIoException('Failed to seek in output streem');
}
rewind($stream);
$output = fread($stream, $len);
if ($output === false) {
throw new RdfIoException('Failed to read from output streem');
$output = '';
if ($len > 0) {
rewind($stream);
$output = fread($stream, $len);
if ($output === false) {
throw new RdfIoException('Failed to read from output streem');
}
}
fclose($stream);
return $output;
Expand Down

0 comments on commit 2ecaf20

Please sign in to comment.