MISRA.LOGIC.SIDEEFFRight operand in a logical 'and' or 'or' expression contains side effects. MISRA C 2012 Rule 13.5: The right hand operand of a logical && or || operator shall not contain persistent side effectsCategory: Required Analysis: Undecidable, System Applies to: C90, C99 RationaleThe evaluation of the right-hand operand of the && and || operators is conditional on the value of the left-hand operand. If the right-hand operand contains side effects then those side effects may or may not occur which may be contrary to programmer expectations. If evaluation of the right-hand operand would produce side effects which are not persistent at the point in the program where the expression occurs then it does not matter whether the right-hand operand is evaluated or not. The term persistent side effect is defined in Appendix J. Exampleuint16_t f ( uint16_t y ) { /* These side effects are not persistent as seen by the caller */ uint16_t temp = y; temp = y + 0x8080U; return temp; } uint16_t h ( uint16_t y ) { static uint16_t temp = 0; /* This side effect is persistent */ temp = y + temp; return temp; } void g ( void ) { /* Compliant - f ( ) has no persistent side effects */ if ( ishigh && ( a == f ( x ) ) ) { } /* Non-compliant - h ( ) has a persistent side effect */ if ( ishigh && ( a == h ( x ) ) ) { } } volatile uint16_t v; uint16_t x; /* Non-compliant - access to volatile v is persistent */ if ( ( x == 0u ) || ( v == 1u ) ) { } /* Non-compliant if fp points to a function with persistent side effects */ ( fp != NULL ) && ( *fp ) ( 0 ); MISRA-C Rule 12.4 (required): The right-hand operand of a logical && or || operator shall not contain side effectsRight operand in a logical 'and' or 'or' expression contains side effects. This rule is also covered by MISRA.LOGIC.SIDEEFF.COND. There are some situations in C code where certain parts of expressions may not be evaluated. If these sub-expressions contain side effects then those side effects may or may not occur, depending on the values of other sub expressions. The operators which can lead to this problem are &&, || and ?:. In the case of the first two (logical operators) the evaluation of the right-hand operand is conditional on the value of the left-hand operand. In the case of the ?: operator, either the second or third operands are evaluated but not both. The conditional evaluation of the right-hand operand of one of the logical operators can easily cause problems if the programmer relies on a side effect occurring. The ?: operator is specifically provided to choose between two sub-expressions, and is therefore less likely to lead to mistakes. Exampleif ( ishigh && ( x == i++ ) ) /* Not compliant */ if ( ishigh && ( x == f(x) ) ) /* Only acceptable if f(x) is known to have no side effects */ The operations that cause side effects are described in section 5.1.2.3 of ISO/IEC 9899:1990 [2] as accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations, which cause changes in the state of the execution environment of the calling function. MISRA-C++ Rule 5-14-1 (required): The right hand operand of a logical && or || operator shall not contain side effectsRationaleThere are some situations in C++ where certain parts of expressions may not be evaluated. If these sub-expressions contain side effects then those side effects may or may not occur, depending on the values of other sub expressions. The operators which can lead to this problem are && and || where the evaluation of the right-hand operand is conditional on the value of the left-hand operand. The conditional evaluation of the right-hand operand of one of the logical operators can easily cause problems if the developer relies on a side effect occurring. Exampleif ( ishigh && ( x == i++ ) ) // Non-compliant ... if ( ishigh && ( x == f( x ) ) ) // Only acceptable if f(x) is // known to have no side effects The operations that cause side effects are accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations, which cause changes in the state of the execution environment of the calling function. |