Skip to content

Commit 172f29b

Browse files
author
Andrei Zmievski
committed
Fix bug #16084.
1 parent 7bc729d commit 172f29b

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

php_memcached.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,14 +1395,32 @@ PHP_METHOD(Memcached, addServers)
13951395
entry_size = zend_hash_num_elements(Z_ARRVAL_PP(entry));
13961396

13971397
if (entry_size > 1) {
1398-
zend_hash_index_find(Z_ARRVAL_PP(entry), 0, (void **)&z_host);
1399-
zend_hash_index_find(Z_ARRVAL_PP(entry), 1, (void **)&z_port);
1398+
zend_hash_internal_pointer_reset(Z_ARRVAL_PP(entry));
1399+
1400+
/* Check that we have a host */
1401+
if (zend_hash_get_current_data(Z_ARRVAL_PP(entry), (void **)&z_host) == FAILURE) {
1402+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not get server host for entry #%d", i+1);
1403+
continue;
1404+
}
1405+
1406+
/* Check that we have a port */
1407+
if (zend_hash_move_forward(Z_ARRVAL_PP(entry)) == FAILURE ||
1408+
zend_hash_get_current_data(Z_ARRVAL_PP(entry), (void **)&z_port) == FAILURE) {
1409+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not get server port for entry #%d", i+1);
1410+
continue;
1411+
}
1412+
14001413
convert_to_string_ex(z_host);
14011414
convert_to_long_ex(z_port);
14021415

14031416
weight = 0;
14041417
if (entry_size > 2) {
1405-
zend_hash_index_find(Z_ARRVAL_PP(entry), 2, (void **)&z_weight);
1418+
/* Try to get weight */
1419+
if (zend_hash_move_forward(Z_ARRVAL_PP(entry)) == FAILURE ||
1420+
zend_hash_get_current_data(Z_ARRVAL_PP(entry), (void **)&z_weight) == FAILURE) {
1421+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not get server weight for entry #%d", i+1);
1422+
}
1423+
14061424
convert_to_long_ex(z_weight);
14071425
weight = Z_LVAL_PP(z_weight);
14081426
}
@@ -1568,7 +1586,6 @@ PHP_METHOD(Memcached, getStats)
15681586
}
15691587
/* }}} */
15701588

1571-
15721589
/* {{{ Memcached::getVersion()
15731590
Returns the version of each memcached server in the pool */
15741591
PHP_METHOD(Memcached, getVersion)

tests/bug_16084.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Memcached: Bug #16084 (Crash when addServers is called with an associative array)
3+
--SKIPIF--
4+
<?php if (!extension_loaded("memcached")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$servers = array ( 0 => array ( 'KEYHERE' => 'localhost', 11211, 3 ), );
8+
$m = new memcached();
9+
$m->addServers($servers);
10+
var_dump($m->getServerList());
11+
?>
12+
--EXPECT--
13+
array(1) {
14+
[0]=>
15+
array(3) {
16+
["host"]=>
17+
string(9) "localhost"
18+
["port"]=>
19+
int(11211)
20+
["weight"]=>
21+
int(3)
22+
}
23+
}

0 commit comments

Comments
 (0)