Skip to content

Commit

Permalink
Fix a few warnings in gcc-11 from string length violations
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdelisle committed Dec 16, 2020
1 parent 091466d commit 20ddc26
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
36 changes: 20 additions & 16 deletions tunnel/IpTunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,41 +652,44 @@ static bool prefixMatches4(uint8_t* addressA, uint8_t* refAddr, uint32_t prefixL
return !((a ^ b) >> (32 - prefixLen));
}

static bool isValidAddress4(uint8_t sourceAndDestIp4[8],
static bool isValidAddress4(uint8_t sourceIp4[4],
uint8_t destIp4[4],
bool isFromTun,
struct IpTunnel_Connection* conn)
{
uint8_t* compareAddr = (isFromTun)
? ((conn->isOutgoing) ? sourceAndDestIp4 : &sourceAndDestIp4[4])
: ((conn->isOutgoing) ? &sourceAndDestIp4[4] : sourceAndDestIp4);
? ((conn->isOutgoing) ? sourceIp4 : destIp4)
: ((conn->isOutgoing) ? destIp4 : sourceIp4);
return prefixMatches4(compareAddr, conn->connectionIp4, conn->connectionIp4Alloc);
}

static bool isValidAddress6(uint8_t sourceAndDestIp6[32],
static bool isValidAddress6(uint8_t sourceIp6[16],
uint8_t destIp6[16],
bool isFromTun,
struct IpTunnel_Connection* conn)
{
if (AddressCalc_validAddress(sourceAndDestIp6)
|| AddressCalc_validAddress(&sourceAndDestIp6[16])) {
if (AddressCalc_validAddress(sourceIp6) || AddressCalc_validAddress(destIp6)) {
return false;
}
uint8_t* compareAddr = (isFromTun)
? ((conn->isOutgoing) ? sourceAndDestIp6 : &sourceAndDestIp6[16])
: ((conn->isOutgoing) ? &sourceAndDestIp6[16] : sourceAndDestIp6);
? ((conn->isOutgoing) ? sourceIp6 : destIp6)
: ((conn->isOutgoing) ? destIp6 : sourceIp6);
return prefixMatches6(compareAddr, conn->connectionIp6, conn->connectionIp6Alloc);
}

static struct IpTunnel_Connection* findConnection(uint8_t sourceAndDestIp6[32],
uint8_t sourceAndDestIp4[8],
static struct IpTunnel_Connection* findConnection(uint8_t sourceIp6[32],
uint8_t destIp6[32],
uint8_t sourceIp4[4],
uint8_t destIp4[4],
bool isFromTun,
struct IpTunnel_pvt* context)
{
for (int i = 0; i < (int)context->pub.connectionList.count; i++) {
struct IpTunnel_Connection* conn = &context->pub.connectionList.connections[i];
if (sourceAndDestIp6 && isValidAddress6(sourceAndDestIp6, isFromTun, conn)) {
if (sourceIp6 && destIp6 && isValidAddress6(sourceIp6, destIp6, isFromTun, conn)) {
return conn;
}
if (sourceAndDestIp4 && isValidAddress4(sourceAndDestIp4, isFromTun, conn)) {
if (sourceIp4 && destIp4 && isValidAddress4(sourceIp4, destIp4, isFromTun, conn)) {
return conn;
}
}
Expand All @@ -707,10 +710,11 @@ static Iface_DEFUN incomingFromTun(struct Message* message, struct Iface* tunIf)
// No connections authorized, fall through to "unrecognized address"
} else if (message->length > 40 && Headers_getIpVersion(message->bytes) == 6) {
struct Headers_IP6Header* header = (struct Headers_IP6Header*) message->bytes;
conn = findConnection(header->sourceAddr, NULL, true, context);
conn = findConnection(
header->sourceAddr, header->destinationAddr, NULL, NULL, true, context);
} else if (message->length > 20 && Headers_getIpVersion(message->bytes) == 4) {
struct Headers_IP4Header* header = (struct Headers_IP4Header*) message->bytes;
conn = findConnection(NULL, header->sourceAddr, true, context);
conn = findConnection(NULL, NULL, header->sourceAddr, header->destAddr, true, context);
} else {
Log_debug(context->logger, "Message of unknown type from TUN");
return Error(INVALID);
Expand All @@ -736,7 +740,7 @@ static Iface_DEFUN ip6FromNode(struct Message* message,
Log_debug(context->logger, "Got message with zero address");
return Error(INVALID);
}
if (!isValidAddress6(header->sourceAddr, false, conn)) {
if (!isValidAddress6(header->sourceAddr, header->destinationAddr, false, conn)) {
uint8_t addr[40];
AddrTools_printIp(addr, header->sourceAddr);
Log_debug(context->logger, "Got message with wrong address for connection [%s]", addr);
Expand All @@ -755,7 +759,7 @@ static Iface_DEFUN ip4FromNode(struct Message* message,
if (Bits_isZero(header->sourceAddr, 4) || Bits_isZero(header->destAddr, 4)) {
Log_debug(context->logger, "Got message with zero address");
return Error(INVALID);
} else if (!isValidAddress4(header->sourceAddr, false, conn)) {
} else if (!isValidAddress4(header->sourceAddr, header->destAddr, false, conn)) {
Log_debug(context->logger, "Got message with wrong address [%d.%d.%d.%d] for connection "
"[%d.%d.%d.%d/%d:%d]",
header->sourceAddr[0], header->sourceAddr[1],
Expand Down
2 changes: 1 addition & 1 deletion util/AddrTools.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void AddrTools_printShortIp(uint8_t output[40], const uint8_t binIp[16])
* "fc4f:630d:e499:8f5b:c49f:6e6b:01ae:3120".
* @return 0 if successful, -1 if the hexAddr is malformed.
*/
int AddrTools_parseIp(uint8_t out[16], const uint8_t hexAddr[40])
int AddrTools_parseIp(uint8_t out[16], const uint8_t* hexAddr)
{
struct Sockaddr_storage ss;
if (Sockaddr_parse((const char*) hexAddr, &ss)
Expand Down
2 changes: 1 addition & 1 deletion util/AddrTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void AddrTools_printShortIp(uint8_t output[40], const uint8_t binIp[16]);
* "fc4f:630d:e499:8f5b:c49f:6e6b:01ae:3120".
* @return 0 if successful, -1 if the hexAddr is malformed.
*/
int AddrTools_parseIp(uint8_t out[16], const uint8_t hexAddr[40]);
int AddrTools_parseIp(uint8_t out[16], const uint8_t* hexAddr);

/**
* Parse out an ethernet MAC address.
Expand Down
2 changes: 1 addition & 1 deletion util/Bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static inline uint64_t Bits_bitReverse64(uint64_t toReverse)
* @length the nuber of bytes to check for zero'd-ness.
* @return true if all bytes checked are zero.
*/
static inline int Bits_isZero(void* buffer, size_t length)
static inline int Bits_isZero(const void* buffer, size_t length)
{
uint8_t* buff = (uint8_t*) buffer;
for (size_t i = 0; i < length; i++) {
Expand Down

0 comments on commit 20ddc26

Please sign in to comment.