Skip to content

Commit

Permalink
Fix PECL bug 16959, binary protocol and bad multiget.
Browse files Browse the repository at this point in the history
libmemcached doesn't null terminate keys when in binary
mode. This fix always sets the null. It is safe because
the max key length is 250 and libmemcached reserves extra
byte for the terminating null.
  • Loading branch information
Teddy Grenman committed Dec 13, 2009
1 parent 640927e commit 6abbe4b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions php_memcached.c
Expand Up @@ -664,6 +664,10 @@ static void php_memc_getMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_ke
res_key = memcached_result_key_value(&result);
res_key_len = memcached_result_key_length(&result);

// This may be a bug in libmemcached, the key is not null terminated
// whe using the binary protocol.
res_key[res_key_len] = 0;

MAKE_STD_ZVAL(value);

if (php_memc_zval_from_payload(value, payload, payload_len, flags, m_obj->serializer TSRMLS_CC) < 0) {
Expand Down
55 changes: 55 additions & 0 deletions tests/bug_16959.phpt
@@ -0,0 +1,55 @@
--TEST--
Memcached: Bug #16959 (getMulti + BINARY_PROTOCOL problem)
--SKIPIF--
<?php if (!extension_loaded("memcached")) print "skip"; ?>
--FILE--
<?
$cache = new Memcached();
$cache->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$cache->addServer('localhost', 11211);

$cache->set('key_0', 'value0');
$cache->set('key_0_additional', 'value0_additional');

// -------------- NORMAL
echo "NORMAL\n";
$keys = array( 'key_0', 'key_0_additional' );
$values = $cache->getMulti($keys);
echo $cache->getResultMessage(), "\n";
echo "Values:\n";
foreach ($values as $k => $v) {
var_dump($k);
var_dump($v);
var_dump($values[$k]);
}
// --------------- REVERSED KEY ORDER
echo "REVERSED KEY ORDER\n";
$keys = array( 'key_0_additional', 'key_0' );
$values = $cache->getMulti($keys);
echo $cache->getResultMessage(), "\n";
echo "Values:\n";
foreach ($values as $k => $v) {
var_dump($k);
var_dump($v);
var_dump($values[$k]);
}

--EXPECT--
NORMAL
SUCCESS
Values:
string(5) "key_0"
string(6) "value0"
string(6) "value0"
string(16) "key_0_additional"
string(17) "value0_additional"
string(17) "value0_additional"
REVERSED KEY ORDER
SUCCESS
Values:
string(16) "key_0_additional"
string(17) "value0_additional"
string(17) "value0_additional"
string(5) "key_0"
string(6) "value0"
string(6) "value0"

0 comments on commit 6abbe4b

Please sign in to comment.