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

Fix incorrect check in fpm_shm_free() #13797

Merged
merged 1 commit into from Mar 24, 2024
Merged

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Mar 24, 2024

if (fpm_shm_size - size > 0) will be rewritten by the compiler as this: if (fpm_shm_size != size), which is undesirable. The reason this happens is that both variables are size_t, so subtracting them cannot be negative. The only way it can be not > 0, is if they're equal because the result will then be 0. This means that the else branch won't work properly. E.g. if fpm_shm_size == 50 and size == 51, then fpm_shm_size will wraparound instead of becoming zero.

To showcase that the compiler actually does this, take a look at this isolated case: https://godbolt.org/z/azobdWcrY. Here we can see the usage of the compare instruction + cmove, so the "then" branch is only done if the variables are equal.

That all being said, I think it's better to be an assertion + an unconditional subtraction, as the check itself already looks weird.

`if (fpm_shm_size - size > 0)` will be rewritten by the compiler as
this: `if (fpm_shm_size == size)`, which is undesirable.
The reason this happens is that both variables are size_t, so
subtracting them cannot be negative. The only way it can be not > 0, is
if they're equal because the result will then be 0.
This means that if we have multiple fpm shm allocations, the shm size
will not decrease correctly.

To showcase that the compiler actually does this, take a look at this
isolated case: https://godbolt.org/z/azobdWcrY. Here we can see the
usage of the compare instruction + cmove, so the "then" branch
is only done if the variables are equal.
@nielsdos nielsdos merged commit dd3aa18 into php:PHP-8.2 Mar 24, 2024
8 checks passed
nielsdos added a commit that referenced this pull request Mar 24, 2024
* PHP-8.2:
  [ci skip] NEWS
  Fix incorrect check in fpm_shm_free() (#13797)
nielsdos added a commit that referenced this pull request Mar 24, 2024
* PHP-8.3:
  [ci skip] NEWS
  Fix incorrect check in fpm_shm_free() (#13797)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants