Skip to content

Commit 9fc51c8

Browse files
authored
PHPC-1698: fix prep_tagsets leak for ReadPreference::__set_state (#1167)
1 parent 501d517 commit 9fc51c8

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

src/MongoDB/ReadPreference.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,10 @@ static PHP_METHOD(ReadPreference, __set_state)
311311
HashTable* props;
312312
zval* array;
313313

314-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
314+
/* Separate the zval, since we may end up modifying the "tags" element in
315+
* php_phongo_read_preference_prep_tagsets(), which is called from
316+
* php_phongo_readpreference_init_from_hash. */
317+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a/", &array) == FAILURE) {
315318
RETURN_FALSE;
316319
}
317320

tests/readPreference/bug1698-001.phpt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
--TEST--
2+
PHPC-1698: php_phongo_read_preference_prep_tagsets may leak in convert_to_object
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
$args = ['mode' => 'secondary', 'tags' => [['dc' => 'ny']]];
9+
var_dump(MongoDB\Driver\ReadPreference::__set_state($args));
10+
var_dump($args);
11+
12+
$tagSets = [['dc' => 'ny']];
13+
var_dump(new MongoDB\Driver\ReadPreference('secondary', $tagSets));
14+
var_dump($tagSets);
15+
16+
$uriTagSets = [['dc' => 'ny']];
17+
var_dump((new MongoDB\Driver\Manager(null, ['readPreference' => 'secondary', 'readPreferenceTags' => $uriTagSets]))->getReadPreference());
18+
var_dump($uriTagSets);
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECTF--
24+
object(MongoDB\Driver\ReadPreference)#%d (2) {
25+
["mode"]=>
26+
string(9) "secondary"
27+
["tags"]=>
28+
array(1) {
29+
[0]=>
30+
object(stdClass)#%d (1) {
31+
["dc"]=>
32+
string(2) "ny"
33+
}
34+
}
35+
}
36+
array(2) {
37+
["mode"]=>
38+
string(9) "secondary"
39+
["tags"]=>
40+
array(1) {
41+
[0]=>
42+
object(stdClass)#%d (1) {
43+
["dc"]=>
44+
string(2) "ny"
45+
}
46+
}
47+
}
48+
object(MongoDB\Driver\ReadPreference)#%d (2) {
49+
["mode"]=>
50+
string(9) "secondary"
51+
["tags"]=>
52+
array(1) {
53+
[0]=>
54+
object(stdClass)#%d (1) {
55+
["dc"]=>
56+
string(2) "ny"
57+
}
58+
}
59+
}
60+
array(1) {
61+
[0]=>
62+
array(1) {
63+
["dc"]=>
64+
string(2) "ny"
65+
}
66+
}
67+
object(MongoDB\Driver\ReadPreference)#%d (2) {
68+
["mode"]=>
69+
string(9) "secondary"
70+
["tags"]=>
71+
array(1) {
72+
[0]=>
73+
object(stdClass)#%d (1) {
74+
["dc"]=>
75+
string(2) "ny"
76+
}
77+
}
78+
}
79+
array(1) {
80+
[0]=>
81+
array(1) {
82+
["dc"]=>
83+
string(2) "ny"
84+
}
85+
}
86+
===DONE===

0 commit comments

Comments
 (0)