Misra rule 20.3

6.20 Standard Libraries

Moderators: misra-c, david ward

Post Reply
dvnarendra
Posts: 1
Joined: Fri Feb 03, 2017 5:36 am
Company: Medtronic India

Misra rule 20.3

Post by dvnarendra » Wed Mar 01, 2017 11:31 am

Code: Select all


#define MAX_BUF_LEN 20
#define MAX_ID_LEN    40

typedef struct
{
uint8_t  sno[MAX_BUF_LEN];
uint8_t  ID[MAX_ID_LEN ];'
} xdata_t;

typedef {
uint8_t   time;
uint32_t vol;
uint8_t ID[MAX_ID_LEN ];
}xpf_t

xdata_t  testdata;
xpf_t      pfdata
....
memcpy(pfdata.ID, testdata.ID, MAX_ID_LEN );  // Misra not compliant
......


I am using parasoft tool for checking misra compliance. for the above memcpy line I am getting the following misra non compliance

Values is "ID" passed to library function "memcpy" without being checked

I am using statically declared array. Why is it throwing this misra compliance? Can any one point how to make thsi line compliant

TIA
Narendra

dg1980
Posts: 109
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Re: Misra rule 20.3

Post by dg1980 » Thu Mar 02, 2017 9:01 am

Rule 20.3 requires parameter checking for library functions, although memcpy is not explicitly mentioned.
The behaviour of memcpy is undefined, if the memory regions overlap.
However, in your example it is provable by static analysis that this can never be the case.
Excerpt from Rule 20.3:
Demonstrate statically that the input parameters never can take invalid values
So, long story short: false positive, contact tool vendor.

misra-c
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Re: Misra rule 20.3

Post by misra-c » Thu Mar 30, 2017 10:48 am

dg1980 has correctly pointed out that there are various ways to satisfy this rule. In MISRA-C:2012 this rule has become a directive (4.11), which highlights that the MISRA-C team is not mandating exactly what a tool should be checking for. Therefore tools will vary in how they perform this check.

In this particular case it CAN BE statically demonstrated that there are no issues of undefined behaviour since the input parameters are valid.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.20 Standard Libraries”