Skip to content

Commit

Permalink
Fix nasa#861, compile time assert for sockaddr size
Browse files Browse the repository at this point in the history
OSAL provides an abstract buffer for socket addresses, independent
of the underlying implementation.  The size of this buffer is
configurable by the user via compile-time options.

This adds a CompileTimeAssert to confirm that the size of this
abstract buffer is large enough to store any of the enabled
address types. This also removes the need for runtime tests.
  • Loading branch information
jphickey committed Mar 15, 2021
1 parent ead5723 commit 87c9cf2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/os/portable/os-impl-bsd-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ typedef union
#endif
} OS_SockAddr_Accessor_t;

/*
* Confirm that the abstract socket address buffer size (OS_SOCKADDR_MAX_LEN) is
* large enough to store any of the enabled address types. If this is true, the
* size of the above union will match OS_SOCKADDR_MAX_LEN. However, if any
* implemention-provided struct types are larger than this, the union will be
* larger, and this indicates a configuration error.
*/
CompileTimeAssert(sizeof(OS_SockAddr_Accessor_t) == OS_SOCKADDR_MAX_LEN, SockAddrSize);

/****************************************************************************************
Sockets API
***************************************************************************************/
Expand Down Expand Up @@ -200,7 +209,7 @@ int32 OS_SocketBind_Impl(const OS_object_token_t *token, const OS_SockAddr_t *Ad
break;
}

if (addrlen == 0 || addrlen > OS_SOCKADDR_MAX_LEN)
if (addrlen == 0)
{
return OS_ERR_BAD_ADDRESS;
}
Expand Down Expand Up @@ -560,7 +569,7 @@ int32 OS_SocketAddrInit_Impl(OS_SockAddr_t *Addr, OS_SocketDomain_t Domain)
break;
}

if (addrlen == 0 || addrlen > OS_SOCKADDR_MAX_LEN)
if (addrlen == 0)
{
return OS_ERR_NOT_IMPLEMENTED;
}
Expand Down

0 comments on commit 87c9cf2

Please sign in to comment.