Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 4dd7504

Browse files
committed
Merge branch 'hotfix/9'
Close #9
2 parents 971398f + b7024c9 commit 4dd7504

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#9](https://github.com/zendframework/zend-serializer/pull/9) fixes the
22+
behavior of the `PhpSerialize` adapter to raise an exception during
23+
deserialization if the value is not serialized, restoring behavior to match
24+
the other adapters.
2225

2326
## 2.7.1 - 2016-04-18
2427

src/Adapter/PhpSerialize.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,18 @@ public function serialize($value)
6464
*/
6565
public function unserialize($serialized)
6666
{
67-
if (!is_string($serialized) || !preg_match('/^((s|i|d|b|a|O|C):|N;)/', $serialized)) {
68-
return $serialized;
67+
if (! is_string($serialized) || ! preg_match('/^((s|i|d|b|a|O|C):|N;)/', $serialized)) {
68+
$value = $serialized;
69+
if (is_object($value)) {
70+
$value = get_class($value);
71+
} elseif (! is_string($value)) {
72+
$value = gettype($value);
73+
}
74+
75+
throw new Exception\RuntimeException(sprintf(
76+
'Serialized data must be a string containing serialized PHP code; received: %s',
77+
$value
78+
));
6979
}
7080

7181
// If we have a serialized boolean false value, just return false;

test/Adapter/PhpSerializeTest.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,47 @@ public function testUnserializeObject()
122122
$this->assertEquals($expected, $data);
123123
}
124124

125-
public function testUnserializingNonserializedStringReturnsItVerbatim()
125+
public function invalidSerializationTypes()
126126
{
127-
$value = 'not a serialized string';
128-
$this->assertEquals($value, $this->adapter->unserialize($value));
127+
return [
128+
'null' => [null, 'NULL'],
129+
'true' => [true, 'boolean'],
130+
'false' => [false, 'boolean'],
131+
'zero' => [0, 'int'],
132+
'int' => [1, 'int'],
133+
'zero-float' => [0.0, 'double'],
134+
'float' => [1.1, 'double'],
135+
'array' => [['foo'], 'array'],
136+
'object' => [(object) ['foo' => 'bar'], 'stdClass'],
137+
];
129138
}
130139

131-
public function testUnserializingInvalidStringRaisesException()
140+
/**
141+
* @dataProvider invalidSerializationTypes
142+
*/
143+
public function testUnserializingNoStringRaisesException($value, $expected)
132144
{
133-
$value = 'a:foobar';
134-
$this->setExpectedException('Zend\Serializer\Exception\RuntimeException');
145+
$this->setExpectedException(
146+
'Zend\Serializer\Exception\RuntimeException',
147+
$expected
148+
);
135149
$this->adapter->unserialize($value);
136150
}
151+
152+
public function invalidStrings()
153+
{
154+
return [
155+
'not-serialized' => ['foobar', 'foobar'],
156+
'invalid-serialization' => ['a:foobar', 'Unserialization failed'],
157+
];
158+
}
159+
160+
/**
161+
* @dataProvider invalidStrings
162+
*/
163+
public function testUnserializingInvalidStringRaisesException($string, $expected)
164+
{
165+
$this->setExpectedException(Serializer\Exception\RuntimeException::class, $expected);
166+
$this->adapter->unserialize($string);
167+
}
137168
}

0 commit comments

Comments
 (0)