MISRA C++:2008 Rule 5-19-1
Evaluation of constant unsigned integer expressions should not lead to wrap-around
説明
ルール定義
Evaluation of constant unsigned integer expressions should not lead to wrap-around. 1
根拠
符号なし整数式は厳密にはオーバーフローしませんが、代わりにラップアラウンドが発生します。実行時にモジュロ演算を使用する利点は十分にあるかもしれませんが、コンパイル時に意図的に使用する利点はほとんどなく、ロジック エラーとなる可能性があります。
Polyspace 実装
Polyspace® は、ラップアラウンドを発生させる可能性のある定数式にフラグを設定します。
コンパイラによって、コンパイル時定数の定義方法は異なります。次のコードに含まれる c+1u は、GCC コンパイラでは定数式定数と見なされますが、標準 C++ コンパイラでは定数式として見なされません。
const uint16_t c = 0xffffu; uint16_t y = c + 1u;
トラブルシューティング
ルール違反が想定されるものの、Polyspace から報告されない場合は、コーディング規約違反が想定どおりに表示されない理由の診断を参照してください。
例
チェック情報
| グループ: Expressions |
| カテゴリ: 推奨 |
バージョン履歴
R2013b で導入
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.