Skip to content

PHPC-2457 Fix modifiers and other Query options by reference #1694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/MongoDB/Query.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be done by php_array_fetchc?

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;
Expand Down
7 changes: 6 additions & 1 deletion src/contrib/php_array_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines 190 to +196
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used to read options in Query, BulkWrite and phongo_lient.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that there are a whole family of php_array_fetch functions, so even this change might be incomplete (in terms of fixing the general issue and not just the regression tests).

In this Slack thread, @alcaeus suggested a new method that conditionally applies ZVAL_DEREF(). That sounded reasonable, along with leaving this as-is and just adding our own ZVAL_DEREF() calls as needed.

As for the php_array_zval_to_<type> family of functions, I think those are easy candidates for automatic dereferencing, as @alcaeus did in previous PRs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #1697 to update php_array_api and fix the deref issue in remaining places.

}
#define php_array_fetchc(zarr, litstr) php_array_fetchl(zarr, litstr, sizeof(litstr)-1)
static inline
Expand Down
34 changes: 34 additions & 0 deletions tests/query/bug2457-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
PHPC-2457: Query modifiers can be passed reference
--FILE--
<?php

$modifiers = ['$orderby' => ['x' => 1]];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You noted the following error in PHPC-2457:

PHP Fatal error: Uncaught MongoDB\Driver\Exception\InvalidArgumentException: Expected "$orderby" modifier to be array or object, bool given

The exception originates from php_phongo_query_opts_append_document(), but I'm not sure how that's possible as the code calls PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(value). That macro delegates to zend_get_type_by_const(). IS_REFERENCE isn't even included in that function, and I can't imagine why the type would match for the function returning "bool".


$query = new MongoDB\Driver\Query([], [
'modifiers' => &$modifiers,
]);

var_dump($query);

?>
===DONE===
<?php exit(0); ?>
--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===
49 changes: 49 additions & 0 deletions tests/query/bug2457-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
PHPC-2457: Query options can be passed reference
--FILE--
<?php

$collation = ['locale' => '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===
<?php exit(0); ?>
--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===