1# MISRA Compliance 2 3The FreeRTOS-Plus-TCP library files conform to the [MISRA C:2012](https://www.misra.org.uk/MISRAHome/MISRAC2012/tabid/196/Default.aspx) 4guidelines, with the deviations listed below. Compliance is checked with Coverity static analysis. 5Since the FreeRTOS-Plus-TCP library is designed for small-embedded devices, it needs to have a very small memory footprint and has to 6be efficient. To achieve that and to increase the performace of the IP-stack, it deviates from some MISRA rules. 7The specific deviations, suppressed inline, are listed below. 8 9Additionally, [MISRA configuration file](https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/test/Coverity/coverity_misra.config) contains the project wide deviations. 10 11### Suppressed with Coverity Comments 12To find the violation references in the source files run grep on the source code 13with ( Assuming rule 11.4 violation; with justification in point 2 ): 14``` 15grep 'MISRA Ref 11.4.2' . -rI 16``` 17#### Rule 2.2 18 19_Ref 2.2.1_ 20 21- MISRA C-2012 Rule 2.2 Unions are used for checksum computation to speed up the 22 process by utilizing the full length of registers (32-bits). After this, 23 the 16-bit union members are used to then compute the final checksum. 24 Doing this is considered as 'overwriting the variable' by Coverity. 25 Thus, it marks some statements as dead code. This is a false positive. 26 27#### Rule 8.9 28_Ref 8.9.1_ 29 30- MISRA C-2012 Rule 8.9 For unit-tests to be repeatable and independent of the 31 order of execution, some variables have file scope definitions rather 32 than function scope. 33 34#### Rule 8.13 35_Ref 8.13.1_ 36 37- MISRA C-2012 Rule 8.13 Parameter passed is never used, should be declared as 38 const. The argument passed to the `prvIPTask` function is left unused which is 39 considered as the variable not being used and thus warranting the use of `const`. 40 However, the FreeRTOS-kernel function `xTaskCreate` expects a function signature 41 of type `void vSomeFunction( void * pvArgs )`. To satisfy that requirement, the 42 function signature of `prvIPTask` does not have a `const` qualifier in the 43 parameter signature. 44 45#### Rule 10.5 46_Ref 10.5.1_ 47 48- MISRA C-2012 Rule 10.5 Converting from an unsigned to an enum type. The 49 operation is safe to perform in that case, as we are using a generic API 50 to send and receive data, in that case the exact data sent it is received 51 52#### Rule 11.1 53_Ref 11.1.1_ 54 55- MISRA C-2012 Rule 11.1 Converting from a void pointer to a function pointer. 56 The `FreeRTOS_setsockopt` API allows users to configure sockets by setting 57 various options. In order to do so, the function must accept one parameter 58 which, based on the option value, can be casted to the corresponding socket 59 field. To that end, that parameter is of `void *` type to accommodate all values. 60 The caller of the API is responsible for providing correct function pointer to the 61 API. Thus, this violation can be safely suppressed. 62 63#### Rule 11.3 64_Ref 11.3.1_ 65 66- MISRA C-2012 Rule 11.3 The data received/sent by the IP stack is represent as a 67 byte stream. This byte stream needs to be casted to various data 68 structures to access certain fields of the packet. However, when casting 69 a byte stream to a structure, MISRA warns us that it can lead to 70 unaligned access. But, in case of FreeRTOS+TCP, the buffer in which the 71 packets are stored are always aligned to a 4 byte word boundary with an 72 offset of 2 bytes. The reason for this 2 byte offset is that the 73 ethernet header is of 14 (12 + 2) bytes. Thus, everything except the 74 ethernet header is properly aligned. There is one alignment exception, 75 which is the sender protocol address in the ARP Header. To combat that, 76 the sender protocol address field is declared as an array of 4 bytes 77 instead of a `uint32_t`. 78 More details can be found [here](https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/pull/512#pullrequestreview-1035211706). 79 80#### Rule 11.4 81_Ref 11.4.1_ 82 83- MISRA c-2012 Rule 11.4 Warns about conversion between a pointer and an integer. 84 Whenever a socket is created using the `FreeRTOS_Socket` API, either a 85 valid socket (a valid non-NULL pointer) is returned; or 86 `FREERTOS_INVALID_SOCKET` is returned (which is essentially ~0U) to 87 depict an error in the socket creation process. This conversion from ~0U 88 to a pointer is used to convey the error to various functions. If the 89 pointer is equal to `FREERTOS_INVALID_SOCKET`, then it is not 90 dereferenced. Thus, this violation can be safely suppressed. 91 92_Ref 11.4.2_ 93 94- MISRA Rule 11.4 The following statement may trigger a: 95 warning: cast increases required alignment of target type [-Wcast-align]. 96 It has been programatically checked that the pointer is well aligned 97 before this point. 98 99_Ref 11.4.3_ 100 101- MISRA Rule 11.4 warns about casting pointer to an integer and vice versa. 102 Here, the poiner to the starting byte of the packet is cast to an 103 integer which is then used to see whether the pointer is well 104 aligned or not. It is not used to access any pointer values. Thus, this 105 violation can be safely suppressed. 106 107#### Rule 11.6 108_Ref 11.6.1_ 109 110- When sending and receiving a DHCP event to the IP-stack, the events are 111 converted to a void pointer and sent to the IP-task. The function used 112 to send the events handles various events for the IP-task and thus only 113 accepts void pointers. The IP-task converts the void pointer back to 114 the original event. Thus, this rule can be safely suppressed. 115 116_Ref 11.6.2_ 117 118- MISRA Rule 11.6 `uintptr_t` is guaranteed by the implementation to fit a 119 pointer size of the platform. The pointer has to be moved backward by a 120 constant offset to get to a 'hidden' pointer which is not available for 121 the user to use. This conversion is done to achieve that while avoiding 122 pointer arithmetic. 123 124#### Rule 11.8 125_Ref 11.8.1_ 126 127- MISRA c-2012 Rule 11.8 warns about removing the `const` qualifier when 128 assigning one value to another. In this case however, a function 129 pointer is being copied. It doesn't make sense in case of function 130 pointers for the pointee to be const or mutable. Thus, this rule is 131 safe to suppress. 1321 133#### Rule 14.3 134_Ref 14.3.1_ 135 136- MISRA C-2012 Rule 14.3 False positive as the value might be changed 137 depending on the conditionally compiled code 138 139#### Rule 17.2 140_Ref 17.2.1_ 141 142- MISRA C-2012 Rule 17.2 warns about using recursion in software as that can have 143 severe implications on the stack usage and can lead to a serious issue. 144 In this case however, the number of recursions are limited by design. 145 Any socket spawned (child) by a socket in listening state (parent) 146 cannot be in listening state. Thus it is not possible for the child to 147 have a secondary child socket thereby limiting the number of recursive 148 calls to one. 149 150#### Rule 20.5 151_Ref 20.5.1_ 152 153- MISRA C-2012 Rule 20.5 warns against the use of #undef. 154 FreeRTOS-Plus-TCP allows its users to set some configuration macros 155 to modify the behavior/performance of the library according to their 156 needs. However, the macros values must be within certain bounds. 157 To achieve that, if the macro values lie outside of the bounds, they 158 are undefined using `#undef` before being redefined to a proper 159 value. 160 161#### Rule 20.10 162_Ref 20.10.1_ 163 164- MISRA C-2012 Rule 20.10 warns against the use of ## concatination operator. 165 However, in this case, it must be used to support compile time 166 assertions in case the preprocessor does not suppport sizeof. This 167 operation (assert) has no runtime execution. 168 169#### Rule 21.6 170_Ref 21.6.1_ 171 172- MISRA C-2012 Rule 21.6 warns about the use of standard library input/output 173 functions as they might have implementation defined or undefined 174 behaviour. The function `snprintf` is used to insert information in a 175 logging string. This is only used in a utility function which aids in 176 debugging and is not part of the 'core' code governing the 177 functionality of the TCP/IP stack. 178 179