How to resolve polyspace MISRA C:2012 D4.14 rule when am passing pointer as parameter to function ?
67 views (last 30 days)
Show older comments
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)
/* 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
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.
Code Prover Run-Time Error Memory Check:
Code Verification Polyspace Bug Finder Configure and Run Analysis Complete List of Polyspace Bug Finder Analysis Engine Options Run Settings
Find more on Run Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!