From b2550b90ae3be56a45bf9daab39926a8be782050 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Mon, 28 Oct 2013 13:20:44 +0100 Subject: [PATCH] Fixed the error handling when decoding invalid XML to avoid a Warning --- .../Component/Serializer/Encoder/XmlEncoder.php | 4 ++++ .../Serializer/Tests/Encoder/XmlEncoderTest.php | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index 7923a75a1f3c..872949edda27 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -76,6 +76,10 @@ public function decode($data, $format, array $context = array()) libxml_use_internal_errors($internalErrors); libxml_disable_entity_loader($disableEntities); + if ($error = libxml_get_last_error()) { + throw new UnexpectedValueException($error->message); + } + foreach ($dom->childNodes as $child) { if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) { throw new UnexpectedValueException('Document types are not allowed.'); diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php index b23d4c793909..07d8bd60d989 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php @@ -20,6 +20,8 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase { + private $encoder; + protected function setUp() { $this->encoder = new XmlEncoder; @@ -301,6 +303,14 @@ public function testDecodeWithoutItemHash() $this->assertEquals($expected, $this->encoder->decode($xml, 'xml')); } + /** + * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException + */ + public function testDecodeInvalidXml() + { + $this->encoder->decode('', 'xml'); + } + public function testPreventsComplexExternalEntities() { $oldCwd = getcwd();