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

inet_ntop: add typecast to silence Coverity #11960

Closed
wants to merge 1 commit into from
Closed

Conversation

bagder
Copy link
Member

@bagder bagder commented Sep 27, 2023

CID 1024653: Integer handling issues (SIGN_EXTENSION)
Suspicious implicit sign extension: "src[i]" with type "unsigned
char const" (8 bits, unsigned) is promoted in "src[i] << (1 - i % 2 << 3)" to type "int" (32 bits, signed), then sign-extended to type "unsigned long" (64 bits, unsigned). If "src[i] << (1 - i % 2 << 3)" is greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

111 words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));

The value will not be greater than 0x7FFFFFFF so this still cannot happen.

Also, switch to ints here instead of longs. The values stored are 16 bit so at least no need to use 64 bit variables. Also, longs are 32 bit on some platforms so this logic still needs to work with 32 bits.

>>> CID 1024653:  Integer handling issues  (SIGN_EXTENSION)
>>> Suspicious implicit sign extension: "src[i]" with type "unsigned
char const" (8 bits, unsigned) is promoted in "src[i] << (1 - i % 2 <<
3)" to type "int" (32 bits, signed), then sign-extended to type
"unsigned long" (64 bits, unsigned).  If "src[i] << (1 - i % 2 << 3)" is
greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

111         words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));

The value will not be greater than 0x7FFFFFFF so this still cannot
happen.

Also, switch to ints here instead of longs. The values stored are 16 bit
so at least no need to use 64 bit variables. Also, longs are 32 bit on
some platforms so this logic still needs to work with 32 bits.
@bagder bagder added the tidy-up label Sep 27, 2023
@bagder bagder closed this in 1f92db8 Sep 27, 2023
@bagder bagder deleted the bagder/inet_ntop branch September 27, 2023 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

None yet

1 participant