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 undefined behavior in shifting #211

Open
escherstair opened this issue Oct 1, 2024 · 0 comments
Open

Fix undefined behavior in shifting #211

escherstair opened this issue Oct 1, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@escherstair
Copy link

Rule 6.5.7 of ISO/IEC 9899:1999 (E) standard describes Bitwise shift operators.
Paragraph 5 states:

The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type
or if E1 has a signed type and a nonnegative value, the value of the result is the integral
part of the quotient of E1 divided by the quantity, 2 raised to the power E2. If E1 has a
signed type and a negative value, the resulting value is implementation-defined.

So, right shifing a negative number is implementation-defined (undefined behavior), if it is really negative, basically because it depends on how the sign bit is implemented.
But nobody can know if during the execution E1 will be negative or positive.
This is one of the reasons why shifting a signed integer (that can be negative, during the program execution) is not a good idea.
Shinfting unsigned integeres only stays on the safe side for every scenario.

Here are some points in the source code where signed integers are shifted:

Is it possible having them fixed so that tools for static analysis do not complain about them?
Thanks

@christophe0606 christophe0606 added review Under review enhancement New feature or request and removed review Under review labels Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants