Skip to content

Commit 1157021

Browse files
Add MISRA justification for use of dynamic memory (#581)
1 parent fc71939 commit 1157021

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

MISRA.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ with ( Assuming rule 11.4 violation; with justification in point 2 ):
1414
```
1515
grep 'MISRA Ref 11.4.2' . -rI
1616
```
17+
#### Directive 4.12
18+
19+
_Ref 4.12.1_
20+
21+
- MISRA C:2012 Directive 4.12: Dynamic memory allocation shall not be used.
22+
MISRA warns against the use of dynamic memory allocation as it might
23+
lead to undefined behavior if not used properly. However, the
24+
FreeRTOS-Plus-TCP library only uses the memory allocation primitives
25+
defined by the FreeRTOS-Kernel, which are deterministic. Additionally,
26+
proper care is taken in the code to not use free'd pointers and to check
27+
the validity of malloc'd memory before it is dereferenced or used.
28+
1729
#### Rule 2.2
1830

1931
_Ref 2.2.1_

source/FreeRTOS_DNS_Callback.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@
110110
TickType_t uxIdentifier )
111111
{
112112
size_t lLength = strlen( pcHostName );
113+
114+
/* MISRA Ref 4.12.1 [Use of dynamic memory]. */
115+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#directive-412. */
116+
/* coverity[misra_c_2012_directive_4_12_violation] */
113117
DNSCallback_t * pxCallback = ( ( DNSCallback_t * ) pvPortMalloc( sizeof( *pxCallback ) + lLength ) );
114118

115119
/* Translate from ms to number of clock ticks. */

source/FreeRTOS_Sockets.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ Socket_t FreeRTOS_socket( BaseType_t xDomain,
404404
if( prvDetermineSocketSize( xDomain, xType, xProtocolCpy, &uxSocketSize ) == pdFAIL )
405405
{
406406
/* MISRA Ref 11.4.1 [Socket error and integer to pointer conversion] */
407-
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-114 */
407+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-114 */
408408
/* coverity[misra_c_2012_rule_11_4_violation] */
409409
xReturn = FREERTOS_INVALID_SOCKET;
410410
}
@@ -414,12 +414,16 @@ Socket_t FreeRTOS_socket( BaseType_t xDomain,
414414
* size depends on the type of socket: UDP sockets need less space. A
415415
* define 'pvPortMallocSocket' will used to allocate the necessary space.
416416
* By default it points to the FreeRTOS function 'pvPortMalloc()'. */
417+
418+
/* MISRA Ref 4.12.1 [Use of dynamic memory]. */
419+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#directive-412. */
420+
/* coverity[misra_c_2012_directive_4_12_violation] */
417421
pxSocket = ( ( FreeRTOS_Socket_t * ) pvPortMallocSocket( uxSocketSize ) );
418422

419423
if( pxSocket == NULL )
420424
{
421425
/* MISRA Ref 11.4.1 [Socket error and integer to pointer conversion] */
422-
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-114 */
426+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-114 */
423427
/* coverity[misra_c_2012_rule_11_4_violation] */
424428
xReturn = FREERTOS_INVALID_SOCKET;
425429
iptraceFAILED_TO_CREATE_SOCKET();
@@ -433,7 +437,7 @@ Socket_t FreeRTOS_socket( BaseType_t xDomain,
433437
vPortFreeSocket( pxSocket );
434438

435439
/* MISRA Ref 11.4.1 [Socket error and integer to pointer conversion] */
436-
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-114 */
440+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-114 */
437441
/* coverity[misra_c_2012_rule_11_4_violation] */
438442
xReturn = FREERTOS_INVALID_SOCKET;
439443
iptraceFAILED_TO_CREATE_EVENT_GROUP();
@@ -528,6 +532,9 @@ Socket_t FreeRTOS_socket( BaseType_t xDomain,
528532
{
529533
SocketSelect_t * pxSocketSet;
530534

535+
/* MISRA Ref 4.12.1 [Use of dynamic memory]. */
536+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#directive-412. */
537+
/* coverity[misra_c_2012_directive_4_12_violation] */
531538
pxSocketSet = ( ( SocketSelect_t * ) pvPortMalloc( sizeof( *pxSocketSet ) ) );
532539

533540
if( pxSocketSet != NULL )
@@ -4235,6 +4242,9 @@ void vSocketWakeUpUser( FreeRTOS_Socket_t * pxSocket )
42354242

42364243
uxSize = ( sizeof( *pxBuffer ) + uxLength ) - sizeof( pxBuffer->ucArray );
42374244

4245+
/* MISRA Ref 4.12.1 [Use of dynamic memory]. */
4246+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#directive-412. */
4247+
/* coverity[misra_c_2012_directive_4_12_violation] */
42384248
pxBuffer = ( ( StreamBuffer_t * ) pvPortMallocLarge( uxSize ) );
42394249

42404250
if( pxBuffer == NULL )

0 commit comments

Comments
 (0)