MISRA C:2023 Rule 18.3
The relational operators >, >=, < and <= shall not be applied to expressions of pointer type except where they point into the same object
Since R2024a
Description
Rule Definition
The relational operators >
, >=
,
<
, and <=
shall not be applied to
expressions of pointer type except where they point into the same
object.
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
Introduced in R2024a