C and C++


LANG.STRUCT.SE.SIZEOF : Side Effects in sizeof

Summary

The operand of a sizeof operator has one or more side effects, or would have one or more side effects if evaluated.

Exception: sizeof(volLval) will not trigger a warning of this class if volLval is a volatile-qualified lvalue (and is not a variable-length array).

For the purposes of this check:

Properties

Class Name Side Effects in sizeof
Significance style
Mnemonic LANG.STRUCT.SE.SIZEOF
Categories
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.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Side Effects in sizeof"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

#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;
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.