Skip to content

Commit d70efb6

Browse files
authored
PHPC-1700: Fix memory leak when read preference is invalid (#1179)
* PHPC-1700: Fix memory leak in prep_tagsets * PHPC-1701: Add regression test for memory leak in prep_authmechanisms
1 parent b581f2a commit d70efb6

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/MongoDB/ReadPreference.c

+5
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ static bool php_phongo_readpreference_init_from_hash(php_phongo_readpreference_t
6363
}
6464

6565
if ((tagSets = zend_hash_str_find(props, "tags", sizeof("tags") - 1))) {
66+
ZVAL_DEREF(tagSets);
6667
if (Z_TYPE_P(tagSets) == IS_ARRAY) {
6768
bson_t* tags = bson_new();
6869

70+
/* Separate tagSets as php_phongo_read_preference_prep_tagsets may
71+
* modify these tags. */
72+
SEPARATE_ZVAL_NOREF(tagSets);
73+
6974
php_phongo_read_preference_prep_tagsets(tagSets);
7075
php_phongo_zval_to_bson(tagSets, PHONGO_BSON_NONE, (bson_t*) tags, NULL);
7176

tests/manager/bug1701-001.phpt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
PHPC-1701: prep_authmechanismproperties may leak if Manager ctor errors
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . "/../utils/basic.inc";
7+
8+
echo throws(function () {
9+
// Using a stream context without SSL options causes an exception in the constructor, triggering the potential leak
10+
new MongoDB\Driver\Manager(
11+
null,
12+
['username' => 'username', 'authMechanism' => 'GSSAPI', 'authMechanismProperties' => ['canonicalize_host_name' => true]],
13+
['context' => stream_context_create([])]
14+
);
15+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECT--
21+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
22+
Stream-Context resource does not contain "ssl" options array
23+
===DONE===

tests/readPreference/bug1698-001.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ array(2) {
3939
["tags"]=>
4040
array(1) {
4141
[0]=>
42-
object(stdClass)#%d (1) {
42+
array(1) {
4343
["dc"]=>
4444
string(2) "ny"
4545
}

0 commit comments

Comments
 (0)