1
0
mirror of https://github.com/FreeRTOS/FreeRTOS-Plus-TCP synced 2025-10-24 20:29:40 +08:00

Misra fix or suppress remaining violations (#529)

* Fix Remaning misra issues

* Suppress rule 8.6

* Fix/Suppress more misra violations

* Style: for formatting

* Style: fix formatting

* Style: fix spelling

* Fix Rule 11.1

* Fix undeteced suppressions

* Enable 32 bits

* Fix more misra leftover violations

* Add justification for a missed violation

* Fix comment for rule 8.13

* Fix comment

* fix misra comments

* Update MISRA.md

Co-authored-by: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com>

* Update source/FreeRTOS_Sockets.c

Co-authored-by: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com>

* Update source/portable/BufferManagement/BufferAllocation_2.c

Co-authored-by: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com>

* Update MISRA.md

Co-authored-by: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com>

* Update MISRA.md

Co-authored-by: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com>

* Suppress Rule 8.9

* Fix build error

* fix build error

* Fix coverity supression bugs

Co-authored-by: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com>
This commit is contained in:
alfred gedeon
2022-08-08 19:45:09 +02:00
committed by GitHub
parent f8c72717cd
commit 4ac10c84a3
10 changed files with 169 additions and 66 deletions

View File

@@ -31,6 +31,35 @@ _Ref 8.9.1_
order of execution, some variables have file scope definitions rather
than function scope.
#### Rule 8.13
_Ref 8.13.1_
- MISRA C-2012 Rule 8.13 Parameter passed is never used, should be declared as
const. The argument passed to the `prvIPTask` function is left unused which is
considered as the variable not being used and thus warranting the use of `const`.
However, the FreeRTOS-kernel function `xTaskCreate` expects a function signature
of type `void vSomeFunction( void * pvArgs )`. To satisfy that requirement, the
function signature of `prvIPTask` does not have a `const` qualifier in the
parameter signature.
#### Rule 10.5
_Ref 10.5.1_
- MISRA C-2012 Rule 10.5 Converting from an unsigned to an enum type. The
operation is safe to perform in that case, as we are using a generic API
to send and receive data, in that case the exact data sent it is received
#### Rule 11.1
_Ref 11.1.1_
- MISRA C-2012 Rule 11.1 Converting from a void pointer to a function pointer.
The `FreeRTOS_setsockopt` API allows users to configure sockets by setting
various options. In order to do so, the function must accept one parameter
which, based on the option value, can be casted to the corresponding socket
field. To that end, that parameter is of `void *` type to accommodate all values.
The caller of the API is responsible for providing correct function pointer to the
API. Thus, this violation can be safely suppressed.
#### Rule 11.3
_Ref 11.3.1_
@@ -107,16 +136,6 @@ _Ref 14.3.1_
- MISRA C-2012 Rule 14.3 False positive as the value might be changed
depending on the conditionally compiled code
#### Rule 21.6
_Ref 21.6.1_
- MISRA C-2012 Rule 21.6 warns about the use of standard library input/output
functions as they might have implementation defined or undefined
behaviour. The function `snprintf` is used to insert information in a
logging string. This is only used in a utility function which aids in
debugging and is not part of the 'core' code governing the
functionality of the TCP/IP stack.
#### Rule 17.2
_Ref 17.2.1_
@@ -128,6 +147,17 @@ _Ref 17.2.1_
have a secondary child socket thereby limiting the number of recursive
calls to one.
#### Rule 20.5
_Ref 20.5.1_
- MISRA C-2012 Rule 20.5 warns against the use of #undef.
FreeRTOS-Plus-TCP allows its users to set some configuration macros
to modify the behavior/performance of the library according to their
needs. However, the macros values must be within certain bounds.
To achieve that, if the macro values lie outside of the bounds, they
are undefined using `#undef` before being redefined to a proper
value.
#### Rule 20.10
_Ref 20.10.1_
@@ -135,3 +165,14 @@ _Ref 20.10.1_
However, in this case, it must be used to support compile time
assertions in case the preprocessor does not suppport sizeof. This
operation (assert) has no runtime execution.
#### Rule 21.6
_Ref 21.6.1_
- MISRA C-2012 Rule 21.6 warns about the use of standard library input/output
functions as they might have implementation defined or undefined
behaviour. The function `snprintf` is used to insert information in a
logging string. This is only used in a utility function which aids in
debugging and is not part of the 'core' code governing the
functionality of the TCP/IP stack.

View File

@@ -122,7 +122,7 @@ static void prvProcessIPEventsAndTimers( void );
* from the network hardware drivers and tasks that are using sockets. It also
* maintains a set of protocol timers.
*/
static void prvIPTask( const void * pvParameters );
static void prvIPTask( void * pvParameters );
/*
* Called when new data is available from the network interface.
@@ -229,7 +229,11 @@ static BaseType_t xIPTaskInitialised = pdFALSE;
*
* @param[in] pvParameters: Not used.
*/
static void prvIPTask( const void * pvParameters )
/* MISRA Ref 8.13.1 [Not decorating a pointer to const parameter with const] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-813 */
/* coverity[misra_c_2012_rule_8_13_violation] */
static void prvIPTask( void * pvParameters )
{
/* Just to prevent compiler warnings about unused parameters. */
( void ) pvParameters;
@@ -393,6 +397,8 @@ static void prvProcessIPEventsAndTimers( void )
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-116 */
/* coverity[misra_c_2012_rule_11_6_violation] */
uxState = ( uintptr_t ) xReceivedEvent.pvData;
/* MISRA Ref 10.5.1 [DHCP events Enum] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-105 */
/* coverity[misra_c_2012_rule_10_5_violation] */
eState = ( eDHCPState_t ) uxState;
@@ -708,6 +714,7 @@ void * FreeRTOS_GetUDPPayloadBuffer( size_t uxRequestedSizeBytes,
* @return pdPASS if the task was successfully created and added to a ready
* list, otherwise an error code defined in the file projdefs.h
*/
/* coverity[single_use] */
BaseType_t FreeRTOS_IPInit( const uint8_t ucIPAddress[ ipIP_ADDRESS_LENGTH_BYTES ],
const uint8_t ucNetMask[ ipIP_ADDRESS_LENGTH_BYTES ],
const uint8_t ucGatewayAddress[ ipIP_ADDRESS_LENGTH_BYTES ],
@@ -1610,6 +1617,10 @@ static eFrameProcessingResult_t prvProcessIPPacket( IPPacket_t * pxIPPacket,
/* Check if the IP headers are acceptable and if it has our destination. */
eReturn = prvAllowIPPacket( pxIPPacket, pxNetworkBuffer, uxHeaderLength );
/* MISRA Ref 14.3.1 [Configuration dependent invariant] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-143 */
/* coverity[misra_c_2012_rule_14_3_violation] */
/* coverity[cond_const] */
if( eReturn == eProcessBuffer )
{
/* Are there IP-options. */
@@ -1649,6 +1660,7 @@ static eFrameProcessingResult_t prvProcessIPPacket( IPPacket_t * pxIPPacket,
/* MISRA Ref 14.3.1 [Configuration dependent invariant] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-143 */
/* coverity[misra_c_2012_rule_14_3_violation] */
/* coverity[const] */
if( eReturn != eReleaseBuffer )
{
/* Add the IP and MAC addresses to the ARP table if they are not

View File

@@ -78,6 +78,7 @@
/* 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] */
/* coverity[single_use] */
static BaseType_t xCallEventHook = pdFALSE;
#endif
@@ -974,10 +975,10 @@ uint16_t usGenerateChecksum( uint16_t usSum,
/* coverity[value_overwrite] */
xSum.u32 = ( uint32_t ) xSum.u16[ 0 ] + xSum.u16[ 1 ];
/* coverity[value_overwrite] */
/* MISRA Ref 2.2.1 [Unions and dead code] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-22 */
/* coverity[misra_c_2012_rule_2_2_violation] */
/* coverity[value_overwrite] */
xSum.u32 = ( uint32_t ) xSum.u16[ 0 ] + xSum.u16[ 1 ];
if( ( uxAlignBits & 1U ) != 0U )

View File

@@ -1533,6 +1533,9 @@ BaseType_t FreeRTOS_closesocket( Socket_t xSocket )
*
* @return Returns NULL, always.
*/
/* MISRA Ref 17.2.1 [Sockets and limited recursion] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-172 */
/* coverity[misra_c_2012_rule_17_2_violation] */
void * vSocketClose( FreeRTOS_Socket_t * pxSocket )
{
NetworkBufferDescriptor_t * pxNetworkBuffer;
@@ -1645,6 +1648,10 @@ void * vSocketClose( FreeRTOS_Socket_t * pxSocket )
*
* @param[in] pxSocketToDelete: The socket being closed.
*/
/* MISRA Ref 17.2.1 [Sockets and limited recursion] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-172 */
/* coverity[misra_c_2012_rule_17_2_violation] */
/* coverity[recursive_step] */
static void prvTCPSetSocketCount( FreeRTOS_Socket_t const * pxSocketToDelete )
{
const ListItem_t * pxIterator;
@@ -1675,6 +1682,7 @@ void * vSocketClose( FreeRTOS_Socket_t * pxSocket )
/* MISRA Ref 17.2.1 [Sockets and limited recursion] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-172 */
/* coverity[misra_c_2012_rule_17_2_violation] */
/* coverity[recursive_step] */
( void ) vSocketClose( pxOtherSocket );
}
}
@@ -1958,7 +1966,12 @@ BaseType_t FreeRTOS_setsockopt( Socket_t xSocket,
/* MISRA Ref 11.8.1 [Function pointer and use of const pointer] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-118 */
/* MISRA Ref 11.1.1 [ Conversion between pointer to
* a function and another type ] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-111 */
/* coverity[misra_c_2012_rule_11_8_violation] */
/* coverity[misra_c_2012_rule_11_1_violation] */
pxSocket->pxUserWakeCallback = ( SocketWakeupCallback_t ) pvOptionValue;
xReturn = 0;
break;

View File

@@ -65,9 +65,6 @@
#if ipconfigUSE_TCP == 1
/* 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] */
/** @brief When closing a socket an event is posted to the Network Event Queue.
* If the queue is full, then the event is not posted and the socket
@@ -75,6 +72,9 @@
* track of any socket which needs to be closed. This variable can be
* accessed by the IP task only. Thus, preventing any race condition.
*/
/* 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] */
static FreeRTOS_Socket_t * xPreviousSocket = NULL;
/*
@@ -107,6 +107,7 @@
*
* @param[in] pxSocket: The socket to be checked.
*/
/* coverity[single_use] */
void vSocketCloseNextTime( FreeRTOS_Socket_t * pxSocket )
{
if( ( xPreviousSocket != NULL ) && ( xPreviousSocket != pxSocket ) )

View File

@@ -168,7 +168,7 @@
* stack. FreeRTOS includes optional stack overflow detection, see:
* http://www.freertos.org/Stacks-and-stack-overflow-checking.html */
#ifndef ipconfigIP_TASK_STACK_SIZE_WORDS
#define ipconfigIP_TASK_STACK_SIZE_WORDS ( configMINIMAL_STACK_SIZE * 5 )
#define ipconfigIP_TASK_STACK_SIZE_WORDS ( configMINIMAL_STACK_SIZE * 5U )
#endif
/* Include all API's and code that is needed for the TCP protocol.
@@ -259,6 +259,9 @@
#ifndef FreeRTOS_debug_printf
#define FreeRTOS_debug_printf( MSG ) do {} while( ipFALSE_BOOL )
/* 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] */
#undef ipconfigHAS_DEBUG_PRINTF
#define ipconfigHAS_DEBUG_PRINTF 0
#endif
@@ -282,6 +285,9 @@
#ifndef FreeRTOS_printf
#define FreeRTOS_printf( MSG ) do {} while( ipFALSE_BOOL )
/* 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] */
#undef ipconfigHAS_PRINTF
#define ipconfigHAS_PRINTF 0
#endif
@@ -611,6 +617,9 @@
#else
/* A sanity check to avoid a possible overflow of size_t. */
#if ipconfigNETWORK_MTU > ( SIZE_MAX >> 1 )
/* 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] */
#undef ipconfigNETWORK_MTU
#define ipconfigNETWORK_MTU ( SIZE_MAX >> 1 )
#endif

View File

@@ -215,13 +215,15 @@ uint8_t * pucGetNetworkBuffer( size_t * pxRequestedSizeBytes )
void vReleaseNetworkBuffer( uint8_t * pucEthernetBuffer )
{
uint8_t * pucEthernetBufferCopy = pucEthernetBuffer;
/* There is space before the Ethernet buffer in which a pointer to the
* network buffer that references this Ethernet buffer is stored. Remove the
* space before freeing the buffer. */
if( pucEthernetBuffer != NULL )
if( pucEthernetBufferCopy != NULL )
{
pucEthernetBuffer -= ipBUFFER_PADDING;
vPortFree( ( void * ) pucEthernetBuffer );
pucEthernetBufferCopy -= ipBUFFER_PADDING;
vPortFree( ( void * ) pucEthernetBufferCopy );
}
}
/*-----------------------------------------------------------*/
@@ -232,8 +234,9 @@ NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedS
NetworkBufferDescriptor_t * pxReturn = NULL;
size_t uxCount;
size_t uxMaxAllowedBytes = ( SIZE_MAX >> 1 );
size_t xRequestedSizeBytesCopy = xRequestedSizeBytes;
if( ( xRequestedSizeBytes <= uxMaxAllowedBytes ) && ( xNetworkBufferSemaphore != NULL ) )
if( ( xRequestedSizeBytesCopy <= uxMaxAllowedBytes ) && ( xNetworkBufferSemaphore != NULL ) )
{
/* If there is a semaphore available, there is a network buffer available. */
if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
@@ -257,27 +260,27 @@ NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedS
/* Allocate storage of exactly the requested size to the buffer. */
configASSERT( pxReturn->pucEthernetBuffer == NULL );
if( xRequestedSizeBytes > 0U )
if( xRequestedSizeBytesCopy > 0U )
{
if( ( xRequestedSizeBytes < ( size_t ) baMINIMAL_BUFFER_SIZE ) )
if( ( xRequestedSizeBytesCopy < ( size_t ) baMINIMAL_BUFFER_SIZE ) )
{
/* ARP packets can replace application packets, so the storage must be
* at least large enough to hold an ARP. */
xRequestedSizeBytes = baMINIMAL_BUFFER_SIZE;
xRequestedSizeBytesCopy = baMINIMAL_BUFFER_SIZE;
}
/* Add 2 bytes to xRequestedSizeBytes and round up xRequestedSizeBytes
/* Add 2 bytes to xRequestedSizeBytesCopy and round up xRequestedSizeBytesCopy
* to the nearest multiple of N bytes, where N equals 'sizeof( size_t )'. */
xRequestedSizeBytes += 2U;
xRequestedSizeBytesCopy += 2U;
if( ( xRequestedSizeBytes & ( sizeof( size_t ) - 1U ) ) != 0U )
if( ( xRequestedSizeBytesCopy & ( sizeof( size_t ) - 1U ) ) != 0U )
{
xRequestedSizeBytes = ( xRequestedSizeBytes | ( sizeof( size_t ) - 1U ) ) + 1U;
xRequestedSizeBytesCopy = ( xRequestedSizeBytesCopy | ( sizeof( size_t ) - 1U ) ) + 1U;
}
/* Extra space is obtained so a pointer to the network buffer can
* be stored at the beginning of the buffer. */
pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );
pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytesCopy + ipBUFFER_PADDING );
if( pxReturn->pucEthernetBuffer == NULL )
{
@@ -293,12 +296,15 @@ NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedS
* buffer storage area, then move the buffer pointer on past the
* stored pointer so the pointer value is not overwritten by the
* application when the buffer is used. */
/* 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] */
*( ( NetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;
pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;
/* Store the actual size of the allocated buffer, which may be
* greater than the original requested size. */
pxReturn->xDataLength = xRequestedSizeBytes;
pxReturn->xDataLength = xRequestedSizeBytesCopy;
#if ( ipconfigUSE_LINKED_RX_MESSAGES != 0 )
{
@@ -393,30 +399,36 @@ NetworkBufferDescriptor_t * pxResizeNetworkBufferWithDescriptor( NetworkBufferDe
{
size_t xOriginalLength;
uint8_t * pucBuffer;
size_t uxSizeBytes = xNewSizeBytes;
NetworkBufferDescriptor_t * pxNetworkBufferCopy = pxNetworkBuffer;
xOriginalLength = pxNetworkBuffer->xDataLength + ipBUFFER_PADDING;
xNewSizeBytes = xNewSizeBytes + ipBUFFER_PADDING;
pucBuffer = pucGetNetworkBuffer( &( xNewSizeBytes ) );
xOriginalLength = pxNetworkBufferCopy->xDataLength + ipBUFFER_PADDING;
uxSizeBytes = uxSizeBytes + ipBUFFER_PADDING;
pucBuffer = pucGetNetworkBuffer( &( uxSizeBytes ) );
if( pucBuffer == NULL )
{
/* In case the allocation fails, return NULL. */
pxNetworkBuffer = NULL;
pxNetworkBufferCopy = NULL;
}
else
{
pxNetworkBuffer->xDataLength = xNewSizeBytes;
pxNetworkBufferCopy->xDataLength = uxSizeBytes;
if( xNewSizeBytes > xOriginalLength )
if( uxSizeBytes > xOriginalLength )
{
xNewSizeBytes = xOriginalLength;
uxSizeBytes = xOriginalLength;
}
( void ) memcpy( pucBuffer - ipBUFFER_PADDING, pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING, xNewSizeBytes );
vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );
pxNetworkBuffer->pucEthernetBuffer = pucBuffer;
( void ) memcpy( pucBuffer - ipBUFFER_PADDING,
pxNetworkBufferCopy->pucEthernetBuffer - ipBUFFER_PADDING,
uxSizeBytes );
vReleaseNetworkBuffer( pxNetworkBufferCopy->pucEthernetBuffer );
pxNetworkBufferCopy->pucEthernetBuffer = pucBuffer;
}
return pxNetworkBuffer;
return pxNetworkBufferCopy;
}

View File

@@ -8,6 +8,7 @@ project ( "FreeRTOS+TCP Static analysis"
# Allow the project to be organized into folders.
set_property( GLOBAL PROPERTY USE_FOLDERS ON )
set(CMAKE_VERBOSE_MAKEFILE ON)
# Use C90.
set( CMAKE_C_STANDARD 90 )

View File

@@ -105,7 +105,7 @@
* number generation is performed via this macro to allow applications to use their
* own random number generation method. For example, it might be possible to
* generate a random number by sampling noise on an analogue input. */
extern uint32_t ulRand();
extern uint32_t ulRand( void );
#define ipconfigRAND32() ulRand()
/* If ipconfigUSE_NETWORK_EVENT_HOOK is set to 1 then FreeRTOS+TCP will call the

View File

@@ -53,6 +53,19 @@
{
deviation: "Rule 15.4",
reason: "Multiple breaks in a do { ... } while ( 0 ); block are used to make the code easier to read and more clean than using multiple nested if-else statements."
},
{
deviation: "Rule 11.5",
reason: "Conversion from pointer to void into pointer to object
all uses are checked and tested not to cause misalignment, pointers
are switched back to their original type before they are accessed"
},
{
deviation: "Rule 8.6",
reason: "We use function callbacks to be defined by the application
writer, we could not provide definitions under the risk of
multiple definitions"
}
]
}