From a95ca62755c2465fa26186a8dec20520188c1589 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 18 Nov 2020 18:00:59 +0100 Subject: [PATCH 1/2] PHPC-1700: Fix memory leak in prep_tagsets --- src/MongoDB/ReadPreference.c | 5 +++++ tests/readPreference/bug1698-001.phpt | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/MongoDB/ReadPreference.c b/src/MongoDB/ReadPreference.c index 3c0996661..c23f9c21f 100644 --- a/src/MongoDB/ReadPreference.c +++ b/src/MongoDB/ReadPreference.c @@ -63,9 +63,14 @@ static bool php_phongo_readpreference_init_from_hash(php_phongo_readpreference_t } if ((tagSets = zend_hash_str_find(props, "tags", sizeof("tags") - 1))) { + ZVAL_DEREF(tagSets); if (Z_TYPE_P(tagSets) == IS_ARRAY) { bson_t* tags = bson_new(); + /* Separate tagSets as php_phongo_read_preference_prep_tagsets may + * modify these tags. */ + SEPARATE_ZVAL_NOREF(tagSets); + php_phongo_read_preference_prep_tagsets(tagSets); php_phongo_zval_to_bson(tagSets, PHONGO_BSON_NONE, (bson_t*) tags, NULL); diff --git a/tests/readPreference/bug1698-001.phpt b/tests/readPreference/bug1698-001.phpt index e256a6e35..46e5da309 100644 --- a/tests/readPreference/bug1698-001.phpt +++ b/tests/readPreference/bug1698-001.phpt @@ -39,7 +39,7 @@ array(2) { ["tags"]=> array(1) { [0]=> - object(stdClass)#%d (1) { + array(1) { ["dc"]=> string(2) "ny" } From fa6d39b3edd6bf85b6950a6820bcdf974c678d86 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 19 Nov 2020 11:24:11 +0100 Subject: [PATCH 2/2] PHPC-1701: Add regression test for memory leak in prep_authmechanisms --- tests/manager/bug1701-001.phpt | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/manager/bug1701-001.phpt diff --git a/tests/manager/bug1701-001.phpt b/tests/manager/bug1701-001.phpt new file mode 100644 index 000000000..a7e147acb --- /dev/null +++ b/tests/manager/bug1701-001.phpt @@ -0,0 +1,23 @@ +--TEST-- +PHPC-1701: prep_authmechanismproperties may leak if Manager ctor errors +--FILE-- + 'username', 'authMechanism' => 'GSSAPI', 'authMechanismProperties' => ['canonicalize_host_name' => true]], + ['context' => stream_context_create([])] + ); +}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n"; + +?> +===DONE=== + +--EXPECT-- +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Stream-Context resource does not contain "ssl" options array +===DONE===