![]() |
![]() |
2773 | ![]() |
||||
![]() | |||||||
| Pointers | |||||||
Dataflow analysis has identified a code construct which is suspicious. Examination of the context indicates that an operation is being performed which could sometimes compare or subtract the value of two pointers which address different objects. The code may be entirely safe but further investigation is advisable. Behaviour is undefined if an attempt is made to subtract pointers which do not address elements of the same array or to compare (with a relational operator) pointers which do not point to the same struct, union or array. For example, in the code below, the pointer
p1 will always address buf1. The pointer p2 will address buf1 but could address buf2 if the value of the function parameter n is zero or negative.
/*PRQA S 488,490,2017,2983,2991,2995,3120,3132,3199,3203,3227,3355,3358,3408,3447 ++*/
extern int buf1[10];
extern int buf2[10];
extern void f2773(int n)
{
int * p1;
int * p2;
long d;
int i;
p1 = buf1;
p2 = buf2;
for (i = 0; i < n; ++i)
{
p2 = &buf1[5];
}
d = p2 - p1; /* 2773 */
if (p2 > p1) /* 2773 */
{
}
}
No MISRA C:2012 Rules applicable to message 2773
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index | Contents |