Versions Compared

Key

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

Do not use a semicolon on the same line as an if, for, or while statement because this it typically indicates programmer error and can result in unexpected behavior.

Noncompliant Code Example

In this noncompliant code example, a semicolon is used on the same line as an if statement.:

Code Block
bgColor#FFcccc
langc

if (a == b); {
  /* ... */
}

Compliant Solution

It is likely, in this example, that the semicolon was accidentally inserted.:

Code Block
bgColor#ccccff
langc

if (a == b) {
  /* ... */
}

Risk Assessment

Errors of omission can result in unintended program flow.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

EXP15-C

High

Likely

Low

P27

L1


Automated Detection

Tool

Version

Checker

Description

Section

Klocwork

Include Pagec:Klocwork_Vc:Klocwork_V
Section

SEMICOL

 

Related Guidelines

ISO/IEC TR 24772 "KOA Likely Incorrect Expressions"

MITRE CWE: CWE-480, "Use of Incorrect Operator"

Bibliography

...

Astrée
Include Page
Astrée_V
Astrée_V
empty-bodyFully checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-EXP15Fully implemented
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
LANG.STRUCT.EBSEmpty branch statement
Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C3109
Klocwork
Include Page
Klocwork_V
Klocwork_V

SEMICOL


LDRA tool suite
Include Page
LDRA_V
LDRA_V
11 S, 12 S, 428 SFully Implemented
Parasoft C/C++test

Include Page
Parasoft_V
Parasoft_V

CERT_C-EXP15-aSuspicious use of semicolon
PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

721, 722

Partially supported: reports missing body from if, for, or while with semi-colon immediately following predicate

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rec. EXP15-CChecks for semicolon on same line as for, if or while statement (rule fully covered)
PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V529, V715
SonarQube C/C++ Plugin
Include Page
SonarQube C/C++ Plugin_V
SonarQube C/C++ Plugin_V
S1116
RuleChecker
Include Page
RuleChecker_V
RuleChecker_V
empty-bodyFully checked

Related Guidelines

Bibliography

[Hatton 1995]Section

...

2.7.2,

...

"Errors

...

of

...

Omission and Addition"


...

Image Modified      03. Expressions (EXP)       Image Added Image Modified