php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #59990 getMulti can return non-unique keys
Submitted: 2011-10-04 18:40 UTC Modified: 2012-04-01 22:45 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: nate dot whittaker at gmail dot com Assigned: andrei (profile)
Status: Closed Package: memcached (PECL)
PHP Version: Irrelevant OS: Mac OS X
Private report: No CVE-ID: None
 [2011-10-04 18:40 UTC] nate dot whittaker at gmail dot com
Description:
------------
When the binary protocol is enabled and when passing the 3rd 
parameter, Memcached::GET_PRESERVE_ORDER, to getMulti(), the 
resulting array contains non-unique keys.

In other words, each key is represented twice in the result. 
One is set to null and the other is set to the value of 
whatever was stored in memcached.

getResultMessage() shows "SUCCESS".

I'm actually on php 5.3.8

Reproduce code:
---------------
$MC = new Memcached;
$MC->setOption(Memcached::OPT_BINARY_PROTOCOL, true); // Removing this causes the code to work, but at the 'expense' of reverting to the ASCII protocol
$MC->addServer('localhost', 11211);

$KeyList = Array (
     'key1'
    ,'key2'
    ,'key3'
    ,'key4'
);

$Test = Array();
foreach ($KeyList as $k) {
    $Test[$k] = 'stored value for '.$k;
}

// This block uses the *Multi() functions
$MC->setMulti($Test, 3600);

sleep(1);
$null = null;
$theList = $MC->getMulti($KeyList, $null, Memcached::GET_PRESERVE_ORDER);

var_dump($theList);

Expected result:
----------------
var_dump should shows:

array
     'key1' => 'stored value for key1'
     'key2' => 'stored value for key2'
     'key3' => 'stored value for key3'
     'key4' => 'stored value for key4'

Actual result:
--------------
var_dump shows:

array
     'key1' => null
     'key2' => null
     'key3' => null
     'key4' => null
     'key1' => 'stored value for key1'
     'key2' => 'stored value for key2'
     'key3' => 'stored value for key3'
     'key4' => 'stored value for key4'

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-21 21:17 UTC] nate at frickenate dot com
Confirmed. Even without the Memcached::GET_PRESERVE_ORDER option, using 
getMulti() with binary protocol enabled results in some kind of parsing problem 
that winds up making some indexes returned in the array not equal to the actual 
key requested.

I know this sounds crazy, but if I get the key "helloworld" from memcached, 
doing a var_dump() of the result shows "helloworld" index in the array - 
however, array_key_exists("helloworld", $result) fails. Somehow the index as 
seen by the eye is not the same as the actual index stored by php.

It's a little messed up - php does not allow for multiple entries of the same 
index in an array, unless memcached package somehow bypasses php's protections. 
I would guess that there is a NULL (byte 0) character in the string somewhere, 
but this is not the case. If I dump the serialize() or json_encode() of the 
array, a NULL character is nowhere to be seen.

Thanks to the other Nate (strange coincidence) for bringing the binary protocol 
to light. I couldn't track down the cause of this behaviour myself, however 
disabling binary protocol in the client does indeed solve the problem.
 [2012-02-19 07:25 UTC] iamcraigcampbell at gmail dot com
I just experienced this exact bug and thought I was going crazy.  Didn't think it 
was possible for an array to have multiple entries with the same index.  Glad to 
see I'm not the only one.
 [2012-04-01 22:44 UTC] andrei@php.net
-Summary: getMulti can return non-unique keys +Summary: Add binary session support -PHP Version: 5.3.6 +PHP Version: Irrelevant
 [2012-04-01 22:45 UTC] andrei@php.net
-Summary: Add binary session support +Summary: getMulti can return non-unique keys -Status: Open +Status: Closed -Assigned To: +Assigned To: andrei
 [2012-04-01 22:45 UTC] andrei@php.net
Opened on Github as an issue, closing this here.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Sep 30 16:01:24 2020 UTC