Skip to content
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

PHPC-2420: Compare Int64 instances without casting #1617

Merged
merged 3 commits into from
Aug 28, 2024

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Jul 26, 2024

PHPC-2420

Previously, we deferred to zend_std_compare_objects when comparing a single Int64 instance with a long or double. By default, this will attempt to cast values to long or double, then comparing it. On 32-bit platforms, this cast truncates any value outside of the 32-bit range and yields a warning about this truncation. This means that we were not able to properly compare such values, even when we technically would easily be able to (as only the value of ZEND_LONG is limited to 32 bits, but int64_t is still available).

This PR changes this logic to specifically handle comparisons between to Int64 instances as well as a single Int64 instance with a long or double. Only if the instance is being compared with anything else will we attempt to cast the value. This removes warnings and fixes comparisons of 64-bit values on 32-bit platforms.

@alcaeus alcaeus requested a review from GromNaN July 26, 2024 08:44
@alcaeus alcaeus self-assigned this Jul 26, 2024
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

LGTM as tests shows this is correct.

src/BSON/Int64.c Show resolved Hide resolved
@alcaeus alcaeus force-pushed the phpc-2420-fix-int64-comparisons branch from 1de9b24 to fd72374 Compare August 27, 2024 10:24
@alcaeus alcaeus requested a review from GromNaN August 27, 2024 10:24
@alcaeus
Copy link
Member Author

alcaeus commented Aug 27, 2024

After some more investigation, I remembered that PHP 7.4 still has the old comparison logic, where the compare_objects handler was only used when comparing two objects, while the compare handler handled comparing an object to any other zval (see code). In PHP 8, comparisons were simplified, with the original compare handler being removed and compare_objects renamed to compare.

@alcaeus alcaeus force-pushed the phpc-2420-fix-int64-comparisons branch from fd72374 to e52355e Compare August 27, 2024 10:27
@alcaeus alcaeus merged commit f47915e into mongodb:v1.19 Aug 28, 2024
70 checks passed
@alcaeus alcaeus deleted the phpc-2420-fix-int64-comparisons branch August 28, 2024 12:15
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