From c8d98d443b26937cab629d878c43b4da5d6202ae Mon Sep 17 00:00:00 2001 From: Monika Singh Date: Fri, 8 Dec 2023 10:37:49 +0530 Subject: [PATCH] Fix MISRA issue (#1049) Fix following MISRA issues : MISRA Rule 8_5 : Symbol "FreeRTOS_inet_ntop6" is declared more than once. MISRA Rule 8_9 : Symbol "FreeRTOS_in6addr_loopback" should be defined at block scope. MISRA Rule 8_8 : missing static storage modifier for "prvCloseDHCPSocket" which has internal linkage. MISRA Rule 9_1 : Using uninitialized value "xRemoteIP.xIs_IPv6" when calling "pxTCPSocketLookup". MISRA Rule 9_1 : Using uninitialized value "pxAddress->sin_family" when calling "prvSocketBindAdd". MISRA Rule 11_3 : A cast shall not be performed between two pointer of different object type. MISRA Rule 12_1 : Missing parentheses on sub-expression of the operator. MISRA Rule 14.4 : The condition expression 0 does not have an essentially boolean type MISRA Rule 15_6 : The body of the "then" branch of the "if" statement is not a compound statement. MISRA Rule 17_7 : The return value of a non-void function "memset" is unused. MISRA Rule 20_5 : Using "#undef". MISRA Rule 20_10 : Use of "#" or "##" preprocessor operator. MISRA Rule 21_1 : Defining or undefining a reserved name "_static", which is an identifier or macro name beginning with an underscore. MISRA Rule 21_15 : Calling function "memcmp" with incompatible types "void " and "uint8_t const ()[6]". --- source/FreeRTOS_ARP.c | 10 +++++----- source/FreeRTOS_DHCP.c | 4 ++-- source/FreeRTOS_DNS.c | 4 +--- source/FreeRTOS_IPv6.c | 3 +++ source/FreeRTOS_RA.c | 4 ++++ source/FreeRTOS_Sockets.c | 13 ++++++++++-- source/FreeRTOS_TCP_IP_IPv6.c | 1 + source/include/FreeRTOSIPConfigDefaults.h | 20 ++++++++++++++----- source/include/FreeRTOS_IPv6_Private.h | 2 +- source/include/FreeRTOS_IPv6_Sockets.h | 7 ------- .../BufferManagement/BufferAllocation_2.c | 8 ++++---- test/cbmc/patches/FreeRTOSIPConfig.h | 7 +------ 12 files changed, 48 insertions(+), 35 deletions(-) diff --git a/source/FreeRTOS_ARP.c b/source/FreeRTOS_ARP.c index 5a84cd60b..aab9b6707 100644 --- a/source/FreeRTOS_ARP.c +++ b/source/FreeRTOS_ARP.c @@ -294,8 +294,8 @@ static TickType_t xLastGratuitousARPTime = 0U; if( ulTargetProtocolAddress == pxTargetEndPoint->ipv4_settings.ulIPAddress ) { - if( memcmp( ( void * ) pxTargetEndPoint->xMACAddress.ucBytes, - ( pxARPHeader->xSenderHardwareAddress.ucBytes ), + if( memcmp( pxTargetEndPoint->xMACAddress.ucBytes, + pxARPHeader->xSenderHardwareAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ) != 0 ) { vARPProcessPacketRequest( pxARPFrame, pxTargetEndPoint, ulSenderProtocolAddress ); @@ -310,9 +310,9 @@ static TickType_t xLastGratuitousARPTime = 0U; /* Make sure target MAC address is either ff:ff:ff:ff:ff:ff or 00:00:00:00:00:00 and senders MAC * address is not matching with the endpoint MAC address. */ - if( ( ( memcmp( ( const void * ) pxARPHeader->xTargetHardwareAddress.ucBytes, xBroadcastMACAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ) == 0 ) || - ( ( memcmp( ( const void * ) pxARPHeader->xTargetHardwareAddress.ucBytes, xGARPTargetAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ) == 0 ) ) ) && - ( memcmp( ( void * ) pxTargetEndPoint->xMACAddress.ucBytes, ( pxARPHeader->xSenderHardwareAddress.ucBytes ), ipMAC_ADDRESS_LENGTH_BYTES ) != 0 ) ) + if( ( ( memcmp( pxARPHeader->xTargetHardwareAddress.ucBytes, xBroadcastMACAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ) == 0 ) || + ( ( memcmp( pxARPHeader->xTargetHardwareAddress.ucBytes, xGARPTargetAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ) == 0 ) ) ) && + ( memcmp( pxTargetEndPoint->xMACAddress.ucBytes, pxARPHeader->xSenderHardwareAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ) != 0 ) ) { MACAddress_t xHardwareAddress; NetworkEndPoint_t * pxCachedEndPoint; diff --git a/source/FreeRTOS_DHCP.c b/source/FreeRTOS_DHCP.c index 19a50bafd..1effc8f2a 100644 --- a/source/FreeRTOS_DHCP.c +++ b/source/FreeRTOS_DHCP.c @@ -843,7 +843,7 @@ * using it. * @param[in] pxEndPoint The end-point that stops using the socket. */ - void prvCloseDHCPSocket( NetworkEndPoint_t * pxEndPoint ) + static void prvCloseDHCPSocket( NetworkEndPoint_t * pxEndPoint ) { if( ( EP_DHCPData.xDHCPSocket == NULL ) || ( EP_DHCPData.xDHCPSocket != xDHCPv4Socket ) ) { @@ -905,7 +905,7 @@ ( void ) FreeRTOS_setsockopt( xDHCPv4Socket, 0, FREERTOS_SO_RCVTIMEO, &( xTimeoutTime ), sizeof( TickType_t ) ); ( void ) FreeRTOS_setsockopt( xDHCPv4Socket, 0, FREERTOS_SO_SNDTIMEO, &( xTimeoutTime ), sizeof( TickType_t ) ); - memset( &xAddress, 0, sizeof( xAddress ) ); + ( void ) memset( &xAddress, 0, sizeof( xAddress ) ); xAddress.sin_family = FREERTOS_AF_INET4; xAddress.sin_len = ( uint8_t ) sizeof( xAddress ); /* Bind to the standard DHCP client port. */ diff --git a/source/FreeRTOS_DNS.c b/source/FreeRTOS_DNS.c index 899e47978..9dc3832b1 100644 --- a/source/FreeRTOS_DNS.c +++ b/source/FreeRTOS_DNS.c @@ -1082,9 +1082,7 @@ /* 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 DNSMessage_t * pxDNSMessageHeader = - ( ( const DNSMessage_t * ) - pxReceiveBuffer->pucPayloadBuffer ); + const DNSMessage_t * pxDNSMessageHeader = ( const DNSMessage_t * ) pxReceiveBuffer->pucPayloadBuffer; #if ( ipconfigUSE_MDNS == 1 ) /* _HT_ changed 'pxReceiveBuffer->sin_port' to 'usPort' */ diff --git a/source/FreeRTOS_IPv6.c b/source/FreeRTOS_IPv6.c index b021f3ef2..0e8f85ea1 100644 --- a/source/FreeRTOS_IPv6.c +++ b/source/FreeRTOS_IPv6.c @@ -62,6 +62,9 @@ const struct xIPv6_Address FreeRTOS_in6addr_any = { 0 }; /** * This variable is initialized by the system to contain the loopback IPv6 address. */ +/* MISRA Ref 8.9.1 [File scoped variables] */ +/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-89 */ +/* coverity[misra_c_2012_rule_8_9_violation] */ const struct xIPv6_Address FreeRTOS_in6addr_loopback = { { 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U } }; #if ( ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM == 1 ) diff --git a/source/FreeRTOS_RA.c b/source/FreeRTOS_RA.c index 0a5c10ee4..9f9ef8fc2 100644 --- a/source/FreeRTOS_RA.c +++ b/source/FreeRTOS_RA.c @@ -168,6 +168,10 @@ /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */ /* coverity[misra_c_2012_rule_11_3_violation] */ pxICMPPacket = ( ( ICMPPacket_IPv6_t * ) pxDescriptor->pucEthernetBuffer ); + + /* 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] */ xRASolicitationRequest = ( ( ICMPRouterSolicitation_IPv6_t * ) &( pxICMPPacket->xICMPHeaderIPv6 ) ); pxDescriptor->xDataLength = uxNeededSize; diff --git a/source/FreeRTOS_Sockets.c b/source/FreeRTOS_Sockets.c index 894a90146..ef07f4335 100644 --- a/source/FreeRTOS_Sockets.c +++ b/source/FreeRTOS_Sockets.c @@ -1926,8 +1926,17 @@ BaseType_t vSocketBind( FreeRTOS_Socket_t * pxSocket, if( pxAddress == NULL ) { pxAddress = &xAddress; - /* Put the port to zero to be assigned later. */ - pxAddress->sin_port = 0U; + /* Clear the address: */ + ( void ) memset( pxAddress, 0, sizeof( struct freertos_sockaddr ) ); + + if( pxSocket->bits.bIsIPv6 != pdFALSE_UNSIGNED ) + { + pxAddress->sin_family = FREERTOS_AF_INET6; + } + else + { + pxAddress->sin_family = FREERTOS_AF_INET; + } } } #endif /* ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND == 1 */ diff --git a/source/FreeRTOS_TCP_IP_IPv6.c b/source/FreeRTOS_TCP_IP_IPv6.c index ce1ac1304..d5a537d41 100644 --- a/source/FreeRTOS_TCP_IP_IPv6.c +++ b/source/FreeRTOS_TCP_IP_IPv6.c @@ -134,6 +134,7 @@ BaseType_t xProcessReceivedTCPPacket_IPV6( NetworkBufferDescriptor_t * pxDescrip /* coverity[misra_c_2012_rule_11_3_violation] */ const IPHeader_IPv6_t * pxIPHeader_IPv6 = ( ( IPHeader_IPv6_t * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER ] ) ); ( void ) memcpy( xRemoteIP.xIPAddress.xIP_IPv6.ucBytes, pxIPHeader_IPv6->xSourceAddress.ucBytes, sizeof( IPv6_Address_t ) ); + xRemoteIP.xIs_IPv6 = pdTRUE; /* Find the destination socket, and if not found: return a socket listing to * the destination PORT. */ diff --git a/source/include/FreeRTOSIPConfigDefaults.h b/source/include/FreeRTOSIPConfigDefaults.h index 97a0b0868..70406fa22 100644 --- a/source/include/FreeRTOSIPConfigDefaults.h +++ b/source/include/FreeRTOSIPConfigDefaults.h @@ -67,6 +67,9 @@ */ #ifndef _static + /* suppressing the use of _static as it is used for other tools like cbmc */ + /* coverity[misra_c_2012_rule_21_1_violation] */ + /* coverity[misra_c_2012_rule_21_2_violation] */ #define _static static #endif @@ -3063,8 +3066,8 @@ #endif #ifndef FreeRTOS_debug_printf - #ifdef configPRINTF - #define FreeRTOS_debug_printf( MSG ) if( ipconfigHAS_DEBUG_PRINTF ) configPRINTF( MSG ) + #if ( ( ipconfigHAS_DEBUG_PRINTF == 1 ) && defined( configPRINTF ) ) + #define FreeRTOS_debug_printf( MSG ) do { configPRINTF( MSG ); } while( ipFALSE_BOOL ) #else #define FreeRTOS_debug_printf( MSG ) do {} while( ipFALSE_BOOL ) #endif @@ -3099,8 +3102,8 @@ #endif #ifndef FreeRTOS_printf - #ifdef configPRINTF - #define FreeRTOS_printf( MSG ) if( ipconfigHAS_PRINTF ) configPRINTF( MSG ) + #if ( ( ipconfigHAS_PRINTF == 1 ) && defined( configPRINTF ) ) + #define FreeRTOS_printf( MSG ) do { configPRINTF( MSG ); } while( ipFALSE_BOOL ) #else #define FreeRTOS_printf( MSG ) do {} while( ipFALSE_BOOL ) #endif @@ -3119,7 +3122,14 @@ */ #ifndef FreeRTOS_flush_logging - #define FreeRTOS_flush_logging() if( ipconfigHAS_PRINTF || ipconfigHAS_DEBUG_PRINTF ) do {} while( ipFALSE_BOOL ) + #define FreeRTOS_flush_logging() \ + if( ipconfigHAS_PRINTF || ipconfigHAS_DEBUG_PRINTF ) \ + { \ + do {} while( ipFALSE_BOOL ); \ + } \ + else \ + { \ + } #endif /*---------------------------------------------------------------------------*/ diff --git a/source/include/FreeRTOS_IPv6_Private.h b/source/include/FreeRTOS_IPv6_Private.h index 1e5ad5d07..e00ab1956 100644 --- a/source/include/FreeRTOS_IPv6_Private.h +++ b/source/include/FreeRTOS_IPv6_Private.h @@ -57,10 +57,10 @@ /* The offset into an IP packet into which the IP data (payload) starts. */ #define ipIPv6_PAYLOAD_OFFSET ( sizeof( IPPacket_IPv6_t ) ) +/* The maximum UDP payload length. */ /* MISRA Ref 20.5.1 [Use of undef] */ /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-2051 */ /* coverity[misra_c_2012_rule_20_5_violation] */ -/* The maximum UDP payload length. */ #undef ipMAX_UDP_PAYLOAD_LENGTH #define ipMAX_UDP_PAYLOAD_LENGTH ( ( ipconfigNETWORK_MTU - ipSIZE_OF_IPv6_HEADER ) - ipSIZE_OF_UDP_HEADER ) /* The offset into a UDP packet at which the UDP data (payload) starts. */ diff --git a/source/include/FreeRTOS_IPv6_Sockets.h b/source/include/FreeRTOS_IPv6_Sockets.h index e435affc6..c9a931e67 100644 --- a/source/include/FreeRTOS_IPv6_Sockets.h +++ b/source/include/FreeRTOS_IPv6_Sockets.h @@ -90,13 +90,6 @@ */ void prv_ntop6_search_zeros( struct sNTOP6_Set * pxSet ); -/* - * Convert a string like 'fe80::8d11:cd9b:8b66:4a80' - * to a 16-byte IPv6 address - */ - const char * FreeRTOS_inet_ntop6( const void * pvSource, - char * pcDestination, - socklen_t uxSize ); /** @brief Called by pxTCPSocketLookup(), this function will check if a socket * is connected to a remote IP-address. It will be called from a loop diff --git a/source/portable/BufferManagement/BufferAllocation_2.c b/source/portable/BufferManagement/BufferAllocation_2.c index 95ae9fb69..02904bf15 100644 --- a/source/portable/BufferManagement/BufferAllocation_2.c +++ b/source/portable/BufferManagement/BufferAllocation_2.c @@ -70,14 +70,14 @@ /* Compile time assertion with zero runtime effects * it will assert on 'e' not being zero, as it tries to divide by it, * will also print the line where the error occurred in case of failure */ -/* MISRA Ref 20.10.1 [Lack of sizeof operator and compile time error checking] */ -/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-2010 */ -/* coverity[misra_c_2012_rule_20_10_violation] */ #if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES ) + /* MISRA Ref 20.10.1 [Lack of sizeof operator and compile time error checking] */ + /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-2010 */ + /* coverity[misra_c_2012_rule_20_10_violation] */ #define ASSERT_CONCAT_( a, b ) a ## b #define ASSERT_CONCAT( a, b ) ASSERT_CONCAT_( a, b ) #define STATIC_ASSERT( e ) \ - enum { ASSERT_CONCAT( assert_line_, __LINE__ ) = 1 / ( !!( e ) ) } + enum { ASSERT_CONCAT( assert_line_, __LINE__ ) = ( 1 / ( !!( e ) ) ) } STATIC_ASSERT( ipconfigETHERNET_MINIMUM_PACKET_BYTES <= baMINIMAL_BUFFER_SIZE ); #endif diff --git a/test/cbmc/patches/FreeRTOSIPConfig.h b/test/cbmc/patches/FreeRTOSIPConfig.h index 03d9164d6..f94bfb655 100644 --- a/test/cbmc/patches/FreeRTOSIPConfig.h +++ b/test/cbmc/patches/FreeRTOSIPConfig.h @@ -37,12 +37,7 @@ /* Set to 1 to print out debug messages. If ipconfigHAS_DEBUG_PRINTF is set to * 1 then FreeRTOS_debug_printf should be defined to the function used to print * out the debugging messages. */ -#ifndef ipconfigHAS_DEBUG_PRINTF - #define ipconfigHAS_DEBUG_PRINTF 0 -#endif -#if ( ipconfigHAS_DEBUG_PRINTF == 1 ) - #define FreeRTOS_debug_printf( X ) configPRINTF( X ) -#endif +#define FreeRTOS_debug_printf( X ) /* Set to 1 to print out non debugging messages, for example the output of the * FreeRTOS_netstat() command, and ping replies. If ipconfigHAS_PRINTF is set to 1