8000 [VarExporter] Fix adding a key to an uninitialized array by nicolas-grekas · Pull Request #48605 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarExporter] Fix adding a key to an uninitialized array #48605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[VarExporter] Fix adding a key to an uninitialized array
  • Loading branch information
nicolas-grekas committed Dec 12, 2022
commit 57c6a623b34da9f4c15978cc7a07e29f0fc2d2fc
36 changes: 28 additions & 8 deletions src/Symfony/Component/VarExporter/LazyGhostTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,37 @@ public function &__get($name): mixed

get_in_scope:

if (null === $scope) {
if (null === $readonlyScope) {
return $this->$name;
try {
if (null === $scope) {
if (null === $readonlyScope) {
return $this->$name;
}
$value = $this->$name;

return $value;
Comment on lines +198 to +203
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this block (198-203) just a very complicated way to say return $this->name;?

Copy link
Member Author
@nicolas-grekas nicolas-grekas Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not :)
The function returns by reference. The first "if" does return a reference. The 2nd "return" does not return a reference.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. 😓

}
$value = $this->$name;
$accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);

return $value;
}
$accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
return $accessor['get']($this, $name, null !== $readonlyScope);
} catch (\Error $e) {
if (\Error::class !== $e::class || !str_starts_with($e->getMessage(), 'Cannot access uninitialized non-nullable property')) {
throw $e;
}

try {
if (null === $scope) {
$this->$name = [];

return $this->$name;
}

return $accessor['get']($this, $name, null !== $readonlyScope);
$accessor['set']($this, $name, []);

return $accessor['get']($this, $name, null !== $readonlyScope);
} catch (\Error) {
throw $e;
}
}
}

public function __set($name, $value): void
Expand Down
36 changes: 28 additions & 8 deletions src/Symfony/Component/VarExporter/LazyProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,37 @@ public function &__get($name): mixed

get_in_scope:

if (null === $scope) {
if (null === $readonlyScope && 1 !== $parent) {
return $instance->$name;
try {
if (null === $scope) {
if (null === $readonlyScope && 1 !== $parent) {
return $instance->$name;
}
$value = $instance->$name;

return $value;
}
$value = $instance->$name;
$accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);

return $value;
}
$accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
return $accessor['get']($instance, $name, null !== $readonlyScope || 1 === $parent);
} catch (\Error $e) {
if (\Error::class !== $e::class || !str_starts_with($e->getMessage(), 'Cannot access uninitialized non-nullable property')) {
throw $e;
}

try {
if (null === $scope) {
$instance->$name = [];

return $instance->$name;
}

return $accessor['get']($instance, $name, null !== $readonlyScope || 1 === $parent);
$accessor['set']($instance, $name, []);

return $accessor['get']($instance, $name, null !== $readonlyScope || 1 === $parent);
} catch (\Error) {
throw $e;
}
}
}

public function __set($name, $value): void
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\VarExporter\Internal\LazyObjectState;
use Symfony\Component\VarExporter\ProxyHelper;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildMagicClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildStdClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildTestClass;
Expand Down Expand Up @@ -393,4 +394,41 @@ public function testFullInitializationAfterPartialInitialization()
$this->assertSame(3, ((array) $instance)["\0".TestClass::class."\0private"]);
$this->assertSame(1001, $counter);
}

public function testIndirectModification()
{
$obj = new class() {
public array $foo;
};
$proxy = $this->createLazyGhost($obj::class, fn () => null);

$proxy->foo[] = 123;

$this->assertSame([123], $proxy->foo);
}

/**
* @template T
*
* @param class-string<T> $class
*
* @return T
*/
private function createLazyGhost(string $class, \Closure|array $initializer, array $skippedProperties = null): object
{
$r = new \ReflectionClass($class);

if (str_contains($class, "\0")) {
$class = __CLASS__.'\\'.debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'].'_L'.$r->getStartLine();
class_alias($r->name, $class);
}
$proxy = str_replace($r->name, $class, ProxyHelper::generateLazyGhost($r));
$class = str_replace('\\', '_', $class).'_'.md5($proxy);

if (!class_exists($class, false)) {
eval((\PHP_VERSION_ID >= 80200 && $r->isReadOnly() ? 'readonly ' : '').'class '.$class.' '.$proxy);
}

return $class::createLazyGhost($initializer, $skippedProperties);
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ public function setFoo($foo): static
$this->assertSame(234, $proxy->foo);
}

public function testIndirectModification()
{
$obj = new class() {
public array $foo;
};
$proxy = $this->createLazyProxy($obj::class, fn () => $obj);

$proxy->foo[] = 123;

$this->assertSame([123], $proxy->foo);
}

/**
* @requires PHP 8.2
*/
Expand Down
0