Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

OperatorContext 
ifControlling expression
whileControlling expression
do ... whileControlling expression
forSecond operand
?:First operand
?:Second or third operands, where the ternary expression is used in any of these contexts
&& Either operand 
|| either operand 
,

Second operand, when the comma expression is used in any of these contexts


 

Performing assignment statements in other contexts do not violate this rule. However, they may violate other rules, such as EXP30-C. Do not depend on the order of evaluation for side effects.

...

Code Block
bgColor#FFcccc
langc
 do { /* ... */ } while (foo(), x = y);

...

Code Block
bgColor#FFcccc
langc
 for (; x; foo(), x = y) { /* ... */ }

Compliant Solution (Unintentional Assignment)

...

Code Block
bgColor#ccccff
langc
do { /* ... */ } while (foo(), (x = y) != 0);

Compliant Solution (for statement)

The same result can be obtained using the for statement, which is specifically designed to evaluate an expression on each iteration of the loop, just before performing the test in its controlling expression. Remember that its controlling expression is the second operand, where the assignment occurs in its third operand:

Code Block
bgColor#ccccff
langc
 for (; x; foo(), x = y) { /* ... */ }

Noncompliant Code Example

...

CERT_C-EXP45-ac
CERT_C-EXP45-Avoid conditions that always evaluate to the same valueA function identifier shall only be used with either a preceding '&', or with a parenthesised parameter list, which may be empty
4071, 4074 

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V
assignment-conditionalFully checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-EXP45
Clang
Include Page
Clang_V
Clang_V
-WparenthesesCan detect some instances of this rule, but does not detect all
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
LANG.STRUCT.CONDASSIG
LANG.STRUCT.SE.COND
LANG.STRUCT.USEASSIGN
Assignment in conditional
Condition contains side effects
Assignment result in expression
Compass/ROSE



Could detect violations of this recommendation by identifying any assignment expression as the top-level expression in an if or while statement

Cppcheck Premium

Include Page
Cppcheck Premium_V
Cppcheck Premium_V

premium-cert-exp45-cPartially implemented

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.EXP18
CC2.EXP21

Fully implemented

GCC
Include Page
GCC_V
GCC_V


Can detect violations of this recommendation when the -Wall flag is used

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C3314, C3326, C3344, C3416

C++4071, C++4074


Klocwork
Include Page
Klocwork_V
Klocwork_V

ASSIGCOND.CALL
ASSIGCOND.GEN
MISRA.ASSIGN.COND


LDRA tool suite
Include Page
LDRA_V
LDRA_V

114 S, 132 S

Enhanced Enforcement
Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

CERT_C-EXP45-b
CERT_C-EXP45-

d

Assignment operators shall not be used in conditions without brackets

Assignment operators shall not be used in expressions that yield a Boolean value

PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

720

Partially supported: reports Boolean test of unparenthesized assignment

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

Invalid use of = operatorAssignment in conditional statementPRQA QA-C
Include Page
PRQA QA-C_vPRQA QA-C_v3314, 3326, 3344, 3416Partially implementedCERT C: Rule EXP45-CChecks for invalid use of = (assignment) operator (rule fully covered)PRQA QA-C++
Include Page
cplusplus:PRQA QA-C++_Vcplusplus:PRQA QA-C++_V
PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V559, V633, V699
RuleChecker

Include Page
RuleChecker_V
RuleChecker_V

assignment-conditionalFully checked
SonarQube C/C++ Plugin
Include Page
SonarQube C/C++ Plugin_V
SonarQube C/C++ Plugin_V
AssignmentInSubExpression

Related Vulnerabilities

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

...