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

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Sep 26, 2024

PHPC-2457

Previous fixes for this were made in #1683 and #1223, but both attempts resulted in missed cases. #1694 fixes this for query modifiers and options, but a number of other occurrences were affected, as evidenced by the changes in this PR.

To fix this on a wider scale, I made a few adjustments to php_array_api.h, some of them unrelated:

  • (unrelated) remove support for zend_engine 2 (PHP < 7)
  • (unrelated) simplify some switch statements and fix casts
  • add php_array_fetch*_deref
  • change php_array_fetch*_<type> to always use *_deref version

After making this change, I audited all usages of php_array_fetch* and updated them accordingly. If references were correctly handled (such as in phongo_zval_to_bson_value which always calls ZVAL_DEREF), the call was not changed. Most cases immediately checked types after calling php_array_fetch, which was changed to php_array_fetch_deref.

In addition, I changed a number of usages of php_array_fetch to php_array_fetchc when a constant string was used.

I also included the tests added in #1694, but did not add tests for each and every usage that was changed. Let me know if you think those tests are necessary.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

Suggested test file rename and a code formatting complaint, but content LGTM.

static inline
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

@@ -0,0 +1,49 @@
--TEST--
PHPC-2457: Query options can be passed reference
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest swapping the names on these two tests, so once modifiers is removed in 2.0 we can delete its test and just be left with a bug2457-001.phpt file.

@alcaeus alcaeus enabled auto-merge (squash) September 27, 2024 06:45
@alcaeus alcaeus removed the request for review from GromNaN September 27, 2024 06:45
@alcaeus alcaeus merged commit 2ef0ff2 into mongodb:v1.20 Sep 27, 2024
81 checks passed
["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).

@alcaeus alcaeus deleted the phpc-2457-fix-array-fetch-deref branch September 30, 2024 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants