From 87c9cf2ac160108cb1b9ae4be75d1d81e6c5d0a3 Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Mon, 15 Mar 2021 09:55:42 -0400 Subject: [PATCH] Fix #861, compile time assert for sockaddr size 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. --- src/os/portable/os-impl-bsd-sockets.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/os/portable/os-impl-bsd-sockets.c b/src/os/portable/os-impl-bsd-sockets.c index 59bc5d26a..f50265dc3 100644 --- a/src/os/portable/os-impl-bsd-sockets.c +++ b/src/os/portable/os-impl-bsd-sockets.c @@ -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 ***************************************************************************************/ @@ -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; } @@ -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; }