[metadata]
id = "EXP02-C"
type = "recommendation"
category = "EXP"
number = 2
title = "Be aware of the short-circuit behavior of the logical AND and OR operators"
description = """
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 containsside effectsbecause it may not be
apparent whether the side effects actually occur. In the following code, the
value ofiis incremented only wheni >= 0:
"""
severity = "Low"
likelihood = "Unlikely"
priority = "P1"
level = "L3"
cert_version = "2016 Edition (Wiki)"
last_modified = "May 20, 2025"
[rules.cert_c.EXP02-C]
enabled = true
[references]
wiki = "https://wiki.sei.cmu.edu/confluence/display/c/EXP02-C.+Be+aware+of+the+short-circuit+behavior+of+the+logical+AND+and+OR+operators"
cwe = ["CWE-768"]