Commit e08c44bf authored by Oliver Klee's avatar Oliver Klee Committed by Stefan Bürk
Browse files

[BUGFIX] Fix crashes for current and getInfo on empty storages

For empty storages, we need to avoid accessing inexistent array elements.

Resolves: #97417
Relates: #97554
Releases: main, 11.5
Change-Id: Ieee36b4c0ede9726ce936030f631a3322e3813a9
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/74342


Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Simon Schaufelberger's avatarSimon Schaufelberger <simonschaufi+typo3@gmail.com>
Tested-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Reviewed-by: crell's avatarcrell <larry@garfieldtech.com>
Reviewed-by: Simon Schaufelberger's avatarSimon Schaufelberger <simonschaufi+typo3@gmail.com>
Reviewed-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
parent 3dc65515
......@@ -180,7 +180,7 @@ class LazyObjectStorage extends ObjectStorage implements LoadingStrategyInterfac
}
/**
* @return TEntity The object at the current iterator position.
* @return TEntity|null The object at the current iterator position.
*
* @see \TYPO3\CMS\Extbase\Persistence\ObjectStorage::current
*/
......
......@@ -123,13 +123,14 @@ class ObjectStorage implements \Countable, \Iterator, \ArrayAccess, ObjectMonito
/**
* Returns the current storage entry.
*
* @return TEntity The object at the current iterator position.
* @return TEntity|null The object at the current iterator position.
*/
#[\ReturnTypeWillChange]
public function current()
{
$item = current($this->storage);
return $item['obj'];
return $item['obj'] ?? null;
}
/**
......@@ -265,7 +266,8 @@ class ObjectStorage implements \Countable, \Iterator, \ArrayAccess, ObjectMonito
public function getInfo()
{
$item = current($this->storage);
return $item['inf'];
return $item['inf'] ?? null;
}
/**
......
......@@ -25,6 +25,30 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
*/
class ObjectStorageTest extends UnitTestCase
{
/**
* @test
*/
public function currentForAnEmptyStorageReturnsNull(): void
{
$objectStorage = new ObjectStorage();
$result = $objectStorage->current();
self::assertNull($result);
}
/**
* @test
*/
public function getInfoForAnEmptyStorageReturnsNull(): void
{
$objectStorage = new ObjectStorage();
$result = $objectStorage->getInfo();
self::assertNull($result);
}
/**
* @test
*/
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment