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

Commit 5c0774f

Browse files
committed
Continued refactoring of Cache consumers
- Mostly testing - Discovered some anomalies in PhpSerializer - Discovered some issues with reference passing in Filesystem cache adapter - Something is wrong with how the CLDR is using caching, which is affecting all Locale-based classes and their tests. - Tags are NOT WORKING in the Filesystem adapter; cannot find items by tags. This is affecting the Paginator tests in particular, and potentially may be the problem with the CLDR usage as well.
18 parents c7c26aa + cc2a971 + a58a318 + 56aa212 + deb83ca + 8c6325a + 06fbdf6 + ec2d4d3 + e3c3d67 + 074157d + 25f5da9 + d867a37 + e2b421c + 995a627 + 5a1a1aa + cf45d64 + 5fe14a9 + 09251bb commit 5c0774f

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

src/Adapter/PhpSerialize.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class PhpSerialize extends AbstractAdapter
4040
/**
4141
* @var null|string Serialized boolean false value
4242
*/
43-
private static $_serializedFalse = null;
43+
private static $serializedFalse = null;
4444

4545
/**
4646
* Constructor
@@ -54,8 +54,8 @@ public function __construct($opts = array())
5454

5555
// needed to check if a returned false is based on a serialize false
5656
// or based on failure (igbinary can overwrite [un]serialize functions)
57-
if (self::$_serializedFalse === null) {
58-
self::$_serializedFalse = serialize(false);
57+
if (self::$serializedFalse === null) {
58+
self::$serializedFalse = serialize(false);
5959
}
6060
}
6161

@@ -69,11 +69,17 @@ public function __construct($opts = array())
6969
*/
7070
public function serialize($value, array $opts = array())
7171
{
72+
set_error_handler(function($errno, $errstr = '', $errfile = '', $errline = '') {
73+
$message = sprintf(
74+
'Error with serialize operation in %s:%d: %s',
75+
$errfile,
76+
$errline,
77+
$errstr
78+
);
79+
throw new RuntimeException($message, $errno);
80+
});
7281
$ret = serialize($value);
73-
if ($ret === false) {
74-
$lastErr = error_get_last();
75-
throw new RuntimeException($lastErr['message']);
76-
}
82+
restore_error_handler();
7783
return $ret;
7884
}
7985

@@ -88,11 +94,37 @@ public function serialize($value, array $opts = array())
8894
*/
8995
public function unserialize($serialized, array $opts = array())
9096
{
91-
$ret = @unserialize($serialized);
92-
if ($ret === false && $serialized !== self::$_serializedFalse) {
93-
$lastErr = error_get_last();
94-
throw new RuntimeException($lastErr['message']);
97+
if (!is_string($serialized)) {
98+
// Must already be unserialized!
99+
return $serialized;
100+
throw new RuntimeException(sprintf(
101+
'%s expects a serialized string argument; received "%s"',
102+
__METHOD__,
103+
(is_object($serialized) ? get_class($serialized) : gettype($serialized))
104+
));
105+
}
106+
if (!preg_match('/^((s|i|d|b|a|O|C):|N;)/', $serialized)) {
107+
return $serialized;
95108
}
109+
110+
// If we have a serialized boolean false value, just return false;
111+
// prevents the unserialize handler from creating an error.
112+
if ($serialized === self::$serializedFalse) {
113+
return false;
114+
}
115+
116+
set_error_handler(function($errno, $errstr = '', $errfile = '', $errline = '') use ($serialized) {
117+
$message = sprintf(
118+
'Error with unserialize operation in %s:%d: %s; (string: "%s")',
119+
$errfile,
120+
$errline,
121+
$errstr,
122+
$serialized
123+
);
124+
throw new RuntimeException($message, $errno);
125+
}, E_NOTICE);
126+
$ret = unserialize($serialized);
127+
restore_error_handler();
96128
return $ret;
97129
}
98130
}

test/Adapter/PhpSerializeTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,16 @@ public function testUnserializeObject()
139139
$this->assertEquals($expected, $data);
140140
}
141141

142-
public function testUnserialzeInvalid()
142+
public function testUnserializingNonserializedStringReturnsItVerbatim()
143143
{
144144
$value = 'not a serialized string';
145-
$this->setExpectedException('Zend\Serializer\Exception\RuntimeException', 'unserialize(): Error at offset 0 of 23 bytes');
146-
$this->_adapter->unserialize($value);
145+
$this->assertEquals($value, $this->_adapter->unserialize($value));
147146
}
148147

148+
public function testUnserializingInvalidStringRaisesException()
149+
{
150+
$value = 'a:foobar';
151+
$this->setExpectedException('Zend\Serializer\Exception\RuntimeException');
152+
$this->_adapter->unserialize($value);
153+
}
149154
}

0 commit comments

Comments
 (0)