Commit ceb26168 authored by Farhad Safarov's avatar Farhad Safarov

Throw ConversionException when unserialization fail for array and object types

cs fix

object type

conversionFailedUnserialization

type hinting & finally restore error handler

cs: use function

type hint

sort use alphabetically

code quality improvements
parent 319e20f8
...@@ -21,7 +21,9 @@ namespace Doctrine\DBAL\Types; ...@@ -21,7 +21,9 @@ namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use function is_resource; use function is_resource;
use function restore_error_handler;
use function serialize; use function serialize;
use function set_error_handler;
use function stream_get_contents; use function stream_get_contents;
use function unserialize; use function unserialize;
...@@ -59,12 +61,16 @@ class ArrayType extends Type ...@@ -59,12 +61,16 @@ class ArrayType extends Type
} }
$value = (is_resource($value)) ? stream_get_contents($value) : $value; $value = (is_resource($value)) ? stream_get_contents($value) : $value;
$val = unserialize($value);
if ($val === false && $value != 'b:0;') {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val; set_error_handler(function (int $code, string $message) : void {
throw ConversionException::conversionFailedUnserialization($this->getName(), $message);
});
try {
return unserialize($value);
} finally {
restore_error_handler();
}
} }
/** /**
......
...@@ -120,4 +120,13 @@ class ConversionException extends \Doctrine\DBAL\DBALException ...@@ -120,4 +120,13 @@ class ConversionException extends \Doctrine\DBAL\DBALException
$error $error
)); ));
} }
public static function conversionFailedUnserialization(string $format, string $error) : self
{
return new self(sprintf(
"Could not convert database value to '%s' as an error was triggered by the unserialization: '%s'",
$format,
$error
));
}
} }
...@@ -21,7 +21,9 @@ namespace Doctrine\DBAL\Types; ...@@ -21,7 +21,9 @@ namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use function is_resource; use function is_resource;
use function restore_error_handler;
use function serialize; use function serialize;
use function set_error_handler;
use function stream_get_contents; use function stream_get_contents;
use function unserialize; use function unserialize;
...@@ -58,12 +60,16 @@ class ObjectType extends Type ...@@ -58,12 +60,16 @@ class ObjectType extends Type
} }
$value = (is_resource($value)) ? stream_get_contents($value) : $value; $value = (is_resource($value)) ? stream_get_contents($value) : $value;
$val = unserialize($value);
if ($val === false && $value !== 'b:0;') {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val; set_error_handler(function (int $code, string $message) : void {
throw ConversionException::conversionFailedUnserialization($this->getName(), $message);
});
try {
return unserialize($value);
} finally {
restore_error_handler();
}
} }
/** /**
......
...@@ -5,9 +5,6 @@ namespace Doctrine\Tests\DBAL\Types; ...@@ -5,9 +5,6 @@ namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform; use Doctrine\Tests\DBAL\Mocks\MockPlatform;
use const E_ALL;
use const E_STRICT;
use function error_reporting;
use function serialize; use function serialize;
class ArrayTest extends \Doctrine\Tests\DbalTestCase class ArrayTest extends \Doctrine\Tests\DbalTestCase
...@@ -28,12 +25,6 @@ class ArrayTest extends \Doctrine\Tests\DbalTestCase ...@@ -28,12 +25,6 @@ class ArrayTest extends \Doctrine\Tests\DbalTestCase
$this->_type = Type::getType('array'); $this->_type = Type::getType('array');
} }
protected function tearDown()
{
error_reporting(-1); // reactive all error levels
}
public function testArrayConvertsToDatabaseValue() public function testArrayConvertsToDatabaseValue()
{ {
self::assertInternalType( self::assertInternalType(
...@@ -52,8 +43,8 @@ class ArrayTest extends \Doctrine\Tests\DbalTestCase ...@@ -52,8 +43,8 @@ class ArrayTest extends \Doctrine\Tests\DbalTestCase
public function testConversionFailure() public function testConversionFailure()
{ {
error_reporting( (E_ALL | E_STRICT) - \E_NOTICE );
$this->expectException('Doctrine\DBAL\Types\ConversionException'); $this->expectException('Doctrine\DBAL\Types\ConversionException');
$this->expectExceptionMessage("Could not convert database value to 'array' as an error was triggered by the unserialization: 'unserialize(): Error at offset 0 of 7 bytes'");
$this->_type->convertToPHPValue('abcdefg', $this->_platform); $this->_type->convertToPHPValue('abcdefg', $this->_platform);
} }
......
...@@ -4,9 +4,6 @@ namespace Doctrine\Tests\DBAL\Types; ...@@ -4,9 +4,6 @@ namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform; use Doctrine\Tests\DBAL\Mocks\MockPlatform;
use const E_ALL;
use const E_STRICT;
use function error_reporting;
use function serialize; use function serialize;
class ObjectTest extends \Doctrine\Tests\DbalTestCase class ObjectTest extends \Doctrine\Tests\DbalTestCase
...@@ -27,11 +24,6 @@ class ObjectTest extends \Doctrine\Tests\DbalTestCase ...@@ -27,11 +24,6 @@ class ObjectTest extends \Doctrine\Tests\DbalTestCase
$this->_type = Type::getType('object'); $this->_type = Type::getType('object');
} }
protected function tearDown()
{
error_reporting(-1); // reactive all error levels
}
public function testObjectConvertsToDatabaseValue() public function testObjectConvertsToDatabaseValue()
{ {
self::assertInternalType('string', $this->_type->convertToDatabaseValue(new \stdClass(), $this->_platform)); self::assertInternalType('string', $this->_type->convertToDatabaseValue(new \stdClass(), $this->_platform));
...@@ -44,8 +36,8 @@ class ObjectTest extends \Doctrine\Tests\DbalTestCase ...@@ -44,8 +36,8 @@ class ObjectTest extends \Doctrine\Tests\DbalTestCase
public function testConversionFailure() public function testConversionFailure()
{ {
error_reporting( (E_ALL | E_STRICT) - \E_NOTICE );
$this->expectException('Doctrine\DBAL\Types\ConversionException'); $this->expectException('Doctrine\DBAL\Types\ConversionException');
$this->expectExceptionMessage("Could not convert database value to 'object' as an error was triggered by the unserialization: 'unserialize(): Error at offset 0 of 7 bytes'");
$this->_type->convertToPHPValue('abcdefg', $this->_platform); $this->_type->convertToPHPValue('abcdefg', $this->_platform);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment