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
R2024a 以降
説明
ルール定義
The relational operators >, >=, <, and <= shall not be applied to expressions of pointer type except where they point into the same object 1
.
根拠
2 つのポインターが同じオブジェクトを指さない場合、ポインター間の比較は未定義の動作を発生させます。
配列の終わりを超える要素を指すことはできますが、この要素にはアクセスできません。
Polyspace 実装
null ポインターまたは別の配列内の要素を指しているポインターを比較すると、Polyspace® はこのルールに対する違反を報告します。比較の関係演算子は >、<、>=、および <= です。
現行の解析で、比較演算で使用されるポインターのいずれかが Polyspace に対して未知である場合、このルールの違反は報告されません。たとえば次のコードでは、Polyspace は arg_ptr および 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
}トラブルシューティング
ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。
例
チェック情報
| グループ: ポインターと配列 |
| カテゴリ: 必要 |
| AGC カテゴリ: 必要 |
バージョン履歴
R2024a で導入
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.