Skip to content

Commit 6566cd9

Browse files
committed
Fixed the error handling when decoding invalid XML to avoid a Warning
1 parent b7bcde9 commit 6566cd9

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Encoder/XmlEncoder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public function decode($data, $format, array $context = array())
7676
libxml_use_internal_errors($internalErrors);
7777
libxml_disable_entity_loader($disableEntities);
7878

79+
if ($error = libxml_get_last_error()) {
80+
throw new UnexpectedValueException($error->message);
81+
}
82+
7983
foreach ($dom->childNodes as $child) {
8084
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
8185
throw new UnexpectedValueException('Document types are not allowed.');

Tests/Encoder/XmlEncoderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
class XmlEncoderTest extends \PHPUnit_Framework_TestCase
2222
{
23+
private $encoder;
24+
2325
protected function setUp()
2426
{
2527
$this->encoder = new XmlEncoder;
@@ -301,6 +303,14 @@ public function testDecodeWithoutItemHash()
301303
$this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
302304
}
303305

306+
/**
307+
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
308+
*/
309+
public function testDecodeInvalidXml()
310+
{
311+
$this->encoder->decode('<?xml version="1.0"?><invalid><xml>', 'xml');
312+
}
313+
304314
public function testPreventsComplexExternalEntities()
305315
{
306316
$oldCwd = getcwd();

0 commit comments

Comments
 (0)