Main Content

MISRA C:2012 Rule 13.5

The right hand operand of a logical && or || operator shall not contain persistent side effects

Description

The right hand operand of a logical && or || operator shall not contain persistent side effects1 .

Rationale

The right operand of the || operator is not evaluated if the left operand is true. The right operand of the && operator is not evaluated if the left operand is false. In these cases, if the right operand modifies the value of a variable, the modification does not take place.

Polyspace Implementation

The rule checker reports situations where the right side of a logical || or && operator has persistent side effects. For instance, if the right side contains a function call and the function modifies a global variable, the rule checker reports a violation.

The rule checker does not report a violation if the right side contains a call to a pure function, that is, a function without side effects. The checker considers a function as pure if the function performs only simple operations such as:

  • Reading a nonvolatile parameter or global variable

  • Writing to a local variable

In addition to simple operations, if the function contains a call to another function, the checker attempts to determine if the callee is a pure function. If the checker determines that the callee is a pure function, then the calling function is considered a pure function (as long as the other operations in the calling function are simple operations).

The rule checker does not consider a function as pure if the function does one of the following:

  • Writes to a global variable or the dereference of a parameter

  • Reads or writes to a volatile variable, or contains an asm block

To determine if a function is pure, the checker needs to analyze the function definition. The checker looks for function definitions only within the same translation unit as the function call (a translation unit is a source file plus all headers included in the source). If a function definition is not found in the current translation unit, the checker does not report a violation of this rule. The checker also does not analyze functions called via function pointers.

If the right operand of the logical || or && operator invokes a function by using a function pointer, Polyspace® cannot determine whether the invoked function has side effects. Polyspace does not report a violation in this case.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

int check (int arg) {
    static int count;
    if(arg > 0) {
        count++;                   /* Persistent side effect */
        return 1;
    }
    else
        return 0;
}

int getSwitch(void);
int getVal(void);

void main(void) {
    int val = getVal();
    int mySwitch = getSwitch();
    int checkResult;

    if(mySwitch && check(val)) {   /* Non-compliant */
    }

    checkResult = check(val);
    if(checkResult && mySwitch) {  /* Compliant */
    }
    
    if(check(val) && mySwitch) {   /* Compliant */
    }
}

In this example, the rule is violated when the right operand of the && operation contains a function call. The function call has a persistent side effect because the static variable count is modified in the function body. Depending on mySwitch, this modification might or might not happen.

The rule is not violated when the left operand contains a function call. Alternatively, to avoid the rule violation, assign the result of the function call to a variable. Use this variable in the logical operation in place of the function call.

Check Information

Group: Side Effects
Category: Required
AGC Category: Required
PQL Name: std.misra_c_2012.R13_5

Version History

Introduced in R2014b

expand all


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.