Skip to content

Commit a8054b9

Browse files
romainneutronfabpot
authored andcommitted
Fix libxml_use_internal_errors and libxml_disable_entity_loader usage
1 parent 06b6ad0 commit a8054b9

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Encoder/XmlEncoder.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public function encode($data, $format, array $context = array())
6666
*/
6767
public function decode($data, $format, array $context = array())
6868
{
69+
if ('' === trim($data)) {
70+
throw new UnexpectedValueException('Invalid XML data, it can not be empty.');
71+
}
72+
6973
$internalErrors = libxml_use_internal_errors(true);
7074
$disableEntities = libxml_disable_entity_loader(true);
7175
libxml_clear_errors();
@@ -77,6 +81,8 @@ public function decode($data, $format, array $context = array())
7781
libxml_disable_entity_loader($disableEntities);
7882

7983
if ($error = libxml_get_last_error()) {
84+
libxml_clear_errors();
85+
8086
throw new UnexpectedValueException($error->message);
8187
}
8288

Tests/Encoder/XmlEncoderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ public function testPreventsComplexExternalEntities()
330330
}
331331
}
332332

333+
public function testDecodeEmptyXml()
334+
{
335+
$this->setExpectedException('Symfony\Component\Serializer\Exception\UnexpectedValueException', 'Invalid XML data, it can not be empty.');
336+
$this->encoder->decode(' ', 'xml');
337+
}
338+
333339
protected function getXmlSource()
334340
{
335341
return '<?xml version="1.0"?>'."\n".

0 commit comments

Comments
 (0)