このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
MISRA C:2012 Rule 12.2
The right hand operand of a shift operator shall lie in the range zero to one less than the width in bits of the essential type of the left hand operand
説明
ルール定義
The right hand operand of a shift operator shall lie in the range zero to one less than the width in bits of the essential type of the left hand operand 1 .
根拠
以下のステートメントを考えてみます。
var = abc << num;
abc
が 16 ビット整数の場合、num
は 0–15 (非負かつ 16 未満) の範囲になければなりません。num
が負または 16 より大きい場合、シフト演算の動作は未定義です。Polyspace 実装
Polyspace® は、シフト演算子の右側のオペランドがこのルールで定義された範囲を超えている場合に違反を報告します。右側のオペランドが変数である場合、オペランドの取り得るすべての値がこのルールで定義された範囲内にあるのでない限り、違反が報告されます。
プリプロセッサ命令により、数値リテラルでシフト演算が行われる場合、Polyspace はその数値が 64 ビット幅であると仮定します。このような数値で有効なシフト範囲は 0 から 63 の間です。次に例を示します。
#if (1 << 64) //Noncompliant //... #endif
トラブルシューティング
ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。
例
チェック情報
グループ: 式 |
カテゴリ: 必要 |
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.