Skip to content

Commit

Permalink
Fixed the error handling when decoding invalid XML to avoid a Warning
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Oct 28, 2013
1 parent 6fcb060 commit b2550b9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Expand Up @@ -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.');
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
Expand Up @@ -20,6 +20,8 @@

class XmlEncoderTest extends \PHPUnit_Framework_TestCase
{
private $encoder;

protected function setUp()
{
$this->encoder = new XmlEncoder;
Expand Down Expand Up @@ -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 version="1.0"?><invalid><xml>', 'xml');
}

public function testPreventsComplexExternalEntities()
{
$oldCwd = getcwd();
Expand Down

0 comments on commit b2550b9

Please sign in to comment.