Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #864, Refactor duplicate addrlen swtiches into an inline function #1388

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 31 additions & 44 deletions src/os/portable/os-impl-bsd-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,14 @@ int32 OS_SocketOpen_Impl(const OS_object_token_t *token)

/*----------------------------------------------------------------
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
* Internal helper routine only, not part of API.
*
*-----------------------------------------------------------------*/
int32 OS_SocketBind_Impl(const OS_object_token_t *token, const OS_SockAddr_t *Addr)
static inline socklen_t OS_GetAddrLen(sa_family_t sa_family)

Check notice

Code scanning / CodeQL

Long function without assertion

All functions of more than 10 lines should have at least one assertion.
{
int os_result;
socklen_t addrlen;
const struct sockaddr * sa;
OS_impl_file_internal_record_t *impl;
OS_stream_internal_record_t * stream;

impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token);
stream = OS_OBJECT_TABLE_GET(OS_stream_table, *token);
socklen_t addrlen;

sa = (const struct sockaddr *)&Addr->AddrData;

switch (sa->sa_family)
switch (sa_family)
{
case AF_INET:
addrlen = sizeof(struct sockaddr_in);
Expand All @@ -242,6 +232,29 @@ int32 OS_SocketBind_Impl(const OS_object_token_t *token, const OS_SockAddr_t *Ad
break;
}

return addrlen;
}

/*----------------------------------------------------------------
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketBind_Impl(const OS_object_token_t *token, const OS_SockAddr_t *Addr)

Check notice

Code scanning / CodeQL

Long function without assertion

All functions of more than 10 lines should have at least one assertion.
{
int os_result;
socklen_t addrlen;
const struct sockaddr * sa;
OS_impl_file_internal_record_t *impl;
OS_stream_internal_record_t * stream;

impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token);
stream = OS_OBJECT_TABLE_GET(OS_stream_table, *token);

sa = (const struct sockaddr *)&Addr->AddrData;
addrlen = OS_GetAddrLen(sa->sa_family);

if (addrlen == 0)
{
return OS_ERR_BAD_ADDRESS;
Expand Down Expand Up @@ -285,21 +298,8 @@ int32 OS_SocketConnect_Impl(const OS_object_token_t *token, const OS_SockAddr_t

impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token);

sa = (const struct sockaddr *)&Addr->AddrData;
switch (sa->sa_family)
{
case AF_INET:
slen = sizeof(struct sockaddr_in);
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
slen = sizeof(struct sockaddr_in6);
break;
#endif
default:
slen = 0;
break;
}
sa = (const struct sockaddr *)&Addr->AddrData;
slen = OS_GetAddrLen(sa->sa_family);

if (slen != Addr->ActualLength)
{
Expand Down Expand Up @@ -564,21 +564,8 @@ int32 OS_SocketSendTo_Impl(const OS_object_token_t *token, const void *buffer, s

impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token);

sa = (const struct sockaddr *)&RemoteAddr->AddrData;
switch (sa->sa_family)
{
case AF_INET:
addrlen = sizeof(struct sockaddr_in);
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
addrlen = sizeof(struct sockaddr_in6);
break;
#endif
default:
addrlen = 0;
break;
}
sa = (const struct sockaddr *)&RemoteAddr->AddrData;
addrlen = OS_GetAddrLen(sa->sa_family);

if (addrlen != RemoteAddr->ActualLength)
{
Expand Down