C and C++


LANG.STRUCT.SE.LOGIC : Side Effects in Logical Operand

Summary

LHS_operand && RHS_operand
/* or */
LHS_operand || RHS_operand

occurs with RHS_operand that contains side effects.

For the sake of this check:

Properties

Class Name Side Effects in Logical Operand
Significance style
Mnemonic LANG.STRUCT.SE.LOGIC
Categories
MisraC2023 MisraC2023:13.5 The right hand operand of a logical && or || operator shall not contain persistent side effects
Misra2012 Misra2012:13.5 The right hand operand of a logical && or || operator shall not contain persistent side effects
Misra2004 Misra2004:12.4 The right-hand operand of a logical && or || operator shall not contain side effects
AUTOSARC++14 AUTOSARC++14:M5-14-1 The right hand operand of a logical &&, || operators shall not contain side effects.
MisraC++2008 MisraC++2008:5-14-1 The right hand operand of a logical && or || operator shall not contain side effects.
MisraC++2023 MisraC++2023:8.14.1 The right hand operand of a logical && or || operator shall not contain persistent side effects
JSF++ JSF++:157 The right hand operand of a && or || operator shall not 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 Logical Operand"
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 <string.h>

int f(int i);
int g(int i) __attribute__((const));
int h(int i) __attribute__((pure));

int SE_LOGIC(int x, int y, volatile int *z, char *s){ 
    y += (!(x--) && y);                         /* side effect on LHS only */

    y += (x && !(y--));        /* 'Side Effect in Logical Operand' warning issued here */

    y += ((y > 5) || f(y));    /* 'Side Effect in Logical Operand' warning issued here */

    y += ((y > 5) || g(y));                     /* g() declared with __attribute__(const) */

    y += ((y > 5) || h(y));                     /* h() declared with __attribute__(pure) */

    y += ((y > 5) || strlen(s));                /* strlen() matches a factory setting for SIDE_EFFECT_FREE_FUNCTIONS */

    y += ((y > 5) || (y = 10));  /* 'Side Effect in Logical Operand' warning issued here */

    y += ((y > 5) || z);                         /* pointer is not itself volatile */

    y += ((y > 5) || *z);        /* 'Side Effect in Logical Operand' warning issued here */

    y += ((y > 5) || z[0]);      /* 'Side Effect in Logical Operand' warning issued here */

    return y;
}

Relevant Configuration File Parameters

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