Skip to content

Commit 75eda25

Browse files
committed
PHPC-2457 Fix modifiers and other Query options by reference
1 parent 5ddc557 commit 75eda25

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/MongoDB/Query.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static bool php_phongo_query_opts_append_document(bson_t* opts, const char* opts
5858
zval* value = php_array_fetch(zarr, zarr_key);
5959
bson_t b = BSON_INITIALIZER;
6060

61+
ZVAL_DEREF(value);
6162
if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_ARRAY) {
6263
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"%s\" %s to be array or object, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(value));
6364
return false;
@@ -333,6 +334,7 @@ bool phongo_query_init(zval* return_value, zval* filter, zval* options)
333334
if (php_array_existsc(options, "modifiers")) {
334335
modifiers = php_array_fetchc(options, "modifiers");
335336

337+
ZVAL_DEREF(modifiers);
336338
if (Z_TYPE_P(modifiers) != IS_ARRAY) {
337339
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"modifiers\" option to be array, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(modifiers));
338340
return false;

tests/query/bug2457-001.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
PHPC-2457: Query modifiers can be passed reference
3+
--FILE--
4+
<?php
5+
6+
$modifiers = ['$orderby' => ['x' => 1]];
7+
8+
$query = new MongoDB\Driver\Query([], [
9+
'modifiers' => &$modifiers,
10+
]);
11+
12+
var_dump($query);
13+
14+
?>
15+
===DONE===
16+
<?php exit(0); ?>
17+
--EXPECTF--
18+
Deprecated: MongoDB\Driver\Query::__construct(): The "modifiers" option is deprecated and will be removed in a future release in %s
19+
object(MongoDB\Driver\Query)#1 (3) {
20+
["filter"]=>
21+
object(stdClass)#2 (0) {
22+
}
23+
["options"]=>
24+
object(stdClass)#4 (1) {
25+
["sort"]=>
26+
object(stdClass)#3 (1) {
27+
["x"]=>
28+
int(1)
29+
}
30+
}
31+
["readConcern"]=>
32+
NULL
33+
}
34+
===DONE===

tests/query/bug2457-002.phpt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
PHPC-2457: Query options can be passed reference
3+
--FILE--
4+
<?php
5+
6+
$collation = ['locale' => 'fr_FR', 'strength' => 2];
7+
$let = ['x' => 1];
8+
$sort = ['_id' => 1];
9+
10+
$query = new MongoDB\Driver\Query([], [
11+
'collation' => &$collation,
12+
'let' => &$let,
13+
'sort' => &$sort,
14+
]);
15+
16+
var_dump($query);
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECT--
22+
object(MongoDB\Driver\Query)#1 (3) {
23+
["filter"]=>
24+
object(stdClass)#2 (0) {
25+
}
26+
["options"]=>
27+
object(stdClass)#6 (3) {
28+
["collation"]=>
29+
object(stdClass)#3 (2) {
30+
["locale"]=>
31+
string(5) "fr_FR"
32+
["strength"]=>
33+
int(2)
34+
}
35+
["let"]=>
36+
object(stdClass)#4 (1) {
37+
["x"]=>
38+
int(1)
39+
}
40+
["sort"]=>
41+
object(stdClass)#5 (1) {
42+
["_id"]=>
43+
int(1)
44+
}
45+
}
46+
["readConcern"]=>
47+
NULL
48+
}
49+
===DONE===

0 commit comments

Comments
 (0)