Skip to content

Fix incorrect bitshifting and masking in ffi bitfield #10403

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 1 commit into from
Jan 24, 2023

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Jan 21, 2023

When a uint8_t is bitshifted to the left, it is actually promoted to an int. For the current code this has the effect of a wrong sign-extension, and the result will also wrongly become zero when insert_pos >= 32. Fix this by adding an explicit cast.
Furthermore, the partial prefix byte mask was computed incorrectly: the
byte is already shifted so the mask should not account for the shift.

Note: I found these issues using a static analyser and manually verified that they're real issues.

Travis failure is unrelated to this PR.

@devnexen
Copy link
Member

looks correct but defering to @dstogov

@devnexen devnexen requested a review from dstogov January 21, 2023 18:06
Copy link
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

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

This looks right, but could you please also add a test case for this bug.

@nielsdos nielsdos changed the title Fix incorrect bitshifting in ffi Fix incorrect bitshifting and masking in ffi bitfield Jan 23, 2023
@nielsdos
Copy link
Member Author

Thank you for the comments. I added a test and by doing so I found an additional issue regarding the prefix byte's mask which I fixed as well.

When a uint8_t is bitshifted to the left, it is actually promoted to an
int. For the current code this has the effect of a wrong sign-extension,
and the result will also wrongly become zero when insert_pos >= 32.
Fix this by adding an explicit cast.
Furthermore, the partial prefix byte mask was computed incorrectly: the
byte is already shifted so the mask should not account for the shift.
@dstogov dstogov merged commit 560ca9c into php:PHP-8.1 Jan 24, 2023
dstogov added a commit that referenced this pull request Jan 24, 2023
* PHP-8.1:
  Fix incorrect bitshifting and masking in ffi bitfield (#10403)
dstogov added a commit that referenced this pull request Jan 24, 2023
* PHP-8.2:
  Fix incorrect bitshifting and masking in ffi bitfield (#10403)
@dstogov
Copy link
Member

dstogov commented Jan 24, 2023

Thank you for the comments. I added a test and by doing so I found an additional issue regarding the prefix byte's mask which I fixed as well.

Great! Thank you very much.

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.

3 participants