diff --git a/src/MongoDB/Query.c b/src/MongoDB/Query.c index 362ec622c..1c02c4847 100644 --- a/src/MongoDB/Query.c +++ b/src/MongoDB/Query.c @@ -333,6 +333,7 @@ bool phongo_query_init(zval* return_value, zval* filter, zval* options) if (php_array_existsc(options, "modifiers")) { modifiers = php_array_fetchc(options, "modifiers"); + ZVAL_DEREF(modifiers); if (Z_TYPE_P(modifiers) != IS_ARRAY) { phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"modifiers\" option to be array, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(modifiers)); return false; diff --git a/src/contrib/php_array_api.h b/src/contrib/php_array_api.h index 17dc74810..e5d8d096a 100644 --- a/src/contrib/php_array_api.h +++ b/src/contrib/php_array_api.h @@ -188,7 +188,12 @@ zval *php_array_fetchl(zval *zarr, const char *key, int key_len) { } static inline zval *php_array_fetch(zval *zarr, const char *key) { - return php_array_fetchl(zarr, key, strlen(key)); + zval *ret = php_array_fetchl(zarr, key, strlen(key)); + if (ret) { + ZVAL_DEREF(ret); + } + + return ret; } #define php_array_fetchc(zarr, litstr) php_array_fetchl(zarr, litstr, sizeof(litstr)-1) static inline diff --git a/tests/query/bug2457-001.phpt b/tests/query/bug2457-001.phpt new file mode 100644 index 000000000..5033ff558 --- /dev/null +++ b/tests/query/bug2457-001.phpt @@ -0,0 +1,34 @@ +--TEST-- +PHPC-2457: Query modifiers can be passed reference +--FILE-- + ['x' => 1]]; + +$query = new MongoDB\Driver\Query([], [ + 'modifiers' => &$modifiers, +]); + +var_dump($query); + +?> +===DONE=== + +--EXPECTF-- +Deprecated: MongoDB\Driver\Query::__construct(): The "modifiers" option is deprecated and will be removed in a future release in %s +object(MongoDB\Driver\Query)#1 (3) { + ["filter"]=> + object(stdClass)#2 (0) { + } + ["options"]=> + object(stdClass)#4 (1) { + ["sort"]=> + object(stdClass)#3 (1) { + ["x"]=> + int(1) + } + } + ["readConcern"]=> + NULL +} +===DONE=== diff --git a/tests/query/bug2457-002.phpt b/tests/query/bug2457-002.phpt new file mode 100644 index 000000000..90a22c246 --- /dev/null +++ b/tests/query/bug2457-002.phpt @@ -0,0 +1,49 @@ +--TEST-- +PHPC-2457: Query options can be passed reference +--FILE-- + 'fr_FR', 'strength' => 2]; +$let = ['x' => 1]; +$sort = ['_id' => 1]; + +$query = new MongoDB\Driver\Query([], [ + 'collation' => &$collation, + 'let' => &$let, + 'sort' => &$sort, +]); + +var_dump($query); + +?> +===DONE=== + +--EXPECT-- +object(MongoDB\Driver\Query)#1 (3) { + ["filter"]=> + object(stdClass)#2 (0) { + } + ["options"]=> + object(stdClass)#6 (3) { + ["collation"]=> + object(stdClass)#3 (2) { + ["locale"]=> + string(5) "fr_FR" + ["strength"]=> + int(2) + } + ["let"]=> + object(stdClass)#4 (1) { + ["x"]=> + int(1) + } + ["sort"]=> + object(stdClass)#5 (1) { + ["_id"]=> + int(1) + } + } + ["readConcern"]=> + NULL +} +===DONE===