C and C++


LANG.STRUCT.SE.SIZEOF : 副作用を含むsizeof

要旨

sizeof 演算子のオペランドに副作用があるか、それが評価されたとき副作用がある可能性があります。

例外:sizeof(volLval) は、もし volLvalvolatile 修飾された左辺値 (かつ可変長配列でない)の場合はこのクラスのワーニングを検出しません。

このチェックの目的は以下の通りです。

プロパティ

クラス名 Side Effects in sizeof
日本語クラス名 副作用を含むsizeof
クラス分類 スタイル (style)
ニーモニック LANG.STRUCT.SE.SIZEOF
カテゴリー
MisraC2023 MisraC2023:13.6 The operand of the sizeof operator shall not contain any expression which has potential side effects
Misra2012 Misra2012:13.6 The operand of the sizeof operator shall not contain any expression which has potential side effects
Misra2004 Misra2004:12.3 The sizeof operator shall not be used on expressions that contain side effects
AUTOSARC++14 AUTOSARC++14:M5-3-4 Evaluation of the operand to the sizeof operator shall not contain side effects.
MisraC++2008 MisraC++2008:5-3-4 Evaluation of the operand to the sizeof operator shall not contain side effects.
CERT-C CERT-C:EXP44-C Do not rely on side effects in operands to sizeof, _Alignof, or _Generic
CERT-CPP CERT-CPP:EXP52-CPP Do not rely on side effects in unevaluated operands
JSF++ JSF++:166 The sizeof operator will not be used on expressions that contain side effects.
JPL JPL:19 Do not use expressions with side effects.
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Side Effects in sizeof"
注:非正規化された AST を継続して使用した場合、使用ディスク容量が増加し解析時間が長くなる可能性があります。

#include <stddef.h>
int f(int i);

int lang_struct_se_sizeof(int a, volatile long int *vp){
      size_t i;

      i = sizeof(a);                                    /* no side effects in operand */

      i += sizeof(++a);                 /* 'Side Effects in sizeof' warning issued here */

      i += sizeof(*vp++);                                /* *vp++ is a volatile-qualified lvalue */
          
      i += sizeof(int[a]);                               /* no side effects in operand */

      i += sizeof(int[++a]);            /* 'Side Effects in sizeof' warning issued here */

      i += sizeof(f(a));                /* 'Side Effects in sizeof' warning issued here
                                         *  - a function call is always considered to be a side effect */

      i += sizeof((1==1)? *vp : *vp++); /* 'Side Effects in sizeof' warning issued here
                                         * - even though the increment will not be executed */
      return i;
}

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。