Prevent infinite loop in PropertyMetadata

This commit is contained in:
Wesley Lancel 2016-10-04 09:14:18 +02:00 committed by Fabien Potencier
parent ab6f181cb4
commit c1ae7b6ad7
2 changed files with 16 additions and 0 deletions

View File

@ -58,8 +58,14 @@ class PropertyMetadata extends MemberMetadata
*/
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());

View File

@ -42,4 +42,14 @@ class PropertyMetadataTest extends \PHPUnit_Framework_TestCase
$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);
}
}