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

getbits2, getbits3: rewrite to avoid left shift of negative integers #261

Open
wants to merge 1 commit into
base: demo5
Choose a base branch
from

Conversation

ourairquality
Copy link

Left shifts of negative integers is 'undefined behaviour'. Also there was an edge case that was not handled correctly, when l1 was zero, and probably never encountered. Rewrite these function to use the same approach as getbits(), form the combined unsigned bits first and then convert to a negative number if necessary.

@ourairquality
Copy link
Author

Extended this PR to update more of the getbit* and setbit* functions, just avoiding undefined behaviour, checking the argument ranges, and handling edge cases. This has not detected or addressed any current issues with their usage.

@ourairquality ourairquality force-pushed the getbits23-neg-left-shifts branch 4 times, most recently from 6f37fd3 to ff371f3 Compare May 15, 2024 09:35
Change the bit position and lengths to be unsigned values. This avoids
having to check if they are negative, and makes it clear that these
functions are only intended to be used with non-negative bit positions
and lengths.

Check and report bit lengths that are out of range.

Better handle some edge cases, such as lengths being zero.

Avoid left shits of unsigned integers which is 'undefined behaviour'.
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.

None yet

1 participant