Skip to content

Fix unspecified behaviour in zend_alloc in heap->limit computation #12613

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

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Nov 5, 2023

Right-shifting a negative number is unspecified (i.e. implementation-defined) behaviour [1]. If we take a look at the generated assembly [2], we see that the wrong value is computed. Fix it by using Z_UL instead of Z_L.

While we're at it, just change every occurrence of this pattern to use Z_UL instead of casting.

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf §6.5.7.5
[2] https://godbolt.org/z/4Y1qKKjsh

Right-shifting a negative number is unspecified (i.e.
implementation-defined) behaviour [1]. If we take a look at the
generated assembly [2], we see that the wrong value is computed.
Fix it by using Z_UL instead of Z_L.

While we're at it, just change every occurrence of this pattern to use
Z_UL instead of casting.

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf §6.5.7.5
[2] https://godbolt.org/z/4Y1qKKjsh
@nielsdos nielsdos force-pushed the unspecified-behaviour-zend-alloc branch from 616eaa6 to 776e737 Compare November 5, 2023 00:30
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

As heap->limit is size_t, would it not be more accurate to use the ((size_t)Z_L(-1) >> (size_t)Z_L(1)) variant? If size_t < unsigned long long (e.g. Windows 32-bit I think) this would lead to an incorrect value (highest bit set to 1).

@nielsdos
Copy link
Member Author

nielsdos commented Nov 6, 2023

As heap->limit is size_t, would it not be more accurate to use the ((size_t)Z_L(-1) >> (size_t)Z_L(1)) variant? If size_t < unsigned long long (e.g. Windows 32-bit I think) this would lead to an incorrect value (highest bit set to 1).

Good catch, forgot about 32-bit for a second.

Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

LGTM, thank you 🙂

@nielsdos nielsdos closed this in 28110f8 Nov 6, 2023
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.

4 participants