Fix undefined behavior due to shifting an int by 31

This commit is contained in:
Cameron Gutman 2020-12-06 00:44:33 -06:00
parent 6dd3f9e7bc
commit ce546b12b0

View File

@ -111,7 +111,7 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
} }
for (i = 0; i < PORT_FLAGS_MAX_COUNT; i++) { for (i = 0; i < PORT_FLAGS_MAX_COUNT; i++) {
if (testPortFlags & (1 << i)) { if (testPortFlags & (1U << i)) {
sockets[i] = createSocket(address.ss_family, sockets[i] = createSocket(address.ss_family,
LiGetProtocolFromPortFlagIndex(i) == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM, LiGetProtocolFromPortFlagIndex(i) == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
LiGetProtocolFromPortFlagIndex(i), LiGetProtocolFromPortFlagIndex(i),
@ -133,7 +133,7 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
Limelog("Failed to start async connect to TCP %u: %d\n", LiGetPortFromPortFlagIndex(i), err); Limelog("Failed to start async connect to TCP %u: %d\n", LiGetPortFromPortFlagIndex(i), err);
// Mask off this bit so we don't try to include it in pollSockets() below // Mask off this bit so we don't try to include it in pollSockets() below
testPortFlags &= ~(1 << i); testPortFlags &= ~(1U << i);
} }
} }
} }
@ -149,7 +149,7 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
Limelog("Failed to send test packet to UDP %u: %d\n", LiGetPortFromPortFlagIndex(i), err); Limelog("Failed to send test packet to UDP %u: %d\n", LiGetPortFromPortFlagIndex(i), err);
// Mask off this bit so we don't try to include it in pollSockets() below // Mask off this bit so we don't try to include it in pollSockets() below
testPortFlags &= ~(1 << i); testPortFlags &= ~(1U << i);
break; break;
} }
@ -170,7 +170,7 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
// Fill out our FD sets // Fill out our FD sets
for (i = 0; i < PORT_FLAGS_MAX_COUNT; i++) { for (i = 0; i < PORT_FLAGS_MAX_COUNT; i++) {
if (testPortFlags & (1 << i)) { if (testPortFlags & (1U << i)) {
pfds[nfds].fd = sockets[i]; pfds[nfds].fd = sockets[i];
if (LiGetProtocolFromPortFlagIndex(i) == IPPROTO_UDP) { if (LiGetProtocolFromPortFlagIndex(i) == IPPROTO_UDP) {
@ -211,7 +211,7 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
// This socket was signalled. Figure out what port it was. // This socket was signalled. Figure out what port it was.
for (portIndex = 0; portIndex < PORT_FLAGS_MAX_COUNT; portIndex++) { for (portIndex = 0; portIndex < PORT_FLAGS_MAX_COUNT; portIndex++) {
if (sockets[portIndex] == pfds[i].fd) { if (sockets[portIndex] == pfds[i].fd) {
LC_ASSERT(testPortFlags & (1 << portIndex)); LC_ASSERT(testPortFlags & (1U << portIndex));
break; break;
} }
} }
@ -225,13 +225,13 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
// a packet from the test server, or it could be because we // a packet from the test server, or it could be because we
// received an ICMP error which will be given to us from // received an ICMP error which will be given to us from
// recvfrom(). // recvfrom().
testPortFlags &= ~(1 << portIndex); testPortFlags &= ~(1U << portIndex);
// Check if the socket can be successfully read now // Check if the socket can be successfully read now
err = recvfrom(sockets[portIndex], buf, sizeof(buf), 0, NULL, NULL); err = recvfrom(sockets[portIndex], buf, sizeof(buf), 0, NULL, NULL);
if (err >= 0) { if (err >= 0) {
// The UDP test was a success. // The UDP test was a success.
failingPortFlags &= ~(1 << portIndex); failingPortFlags &= ~(1U << portIndex);
Limelog("UDP port %u test successful\n", LiGetPortFromPortFlagIndex(portIndex)); Limelog("UDP port %u test successful\n", LiGetPortFromPortFlagIndex(portIndex));
} }
@ -250,10 +250,10 @@ unsigned int LiTestClientConnectivity(const char* testServer, unsigned short ref
} }
// The TCP test has completed for this port // The TCP test has completed for this port
testPortFlags &= ~(1 << portIndex); testPortFlags &= ~(1U << portIndex);
if (err == 0) { if (err == 0) {
// The TCP test was a success // The TCP test was a success
failingPortFlags &= ~(1 << portIndex); failingPortFlags &= ~(1U << portIndex);
Limelog("TCP port %u test successful\n", LiGetPortFromPortFlagIndex(portIndex)); Limelog("TCP port %u test successful\n", LiGetPortFromPortFlagIndex(portIndex));
} }