MISRA C:2012 Rule 18.3
The relational operators >, >=, < and <= shall not be applied to expressions of pointer type except where they point into the same object
Description
Rule Definition
The relational operators >
, >=
,
<
, and <=
shall not be applied
to expressions of pointer type except where they point into the same
object1
.
Rationale
If two pointers do not point to the same object, comparisons between the pointers produces undefined behavior.
You can address the element beyond the end of an array, but you cannot access this element.
Polyspace Implementation
Polyspace® reports a violation of this rule when you compare pointers that are
null or that point to elements in different arrays. The relational operators for the
comparison are >
, <
,
>=
, and <=
.
If one of the pointers in the comparison operation is unknown to Polyspace in the current analysis, a violation of this rule is not reported. For
example, in this code, Polyspace cannot determine the underlying objects of arg_ptr
and temp
:
extern int *getPtr(); void foo(int *arg_ptr) { int diff, diff2; int c_str[50]; int *temp = getPtr(); if(c_str < arg_ptr) {/**/} //No violation if(c_str < tmp) {/**/} //No violation }
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Pointers and Arrays |
Category: Required |
AGC Category: Required |
Version History
See Also
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.