Skip to content

Commit df285ce

Browse files
committed
Merge pull request #331
2 parents e06c96d + 857ab51 commit df285ce

File tree

6 files changed

+68
-28
lines changed

6 files changed

+68
-28
lines changed

phongo_compat.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,8 @@
4343
# define HASH_KEY_NON_EXISTENT HASH_KEY_NON_EXISTANT
4444
#endif
4545

46-
#if PHP_VERSION_ID < 50400
47-
# define object_properties_init(_std, _class_type) \
48-
zend_hash_copy(*_std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
49-
#endif
50-
5146
#if PHP_VERSION_ID >= 70000
5247
# define str_efree(s) efree((char*)s)
53-
#elif PHP_VERSION_ID < 50400
54-
# define str_efree(s) efree((char*)s)
5548
#else
5649
# include <Zend/zend_string.h>
5750
#endif
@@ -102,15 +95,6 @@
10295
# endif
10396
#endif
10497

105-
#if PHP_VERSION_ID < 50400
106-
# define GET_DEFAULT_CONTEXT() \
107-
ctx = FG(default_context) ? FG(default_context) : php_stream_context_alloc()
108-
#else
109-
# define GET_DEFAULT_CONTEXT() \
110-
ctx = FG(default_context) ? FG(default_context) : php_stream_context_alloc(TSRMLS_C)
111-
#endif
112-
113-
11498
#ifndef php_ignore_value
11599
# if defined(__GNUC__) && __GNUC__ >= 4
116100
# define php_ignore_value(x) (({ __typeof__ (x) __x = (x); (void) __x; }))

php_phongo.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,9 +1906,11 @@ static bool php_phongo_apply_wc_options_to_client(mongoc_client_t *client, bson_
19061906
static mongoc_client_t *php_phongo_make_mongo_client(php_phongo_manager_t *manager, const mongoc_uri_t *uri, zval *driverOptions TSRMLS_DC) /* {{{ */
19071907
{
19081908
#if PHP_VERSION_ID >= 70000
1909-
zval *tmp;
1909+
zval *zdebug = NULL;
1910+
zval *zcontext = NULL;
19101911
#else
1911-
zval **tmp;
1912+
zval **zdebug = NULL;
1913+
zval **zcontext = NULL;
19121914
#endif
19131915
php_stream_context *ctx = NULL;
19141916
const char *mech, *mongoc_version, *bson_version;
@@ -1917,30 +1919,31 @@ static mongoc_client_t *php_phongo_make_mongo_client(php_phongo_manager_t *manag
19171919
ENTRY;
19181920

19191921
#if PHP_VERSION_ID >= 70000
1920-
if (driverOptions && (tmp = zend_hash_str_find(Z_ARRVAL_P(driverOptions), "debug", sizeof("debug")-1)) != NULL) {
1922+
if (driverOptions && (zdebug = zend_hash_str_find(Z_ARRVAL_P(driverOptions), "debug", sizeof("debug")-1)) != NULL) {
19211923
zend_string *key = zend_string_init(PHONGO_DEBUG_INI, sizeof(PHONGO_DEBUG_INI)-1, 0);
1922-
zend_string *value_str = zval_get_string(tmp);
1924+
zend_string *value_str = zval_get_string(zdebug);
19231925
zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
19241926
zend_string_release(key);
19251927
zend_string_release(value_str);
19261928
}
19271929
#else
1928-
if (driverOptions && zend_hash_find(Z_ARRVAL_P(driverOptions), "debug", strlen("debug") + 1, (void**)&tmp) == SUCCESS) {
1929-
convert_to_string(*tmp);
1930+
if (driverOptions && zend_hash_find(Z_ARRVAL_P(driverOptions), "debug", strlen("debug") + 1, (void**)&zdebug) == SUCCESS) {
1931+
convert_to_string(*zdebug);
19301932

1931-
zend_alter_ini_entry_ex((char *)PHONGO_DEBUG_INI, sizeof(PHONGO_DEBUG_INI), Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
1933+
zend_alter_ini_entry_ex((char *)PHONGO_DEBUG_INI, sizeof(PHONGO_DEBUG_INI), Z_STRVAL_PP(zdebug), Z_STRLEN_PP(zdebug), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
19321934
}
19331935
#endif
19341936

19351937
#if PHP_VERSION_ID >= 70000
1936-
if (driverOptions && (tmp = zend_hash_str_find(Z_ARRVAL_P(driverOptions), "context", sizeof("context")-1)) != NULL) {
1937-
ctx = php_stream_context_from_zval(tmp, 0);
1938+
if (driverOptions && (zcontext = zend_hash_str_find(Z_ARRVAL_P(driverOptions), "context", sizeof("context")-1)) != NULL) {
1939+
ctx = php_stream_context_from_zval(zcontext, 0);
19381940
#else
1939-
if (driverOptions && zend_hash_find(Z_ARRVAL_P(driverOptions), "context", strlen("context") + 1, (void**)&tmp) == SUCCESS) {
1940-
ctx = php_stream_context_from_zval(*tmp, 0);
1941+
if (driverOptions && zend_hash_find(Z_ARRVAL_P(driverOptions), "context", strlen("context") + 1, (void**)&zcontext) == SUCCESS) {
1942+
ctx = php_stream_context_from_zval(*zcontext, 0);
19411943
#endif
19421944
} else {
1943-
GET_DEFAULT_CONTEXT();
1945+
zval *tmp = NULL; /* PHP 5.x requires an lvalue */
1946+
ctx = php_stream_context_from_zval(tmp, 0);
19441947
}
19451948

19461949
if (mongoc_uri_get_ssl(uri)) {
@@ -2041,6 +2044,17 @@ bool phongo_manager_init(php_phongo_manager_t *manager, const char *uri_string,
20412044
return false;
20422045
}
20432046

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

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)