MATLAB Answers

How to resolve polyspace MISRA C:2012 D4.14 rule when am passing pointer as parameter to function ?

54 views (last 30 days)
sai akshay
sai akshay on 11 Sep 2019
Answered: Luke Halberstadt on 26 Dec 2019
this is my fuction FCM_CopyDTCStatusByDTCNumber (uint32 DTC_Number, uint8* data_ptr) and for this its throwing
MISRA C:2012 D4.14 (Required)
The validity of values received from external sources shall be checked.
Array index is from an unsecure source. Index may be negative or too big
and am checking pointer is not null as
void FCM_CopyDTCStatusByDTCNumber (uint32 DTC_Number, uint8* data_ptr)
{
uint16 DTC_Index;
/* Find the DTC Index */
DTC_Index = FCM_FindDTCIndex(DTC_Number);
/* Loop till the all DTCs */
if (DTC_Index < FCM_MAXNUM_DTCS)
{
if(data_ptr!= NULL_PTR) <------
{
/* Copy the DTC Number to buffer */
*data_ptr++ = (uint8)(FCM_DTCInfo[DTC_Index].Fault_Id >> FCM_SIXTEEN);
*data_ptr++ = (uint8)(FCM_DTCInfo[DTC_Index].Fault_Id >> FCM_EIGHT);
*data_ptr++ = (uint8)(FCM_DTCInfo[DTC_Index].Fault_Id);
}
check the arrow mark IN ABOVE CODE i will resolve pointer is not NULL

Answers (1)

Luke Halberstadt
Luke Halberstadt on 26 Dec 2019
Even though you are checking that the input parameter is not NULL, it is still possible for the dereferenced "data_ptr" to traverse beyond the end of the valid allocated memory.
Please note that verifying whether or not you stay within the bounds of the provided memory cannot be checked within "FCM_CopyDTCStatusByDTCNumber" unless broader context is given. This could be in the form of the code that calls this function or you could use external constraints:
As described on the constraint page, you cannot constrain function inputs when using Bug Finder. This means that even after you provide constraints for the memory pointed to by "data_ptr", you will still see 4.14 violations in Bug Finder and will need to justify these via comments.
Code Prover ignores directive 4.14, so it will never show 4.14 violations, but it does check for valid memory access, taking constraints into account.
MISRA Checkers:
Code Prover Run-Time Error Memory Check:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!