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

[IPv6] Fix coverity #933

Merged
merged 6 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions source/FreeRTOS_ARP.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,11 @@ BaseType_t xIsIPInARPCache( uint32_t ulAddressToLookup )
*
* @return pdTRUE if the packet needs ARP resolution, pdFALSE otherwise.
*/
BaseType_t xCheckRequiresARPResolution( NetworkBufferDescriptor_t * pxNetworkBuffer )
BaseType_t xCheckRequiresARPResolution( const NetworkBufferDescriptor_t * pxNetworkBuffer )
{
BaseType_t xNeedsARPResolution = pdFALSE;

switch( uxIPHeaderSizePacket( ( const NetworkBufferDescriptor_t * ) pxNetworkBuffer ) )
switch( uxIPHeaderSizePacket( pxNetworkBuffer ) )
{
#if ( ipconfigUSE_IPv4 != 0 )
case ipSIZE_OF_IPv4_HEADER:
Expand Down Expand Up @@ -1457,9 +1457,6 @@ void FreeRTOS_ClearARP( const struct xNetworkEndPoint * pxEndPoint )
BaseType_t xResult = pdFALSE;
NetworkBufferDescriptor_t * pxUseDescriptor = pxDescriptor;

/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
const IPPacket_t * pxIPPacket;

if( ( pxUseDescriptor == NULL ) || ( pxUseDescriptor->xDataLength < sizeof( IPPacket_t ) ) )
Expand All @@ -1468,6 +1465,9 @@ void FreeRTOS_ClearARP( const struct xNetworkEndPoint * pxEndPoint )
}
else
{
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
pxIPPacket = ( ( IPPacket_t * ) pxUseDescriptor->pucEthernetBuffer );

if( pxIPPacket->xEthernetHeader.usFrameType == ipIPv4_FRAME_TYPE )
Expand Down
1 change: 1 addition & 0 deletions source/FreeRTOS_DHCPv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ static BaseType_t xDHCPv6Process_PassReplyToEndPoint( struct xNetworkEndPoint *
}
else
{
/* do nothing, coverity happy */
}
}

Expand Down
219 changes: 105 additions & 114 deletions source/FreeRTOS_DNS_Cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
/*!
* @brief DNS cache structure instantiation
*/
static DNSCacheRow_t xDNSCache[ ipconfigDNS_CACHE_ENTRIES ] = { 0x0 };
static DNSCacheRow_t xDNSCache[ ipconfigDNS_CACHE_ENTRIES ];

/*!
* @brief indicates the index of a free entry in the cache structure
Expand Down Expand Up @@ -82,11 +82,9 @@
const IPv46_Address_t * pxIP,
uint32_t ulCurrentTimeSeconds );

#if ( ipconfigUSE_DNS_CACHE == 1 )
/** Copy DNS cache entries at xIndex to a linked struct addrinfo. */
static void prvReadDNSCache( BaseType_t uxIndex,
struct freertos_addrinfo ** ppxAddressInfo );
#endif
/** Copy DNS cache entries at xIndex to a linked struct addrinfo. */
static void prvReadDNSCache( BaseType_t uxIndex,
struct freertos_addrinfo ** ppxAddressInfo );

/*-----------------------------------------------------------*/

Expand Down Expand Up @@ -476,76 +474,71 @@
}
/*-----------------------------------------------------------*/

#if ( ipconfigUSE_DNS_CACHE == 1 )

/**
* @brief Copy DNS cache entries at uxIndex to a linked struct addrinfo.
* @param[in] uxIndex The index from where entries must be copied.
* @param[out] ppxAddressInfo Target to store the DNS entries.
*/
static void prvReadDNSCache( BaseType_t uxIndex,
struct freertos_addrinfo ** ppxAddressInfo )
{
size_t uxIPAddressIndex;
size_t uxNumIPAddresses = 1U;
const IPv46_Address_t * pxAddresses;
struct freertos_addrinfo * pxNewAddress = NULL;
struct freertos_addrinfo ** ppxLastAddress = ppxAddressInfo;

#if ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY > 1 )
uxNumIPAddresses = ( size_t ) xDNSCache[ uxIndex ].ucNumIPAddresses;
static void prvReadDNSCache( BaseType_t uxIndex,
struct freertos_addrinfo ** ppxAddressInfo )
{
size_t uxIPAddressIndex;
size_t uxNumIPAddresses = 1U;
const IPv46_Address_t * pxAddresses;
struct freertos_addrinfo * pxNewAddress = NULL;
struct freertos_addrinfo ** ppxLastAddress = ppxAddressInfo;

if( uxNumIPAddresses > ( size_t ) ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY )
{
/* Make this a configASSERT()? */
uxNumIPAddresses = ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY;
}
#endif /* ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY > 1 ) */
#if ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY > 1 )
uxNumIPAddresses = ( size_t ) xDNSCache[ uxIndex ].ucNumIPAddresses;

for( uxIPAddressIndex = 0; uxIPAddressIndex < uxNumIPAddresses; uxIPAddressIndex++ )
if( uxNumIPAddresses > ( size_t ) ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY )
{
pxAddresses = &( xDNSCache[ uxIndex ].xAddresses[ uxIPAddressIndex ] );

switch( pxAddresses->xIs_IPv6 ) /* LCOV_EXCL_BR_LINE - xIs_IPv6 is always either pdFALSE or pdTRUE. */
{
#if ( ipconfigUSE_IPv4 != 0 )
case pdFALSE:
{
const uint8_t * ucBytes = ( const uint8_t * ) &( pxAddresses->xIPAddress.ulIP_IPv4 );
pxNewAddress = pxNew_AddrInfo( xDNSCache[ uxIndex ].pcName, FREERTOS_AF_INET4, ucBytes );
}
break;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
/* Make this a configASSERT()? */
uxNumIPAddresses = ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY;
}
#endif /* ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY > 1 ) */

#if ( ipconfigUSE_IPv6 != 0 )
case pdTRUE:
pxNewAddress = pxNew_AddrInfo( xDNSCache[ uxIndex ].pcName, FREERTOS_AF_INET6, pxAddresses->xIPAddress.xIP_IPv6.ucBytes );
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
for( uxIPAddressIndex = 0; uxIPAddressIndex < uxNumIPAddresses; uxIPAddressIndex++ )
{
pxAddresses = &( xDNSCache[ uxIndex ].xAddresses[ uxIPAddressIndex ] );

default: /* LCOV_EXCL_LINE - xIs_IPv6 is always either pdFALSE or FREERTOS_AF_INET6. */
/* MISRA 16.4 Compliance */
FreeRTOS_debug_printf( ( "prvReadDNSCache: Undefined IP Type \n" ) );
break; /* LCOV_EXCL_LINE - xIs_IPv6 is always either pdFALSE or FREERTOS_AF_INET6. */
}
switch( pxAddresses->xIs_IPv6 ) /* LCOV_EXCL_BR_LINE - xIs_IPv6 is always either pdFALSE or pdTRUE. */
{
#if ( ipconfigUSE_IPv4 != 0 )
case pdFALSE:
{
const uint8_t * ucBytes = ( const uint8_t * ) &( pxAddresses->xIPAddress.ulIP_IPv4 );
pxNewAddress = pxNew_AddrInfo( xDNSCache[ uxIndex ].pcName, FREERTOS_AF_INET4, ucBytes );
}
break;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */

if( pxNewAddress == NULL )
{
/* Malloc must has failed. */
break;
}
#if ( ipconfigUSE_IPv6 != 0 )
case pdTRUE:
pxNewAddress = pxNew_AddrInfo( xDNSCache[ uxIndex ].pcName, FREERTOS_AF_INET6, pxAddresses->xIPAddress.xIP_IPv6.ucBytes );
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */

/* Set either 'ppxAddressInfo' or 'pxNewAddress->ai_next'. */
*( ppxLastAddress ) = pxNewAddress;
default: /* LCOV_EXCL_LINE - xIs_IPv6 is always either pdFALSE or FREERTOS_AF_INET6. */
/* MISRA 16.4 Compliance */
FreeRTOS_debug_printf( ( "prvReadDNSCache: Undefined IP Type \n" ) );
break; /* LCOV_EXCL_LINE - xIs_IPv6 is always either pdFALSE or FREERTOS_AF_INET6. */
}

ppxLastAddress = &( pxNewAddress->ai_next );
if( pxNewAddress == NULL )
{
/* Malloc must has failed. */
break;
}

/* Set either 'ppxAddressInfo' or 'pxNewAddress->ai_next'. */
*( ppxLastAddress ) = pxNewAddress;

ppxLastAddress = &( pxNewAddress->ai_next );
}
#endif /* #if( ipconfigUSE_DNS_CACHE == 1 ) */
}
/*-----------------------------------------------------------*/

#if ( ipconfigUSE_DNS_CACHE == 1 )

/**
* @brief Lookup the given hostname in the DNS cache
* @param[in] pcHostName THe host name to lookup
Expand All @@ -554,74 +547,73 @@
* @returns This function returns either a valid IPv4 address, or
* in case of an IPv6 lookup, it will return a non-zero.
*/
uint32_t Prepare_CacheLookup( const char * pcHostName,
BaseType_t xFamily,
struct freertos_addrinfo ** ppxAddressInfo )
{
uint32_t ulIPAddress = 0U;
IPv46_Address_t xIPv46_Address;
uint32_t Prepare_CacheLookup( const char * pcHostName,
BaseType_t xFamily,
struct freertos_addrinfo ** ppxAddressInfo )
{
uint32_t ulIPAddress = 0U;
IPv46_Address_t xIPv46_Address;

switch( xFamily )
{
#if ( ipconfigUSE_IPv4 != 0 )
case FREERTOS_AF_INET:
{
BaseType_t xFound;
switch( xFamily )
{
#if ( ipconfigUSE_IPv4 != 0 )
case FREERTOS_AF_INET:
{
BaseType_t xFound;

xIPv46_Address.xIs_IPv6 = pdFALSE;
xFound = FreeRTOS_ProcessDNSCache( pcHostName, &( xIPv46_Address ), 0, pdTRUE, ppxAddressInfo );
xIPv46_Address.xIs_IPv6 = pdFALSE;
xFound = FreeRTOS_ProcessDNSCache( pcHostName, &( xIPv46_Address ), 0, pdTRUE, ppxAddressInfo );

if( xFound != 0 )
if( xFound != 0 )
{
if( ( ppxAddressInfo != NULL ) && ( *( ppxAddressInfo ) != NULL ) )
{
if( ( ppxAddressInfo != NULL ) && ( *( ppxAddressInfo ) != NULL ) )
{
const struct freertos_sockaddr * sockaddr = ( *( ppxAddressInfo ) )->ai_addr;
const struct freertos_sockaddr * sockaddr = ( *( ppxAddressInfo ) )->ai_addr;

ulIPAddress = sockaddr->sin_address.ulIP_IPv4;
}
}
else
{
/* prvGetHostByName will be called to start a DNS lookup. */
ulIPAddress = sockaddr->sin_address.ulIP_IPv4;
}
}
break;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */

#if ( ipconfigUSE_IPv6 != 0 )
case FREERTOS_AF_INET6:
else
{
BaseType_t xFound;
/* prvGetHostByName will be called to start a DNS lookup. */
}
}
break;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */

xIPv46_Address.xIs_IPv6 = pdTRUE;
xFound = FreeRTOS_ProcessDNSCache( pcHostName, &( xIPv46_Address ), 0, pdTRUE, ppxAddressInfo );
#if ( ipconfigUSE_IPv6 != 0 )
case FREERTOS_AF_INET6:
{
BaseType_t xFound;

if( xFound != 0 )
{
if( ( ppxAddressInfo != NULL ) && ( *( ppxAddressInfo ) != NULL ) )
{
/* This function returns either a valid IPv4 address, or
* in case of an IPv6 lookup, it will return a non-zero */
ulIPAddress = 1U;
}
}
else
xIPv46_Address.xIs_IPv6 = pdTRUE;
xFound = FreeRTOS_ProcessDNSCache( pcHostName, &( xIPv46_Address ), 0, pdTRUE, ppxAddressInfo );

if( xFound != 0 )
{
if( ( ppxAddressInfo != NULL ) && ( *( ppxAddressInfo ) != NULL ) )
{
/* prvGetHostByName will be called to start a DNS lookup. */
/* This function returns either a valid IPv4 address, or
* in case of an IPv6 lookup, it will return a non-zero */
ulIPAddress = 1U;
}
}
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */

default:
/* MISRA 16.4 Compliance */
FreeRTOS_debug_printf( ( "Prepare_CacheLookup: Undefined xFamily \n" ) );
break;
}

return ulIPAddress;
else
{
/* prvGetHostByName will be called to start a DNS lookup. */
}
}
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */

default:
/* MISRA 16.4 Compliance */
FreeRTOS_debug_printf( ( "Prepare_CacheLookup: Undefined xFamily \n" ) );
break;
}
#endif /* ( ipconfigUSE_DNS_CACHE == 1 ) */

return ulIPAddress;
}
/*-----------------------------------------------------------*/

#if ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY > 1 )
Expand Down Expand Up @@ -688,5 +680,4 @@
#endif /* if ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY > 1 ) */
/*-----------------------------------------------------------*/


#endif /* if ( ( ipconfigUSE_DNS != 0 ) && ( ipconfigUSE_DNS_CACHE == 1 ) ) */
13 changes: 10 additions & 3 deletions source/FreeRTOS_IP.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,20 @@ static void prvIPTask_Initialise( void )
/* Mark the timer as inactive since we are not waiting on any ARP resolution as of now. */
vIPSetARPResolutionTimerEnableState( pdFALSE );

#if ( ipconfigDNS_USE_CALLBACKS != 0 )
#if ( ( ipconfigDNS_USE_CALLBACKS != 0 ) && ( ipconfigUSE_DNS != 0 ) )
{
/* The following function is declared in FreeRTOS_DNS.c and 'private' to
/* The following function is declared in FreeRTOS_DNS.c and 'private' to
* this library */
vDNSInitialise();
}
#endif /* ipconfigDNS_USE_CALLBACKS != 0 */
#endif /* ( ipconfigDNS_USE_CALLBACKS != 0 ) && ( ipconfigUSE_DNS != 0 ) */

#if ( ( ipconfigUSE_DNS_CACHE != 0 ) && ( ipconfigUSE_DNS != 0 ) )
{
/* Clear the DNS cache once only. */
FreeRTOS_dnsclear();
}
#endif /* ( ( ipconfigUSE_DNS_CACHE != 0 ) && ( ipconfigUSE_DNS != 0 ) ) */

/* Initialisation is complete and events can now be processed. */
xIPTaskInitialised = pdTRUE;
Expand Down
2 changes: 1 addition & 1 deletion source/FreeRTOS_IP_Utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ static BaseType_t prvChecksumProtocolChecks( size_t uxBufferLength,
}
else
{
uxOptionsLength = ( size_t ) ( ( ucLength - 5U ) << 2U );
uxOptionsLength = ( ( ( size_t ) ucLength - 5U ) << 2U );

pxSet->uxProtocolHeaderLength = ipSIZE_OF_TCP_HEADER + uxOptionsLength;
#if ( ipconfigHAS_DEBUG_PRINTF != 0 )
Expand Down
2 changes: 1 addition & 1 deletion source/FreeRTOS_ND.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@

if( eMyType == eIPType )
{
eReturn = prvNDCacheLookup( pxIPAddress, pxMACAddress, &pxEndPoint );
eReturn = prvNDCacheLookup( pxIPAddress, pxMACAddress, ppxEndPoint );
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/FreeRTOS_RA.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
size_t uxPrefixLength = ( size_t ) pucBytes[ uxIndex + 1U ];
size_t uxLength = uxPrefixLength * 8U;

if( uxPrefixLength == 0 )
if( uxPrefixLength == 0U )
{
/* According to RFC 4861, length of the option value 0 is invalid. Hence returning from here */
FreeRTOS_printf( ( "RA: Invalid length of the option value as zero. " ) );
Expand Down
Loading