Skip to content

Commit

Permalink
bug #20146 [Validator] Prevent infinite loop in PropertyMetadata (wes…
Browse files Browse the repository at this point in the history
…leylancel)

This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #20146).

Discussion
----------

[Validator] Prevent infinite loop in PropertyMetadata

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |n/a
| License       | MIT
| Doc PR        | n/a

This commit fixes a possible infinite loop in PropertyMetadata when the PropertyMetadata class was constructed with an existing property of class and later used (after being serialized and cached) on that same class while that property no longer existing. `get_parent_class` will return false when there is no parent class and `property_existing` will then keeping return false causing the `while` loop to be infinite.

Commits
-------

c1ae7b6 Prevent infinite loop in PropertyMetadata
  • Loading branch information
fabpot committed Oct 13, 2016
2 parents ab6f181 + c1ae7b6 commit 705a4f7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Mapping/PropertyMetadata.php
Expand Up @@ -58,8 +58,14 @@ public function getPropertyValue($object)
*/
protected function newReflectionMember($objectOrClassName)
{
$originalClass = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);

while (!property_exists($objectOrClassName, $this->getName())) {
$objectOrClassName = get_parent_class($objectOrClassName);

if (false === $objectOrClassName) {
throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s"', $this->getName(), $originalClass));
}
}

$member = new \ReflectionProperty($objectOrClassName, $this->getName());
Expand Down
Expand Up @@ -42,4 +42,14 @@ public function testGetPropertyValueFromOverriddenPrivateProperty()
$this->assertTrue($metadata->isPublic($entity));
$this->assertEquals('Overridden data', $metadata->getPropertyValue($entity));
}

public function testGetPropertyValueFromRemovedProperty()
{
$entity = new Entity('foobar');
$metadata = new PropertyMetadata(self::CLASSNAME, 'internal');
$metadata->name = 'test';

$this->setExpectedException('Symfony\Component\Validator\Exception\ValidatorException');
$metadata->getPropertyValue($entity);
}
}

0 comments on commit 705a4f7

Please sign in to comment.