Skip to content

Commit

Permalink
[BUGFIX] Fix crashes for current and getInfo on empty storages
Browse files Browse the repository at this point in the history
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/+/74765
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
  • Loading branch information
oliverklee authored and sbuerk committed Jun 4, 2022
1 parent 7a637e8 commit a2c1edc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function count(): int
}

/**
* @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
*/
Expand Down
8 changes: 5 additions & 3 deletions typo3/sysext/extbase/Classes/Persistence/ObjectStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ public function key()
/**
* 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;
}

/**
Expand Down Expand Up @@ -265,7 +266,8 @@ public function detach($object)
public function getInfo()
{
$item = current($this->storage);
return $item['inf'];

return $item['inf'] ?? null;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions typo3/sysext/extbase/Tests/Unit/Persistence/ObjectStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@
*/
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
*/
Expand Down

0 comments on commit a2c1edc

Please sign in to comment.