Unverified Commit d2159ea6 authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #3254 from seferov/array-type-convert

Throw ConversionException when unserialization fail for array and object types
parents 319e20f8 ceb26168
......@@ -21,7 +21,9 @@ namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use function is_resource;
use function restore_error_handler;
use function serialize;
use function set_error_handler;
use function stream_get_contents;
use function unserialize;
......@@ -59,12 +61,16 @@ class ArrayType extends Type
}
$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
$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;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use function is_resource;
use function restore_error_handler;
use function serialize;
use function set_error_handler;
use function stream_get_contents;
use function unserialize;
......@@ -58,12 +60,16 @@ class ObjectType extends Type
}
$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;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
use const E_ALL;
use const E_STRICT;
use function error_reporting;
use function serialize;
class ArrayTest extends \Doctrine\Tests\DbalTestCase
......@@ -28,12 +25,6 @@ class ArrayTest extends \Doctrine\Tests\DbalTestCase
$this->_type = Type::getType('array');
}
protected function tearDown()
{
error_reporting(-1); // reactive all error levels
}
public function testArrayConvertsToDatabaseValue()
{
self::assertInternalType(
......@@ -52,8 +43,8 @@ class ArrayTest extends \Doctrine\Tests\DbalTestCase
public function testConversionFailure()
{
error_reporting( (E_ALL | E_STRICT) - \E_NOTICE );
$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);
}
......
......@@ -4,9 +4,6 @@ namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
use const E_ALL;
use const E_STRICT;
use function error_reporting;
use function serialize;
class ObjectTest extends \Doctrine\Tests\DbalTestCase
......@@ -27,11 +24,6 @@ class ObjectTest extends \Doctrine\Tests\DbalTestCase
$this->_type = Type::getType('object');
}
protected function tearDown()
{
error_reporting(-1); // reactive all error levels
}
public function testObjectConvertsToDatabaseValue()
{
self::assertInternalType('string', $this->_type->convertToDatabaseValue(new \stdClass(), $this->_platform));
......@@ -44,8 +36,8 @@ class ObjectTest extends \Doctrine\Tests\DbalTestCase
public function testConversionFailure()
{
error_reporting( (E_ALL | E_STRICT) - \E_NOTICE );
$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);
}
......
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