Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 941f9e8

Browse files
committed
[Cache] Prevent notice on case matching metadata trick
On saving an array of one element with a int key of strlen 10 it matches the first conditions of the trick used to save metadata. > Notice: Trying to access array offset on value of type int Casting it to string fixes it.
1 parent 8a00352 commit 941f9e8

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static function ($key, $value, $isHit) {
5151
// Detect wrapped values that encode for their expiry and creation duration
5252
// For compactness, these values are packed in the key of an array using
5353
// magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F
54-
if (\is_array($v) && 1 === \count($v) && 10 === \strlen($k = key($v)) && "\x9D" === $k[0] && "\0" === $k[5] && "\x5F" === $k[9]) {
54+
if (\is_array($v) && 1 === \count($v) && 10 === \strlen($k = (string) key($v)) && "\x9D" === $k[0] && "\0" === $k[5] && "\x5F" === $k[9]) {
5555
$item->value = $v[$k];
5656
$v = unpack('Ve/Nc', substr($k, 1, -1));
5757
$item->metadata[CacheItem::METADATA_EXPIRY] = $v['e'] + CacheItem::METADATA_EXPIRY_OFFSET;

src/Symfony/Component/Cache/Adapter/ProxyAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static function ($key, $innerItem) use ($poolHash) {
5959
// Detect wrapped values that encode for their expiry and creation duration
6060
// For compactness, these values are packed in the key of an array using
6161
// magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F
62-
if (\is_array($v) && 1 === \count($v) && 10 === \strlen($k = key($v)) && "\x9D" === $k[0] && "\0" === $k[5] && "\x5F" === $k[9]) {
62+
if (\is_array($v) && 1 === \count($v) && 10 === \strlen($k = (string) key($v)) && "\x9D" === $k[0] && "\0" === $k[5] && "\x5F" === $k[9]) {
6363
$item->value = $v[$k];
6464
$v = unpack('Ve/Nc', substr($k, 1, -1));
6565
$item->metadata[CacheItem::METADATA_EXPIRY] = $v['e'] + CacheItem::METADATA_EXPIRY_OFFSET;

src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,30 @@ public function testClearPrefix()
272272
$this->assertFalse($cache->hasItem('foobar'));
273273
$this->assertTrue($cache->hasItem('barfoo'));
274274
}
275+
276+
public function testWeirdDataMatchingMetadataWrappedValues()
277+
{
278+
if (isset($this->skippedTests[__FUNCTION__])) {
279+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
280+
}
281+
282+
$cache = $this->createCachePool(0, __FUNCTION__);
283+
$cache->clear();
284+
285+
$item = $cache->getItem('foobar');
286+
287+
// it should be an array containing only one element
288+
// with key having a strlen of 10.
289+
$weirdDataMatchingMedatataWrappedValue = [
290+
1234567890 => [
291+
1
292+
]
293+
];
294+
295+
$cache->save($item->set($weirdDataMatchingMedatataWrappedValue));
296+
297+
$this->assertTrue($cache->hasItem('foobar'));
298+
}
275299
}
276300

277301
class NotUnserializable

0 commit comments

Comments
 (0)