Skip to content

Commit

Permalink
bug #35532 [Validator] fix access to uninitialized property when gett…
Browse files Browse the repository at this point in the history
…ing value (greedyivan)

This PR was squashed before being merged into the 3.4 branch (closes #35532).

Discussion
----------

[Validator] fix access to uninitialized property when getting value

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #35454
| License       | MIT
| Doc PR        |

In PHP 7.4, the type-hinted property is [uninitialized](https://wiki.php.net/rfc/typed_properties_v2#uninitialized_and_unset_properties) by default. So it needs to be checked before use.

Commits
-------

1edecf7 [Validator] fix access to uninitialized property when getting value
  • Loading branch information
fabpot committed Jan 31, 2020
2 parents e50db1f + 1edecf7 commit aeea451
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Symfony/Component/Validator/Mapping/PropertyMetadata.php
Expand Up @@ -48,7 +48,13 @@ public function __construct($class, $name)
*/
public function getPropertyValue($object)
{
return $this->getReflectionMember($object)->getValue($object);
$reflProperty = $this->getReflectionMember($object);

if (\PHP_VERSION_ID >= 70400 && !$reflProperty->isInitialized($object)) {
return null;
}

return $reflProperty->getValue($object);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Validator/Tests/Fixtures/Entity_74.php
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\Validator\Tests\Fixtures;

class Entity_74
{
public int $uninitialized;
}
Expand Up @@ -14,10 +14,12 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Mapping\PropertyMetadata;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
use Symfony\Component\Validator\Tests\Fixtures\Entity_74;

class PropertyMetadataTest extends TestCase
{
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
const CLASSNAME_74 = 'Symfony\Component\Validator\Tests\Fixtures\Entity_74';
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';

public function testInvalidPropertyName()
Expand Down Expand Up @@ -53,4 +55,15 @@ public function testGetPropertyValueFromRemovedProperty()
$this->expectException('Symfony\Component\Validator\Exception\ValidatorException');
$metadata->getPropertyValue($entity);
}

/**
* @requires PHP 7.4
*/
public function testGetPropertyValueFromUninitializedProperty()
{
$entity = new Entity_74();
$metadata = new PropertyMetadata(self::CLASSNAME_74, 'uninitialized');

$this->assertNull($metadata->getPropertyValue($entity));
}
}

0 comments on commit aeea451

Please sign in to comment.