Fixed undefined behavior in netmask generation #1717 #1718
Closed
Conversation
@@ -500,7 +500,7 @@ static int sockaddr_to_mask (const struct sockaddr *sa, int bits, u32 *mask) | |||
mask[i] = 0; | |||
} | |||
else { | |||
mask[i] = ~((1 << (unmasked_bits - (32 * (4 - i)))) - 1); | |||
mask[i] = ~(0xffffffff % (1 << unmasked_bits)); |
nnposter
Aug 31, 2019
This code suffers from a similar issue as the original one: There is no guaranteed outcome when unmasked_bits
exceeds 31. As an example:
#include <stdio.h>
void main ()
{
int m=32;
printf("%08x\n%08x\n",(0xFFFFFFFF<<(m-1)<<1),(0xFFFFFFFF<<m));
}
When compiled with GCC on Ubuntu 18.04 x64:
$ ./a.out
00000000
ffffffff
This code suffers from a similar issue as the original one: There is no guaranteed outcome when unmasked_bits
exceeds 31. As an example:
#include <stdio.h>
void main ()
{
int m=32;
printf("%08x\n%08x\n",(0xFFFFFFFF<<(m-1)<<1),(0xFFFFFFFF<<m));
}
When compiled with GCC on Ubuntu 18.04 x64:
$ ./a.out
00000000
ffffffff
rfrohl
Aug 31, 2019
Author
I assumed that it would hit another branch if unmasked_bits
is a multiple of 32. But I my understanding of how mask
is used in the rest of the code is limited, so I might miss something here.
Will test the other patch from the ticket on Monday
I assumed that it would hit another branch if unmasked_bits
is a multiple of 32. But I my understanding of how mask
is used in the rest of the code is limited, so I might miss something here.
Will test the other patch from the ticket on Monday
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Tested this change on x86_64, i586, ppc, ppc64, ppc64le, aarch64 and s390x. Unit tests passed successfully on these architectures.
Also played around with the test binary (addrset) a bit and could not find any obvious problems with the change.