Skip to content

Commit 3b06e81

Browse files
committed
PHPC-572: Keep stream context options alive for Manager's lifetime
1 parent 2c4ed2a commit 3b06e81

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

php_phongo.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,17 @@ bool phongo_manager_init(php_phongo_manager_t *manager, const char *uri_string,
20432043
return false;
20442044
}
20452045

2046+
/* Keep a reference to driverOptions, since it may be referenced later for
2047+
* lazy stream initialization. */
2048+
if (driverOptions) {
2049+
#if PHP_VERSION_ID >= 70000
2050+
ZVAL_COPY(&manager->driverOptions, driverOptions);
2051+
#else
2052+
Z_ADDREF_P(driverOptions);
2053+
manager->driverOptions = driverOptions;
2054+
#endif
2055+
}
2056+
20462057
return true;
20472058
} /* }}} */
20482059

php_phongo_structs-5.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef struct {
5656
zend_object std;
5757
mongoc_client_t *client;
5858
char *pem_file;
59+
zval *driverOptions;
5960
} php_phongo_manager_t;
6061

6162
typedef struct {

php_phongo_structs-7.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct {
5555
typedef struct {
5656
mongoc_client_t *client;
5757
char *pem_file;
58+
zval driverOptions;
5859
zend_object std;
5960
} php_phongo_manager_t;
6061

src/MongoDB/Manager.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,14 @@ static void php_phongo_manager_free_object(phongo_free_object_arg *object TSRMLS
381381
efree(intern->pem_file);
382382
}
383383

384+
#if PHP_VERSION_ID >= 70000
385+
zval_ptr_dtor(&intern->driverOptions);
386+
#else
387+
if (intern->driverOptions) {
388+
zval_ptr_dtor(&intern->driverOptions);
389+
}
390+
#endif
391+
384392
#if PHP_VERSION_ID < 70000
385393
efree(intern);
386394
#endif

tests/manager/bug0572.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
PHPC-572: Ensure stream context does not go out of scope before socket init
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("STANDALONE_SSL"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$closure = function() {
10+
$context = stream_context_create([
11+
'ssl' => [
12+
'verify_peer' => false,
13+
'verify_peer_name' => false,
14+
'allow_self_signed' => true,
15+
],
16+
]);
17+
return new MongoDB\Driver\Manager(STANDALONE_SSL, ['ssl' => true], ['context' => $context]);
18+
};
19+
20+
$manager = $closure();
21+
$cursor = $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(['ping' => 1]));
22+
var_dump($cursor->toArray()[0]);
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECTF--
28+
object(stdClass)#%d (%d) {
29+
["ok"]=>
30+
float(1)
31+
}
32+
===DONE===

0 commit comments

Comments
 (0)