メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

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

説明

ルール定義

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
}
これらのポインターが関係する比較は、このルールの違反として報告されません。

トラブルシューティング

ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。

すべて展開する

void f1(void){
    int arr1[10];
    int arr2[10];
    int *ptr1 = arr1;

    if(ptr1 < arr2){}    /* Non-compliant */
    if(ptr1 < arr1){}    /* Compliant */
}

この例では、ptr1arr1 を指すポインターです。ルール 18.3 に準拠するには、ptr1arr1 のみを比較できます。したがって、ptr1arr2 の比較は準拠しません。

struct limits{
  int lower_bound;
  int upper_bound;
};

void func2(void){
    struct limits lim_1 = { 2, 5 };
    struct limits lim_2 = { 10, 5 };

    if(&lim_1.lower_bound <= &lim_2.upper_bound){}  /* Non-compliant *
    if(&lim_1.lower_bound <= &lim_1.upper_bound){}  /* Compliant */
}

この例では、2 つの limits 構造体である lim1 および lim2 を定義し、要素を比較します。ルール 18.3 に準拠するには、構造体内の構造体要素のみ比較できます。最初の比較では lim1lower_boundlim2upper_bound を比較しています。この比較は、lim_1.lower_bound および lim_2.upper_bound が 2 つの異なる構造体の要素であるため、準拠しません。

チェック情報

グループ: ポインターと配列
カテゴリ: 必要
AGC カテゴリ: 必要

バージョン履歴

すべて展開する


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.