Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.LOGIC.SIDEEFF

Right 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 effects

Category: Required

Analysis: Undecidable, System

Applies to: C90, C99

Rationale

The 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.

Example

uint16_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 effects

Right 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.

Example

if ( 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 effects

Rationale

There 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.

Example

if ( 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.