GitHub
CERT Secure Coding

EXP02-C. Be aware of the short-circuit behavior of the logical AND and OR operators

The logical AND and logical OR operators ( && and || , respectively) exhibit "short-circuit" operation. That is, the second operand is not evaluated if the result can be deduced solely by evaluating the first operand.

Programmers should exercise caution if the second operand contains side effects because it may not be apparent whether the side effects actually occur.

In the following code, the value of i is incremented only when i >= 0 :``` java enum { max = 15 }; int i = /* Initialize to user-supplied value */;

if ( (i >= 0) && ( (i++) <= max) ) { /* Code */ }


Although the behavior is well defined, it is not immediately obvious whether or not `i` gets incremented.

## Noncompliant Code Example

In this noncompliant code example, the second operand of the logical OR operator invokes a function that results in side effects:

::code-block{quality="bad"}

``` c
char *p = /* Initialize; may or may not be NULL */

if (p || (p = (char *) malloc(BUF_SIZE)) ) {
  /* Perform some computation based on p */
  free(p);
  p = NULL;
} else {
  /* Handle malloc() error */
  return;
}

::

Because malloc() is called only if p is NULL when entering the if clause, free() might be called with a pointer to local data not allocated by malloc() . (See MEM34-C. Only free memory allocated dynamically .) This behavior is partially due to the uncertainty of whether or not malloc() is actually called.

Compliant Solution

In this compliant solution, a second pointer, q , is used to indicate whether malloc() is called; if not, q remains set to NULL . Passing NULL to free() is guaranteed to safely do nothing.

Compliant code
char *p = /* Initialize; may or may not be NULL */
char *q = NULL;
if (p == NULL) {
  q = (char *) malloc(BUF_SIZE);
  p = q;
}
if (p == NULL) {
  /* Handle malloc() error */
  return;
}

/* Perform some computation based on p */
free(q);
q = NULL;

Risk Assessment

Failing to understand the short-circuit behavior of the logical OR or AND operator may cause unintended program behavior.

Recommendation Severity Likelihood Detectable Repairable Priority Level
EXP02-C Low Unlikely No No P1 L3

Automated Detection

Tool

Version

Checker

Description

Astrée
25.10
logop-side-effectFully checked
Axivion Bauhaus Suite

7.2.0

CertC-EXP02Fully implemented
Compass/ROSE



Could detect possible violations of this recommendation by reporting expressions with side effects, including function calls, that appear on the right-hand side of an && or || operator

Helix QAC

2025.2

C3415
Klocwork
2025.2
MISRA.LOGIC.SIDEEFF
LDRA tool suite
9.7.1

35 D
1 Q
133 S
406 S
408 S

Fully implemented

Parasoft C/C++test
2025.2
CERT_C-EXP02-a

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

PC-lint Plus

1.4

9007

Fully supported

RuleChecker
25.10
logop-side-effectFully checked
SonarQube C/C++ Plugin
3.11
SideEffectInRightHandSideOfLogical

Search for vulnerabilities resulting from the violation of this rule on the CERT website .

SEI CERT C++ Coding StandardVOID EXP02-CPP. Be aware of the short-circuit behavior of the logical AND and OR operators
MITRE CWECWE-768 , Incorrect short circuit evaluation