Skip to content

Commit 1edecf7

Browse files
greedyivanfabpot
authored andcommitted
[Validator] fix access to uninitialized property when getting value
1 parent e50db1f commit 1edecf7

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/Symfony/Component/Validator/Mapping/PropertyMetadata.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ public function __construct($class, $name)
4848
*/
4949
public function getPropertyValue($object)
5050
{
51-
return $this->getReflectionMember($object)->getValue($object);
51+
$reflProperty = $this->getReflectionMember($object);
52+
53+
if (\PHP_VERSION_ID >= 70400 && !$reflProperty->isInitialized($object)) {
54+
return null;
55+
}
56+
57+
return $reflProperty->getValue($object);
5258
}
5359

5460
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\Validator\Tests\Fixtures;
4+
5+
class Entity_74
6+
{
7+
public int $uninitialized;
8+
}

src/Symfony/Component/Validator/Tests/Mapping/PropertyMetadataTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\Mapping\PropertyMetadata;
1616
use Symfony\Component\Validator\Tests\Fixtures\Entity;
17+
use Symfony\Component\Validator\Tests\Fixtures\Entity_74;
1718

1819
class PropertyMetadataTest extends TestCase
1920
{
2021
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
22+
const CLASSNAME_74 = 'Symfony\Component\Validator\Tests\Fixtures\Entity_74';
2123
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';
2224

2325
public function testInvalidPropertyName()
@@ -53,4 +55,15 @@ public function testGetPropertyValueFromRemovedProperty()
5355
$this->expectException('Symfony\Component\Validator\Exception\ValidatorException');
5456
$metadata->getPropertyValue($entity);
5557
}
58+
59+
/**
60+
* @requires PHP 7.4
61+
*/
62+
public function testGetPropertyValueFromUninitializedProperty()
63+
{
64+
$entity = new Entity_74();
65+
$metadata = new PropertyMetadata(self::CLASSNAME_74, 'uninitialized');
66+
67+
$this->assertNull($metadata->getPropertyValue($entity));
68+
}
5669
}

0 commit comments

Comments
 (0)