Skip to content

PHPC-2457: Fix using array items by reference #1697

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

Merged
merged 7 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 5 additions & 5 deletions src/contrib/php_array_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,24 @@ static inline zval* zval_deref_safe(zval* z) {
* zval *php_array_fetchz_deref(zval *zarr, zval *key)
*/
static inline
zval *php_array_fetchl_deref(zval *zarr, const char *key, int key_len) {
zval *php_array_fetchl_deref(zval *zarr, const char *key, int key_len) {
return zval_deref_safe(php_array_fetchl(zarr, key, key_len));
}
Copy link
Member

Choose a reason for hiding this comment

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

I assume this is clang-format, but I find this formatting very bizarre.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's copy-paste from a previous occurrence, but I'll fix it

static inline
zval *php_array_fetch_deref(zval *zarr, const char *key) {
zval *php_array_fetch_deref(zval *zarr, const char *key) {
return zval_deref_safe(php_array_fetch(zarr, key));
}
#define php_array_fetchc_deref(zarr, litstr) zval_deref_safe(php_array_fetchl(zarr, litstr, sizeof(litstr)-1))
static inline
zval *php_array_fetchl_safe_deref(zval *zarr, const char *key, int key_len) {
zval *php_array_fetchl_safe_deref(zval *zarr, const char *key, int key_len) {
return zval_deref_safe(php_array_fetchl_safe(zarr, key, key_len));
}
static inline
zval *php_array_fetchn_deref(zval *zarr, zend_ulong idx) {
zval *php_array_fetchn_deref(zval *zarr, zend_ulong idx) {
return zval_deref_safe(php_array_fetchn(zarr, idx));
}
static inline
zval *php_array_fetchz_deref(zval *zarr, zval *key) {
zval *php_array_fetchz_deref(zval *zarr, zval *key) {
return zval_deref_safe(php_array_fetchz(zarr, key));
}

Expand Down
31 changes: 23 additions & 8 deletions tests/query/bug2457-001.phpt
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
--TEST--
PHPC-2457: Query modifiers can be passed reference
PHPC-2457: Query options can be passed reference
--FILE--
<?php

$modifiers = ['$orderby' => ['x' => 1]];
$collation = ['locale' => 'fr_FR', 'strength' => 2];
$let = ['x' => 1];
$sort = ['_id' => 1];

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

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
--EXPECT--
object(MongoDB\Driver\Query)#1 (3) {
["filter"]=>
object(stdClass)#2 (0) {
}
["options"]=>
object(stdClass)#4 (1) {
["sort"]=>
object(stdClass)#3 (1) {
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
Expand Down
31 changes: 8 additions & 23 deletions tests/query/bug2457-002.phpt
Original file line number Diff line number Diff line change
@@ -1,45 +1,30 @@
--TEST--
PHPC-2457: Query options can be passed reference
PHPC-2457: Query modifiers can be passed reference
--FILE--
<?php

$collation = ['locale' => 'fr_FR', 'strength' => 2];
$let = ['x' => 1];
$sort = ['_id' => 1];
$modifiers = ['$orderby' => ['x' => 1]];

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

var_dump($query);

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
--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)#6 (3) {
["collation"]=>
object(stdClass)#3 (2) {
["locale"]=>
string(5) "fr_FR"
["strength"]=>
int(2)
}
["let"]=>
object(stdClass)#4 (1) {
["x"]=>
int(1)
}
object(stdClass)#4 (1) {
Copy link
Member

Choose a reason for hiding this comment

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

In the future, note that you can use %d for the object identifiers here, since those don't need to be asserted (and could change if we did internal refactoring).

["sort"]=>
object(stdClass)#5 (1) {
["_id"]=>
object(stdClass)#3 (1) {
["x"]=>
int(1)
}
}
Expand Down