Skip to content

Commit

Permalink
BUGFIX: Support empty data in SimpleFileBackend
Browse files Browse the repository at this point in the history
In newer versions of PHP, calling `fread` with non-positive length
raises an error:
fread(): Argument #2 ($length) must be greater than 0

Since SimpleFileBackend supports saving an empty file, it should also
support reading one.
  • Loading branch information
christianharrington committed Nov 10, 2022
1 parent aeff374 commit 1b96769
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Neos.Cache/Classes/Backend/SimpleFileBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,16 @@ protected function readCacheFile(string $cacheEntryPathAndFilename, int $offset
if ($offset !== null) {
fseek($file, $offset);
}
$data = fread($file, $maxlen !== null ? $maxlen : filesize($cacheEntryPathAndFilename) - (int)$offset);

$length = $maxlen !== null ? $maxlen : filesize($cacheEntryPathAndFilename) - (int)$offset;

// fread requires a positive length. If the file is empty, we just return an empty string.
if ($length === 0) {
$data = '';
} else {
$data = fread($file, $length);
}

flock($file, LOCK_UN);
}
fclose($file);
Expand Down
16 changes: 16 additions & 0 deletions Neos.Cache/Tests/Unit/Backend/SimpleFileBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,22 @@ public function getReturnsContentOfTheCorrectCacheFile()
self::assertSame($data2, $simpleFileBackend->get($entryIdentifier));
}

/**
* @test
*/
public function getSupportsEmptyData()
{
$this->mockCacheFrontend->expects(self::any())->method('getIdentifier')->will(self::returnValue('UnitTestCache'));

$data = '';
$entryIdentifier = 'SimpleFileBackendTest';

$simpleFileBackend = $this->getSimpleFileBackend();
$simpleFileBackend->set($entryIdentifier, $data);

self::assertSame($data, $simpleFileBackend->get($entryIdentifier));
}

/**
* @test
*/
Expand Down

0 comments on commit 1b96769

Please sign in to comment.