Versions Compared

Key

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

The C ISO Standard defines octal constants as a 0 followed by octal digits (0 1 2 3 4 5 6 7).

This can lead to programming errors in constants that are meant to be taken by their decimal value, especially when declaring multiple constants and preserving fixed length.

Noncompliant code example

Programming errors can occur when decimal values are mistakenly specified as octal constants.

Noncompliant Code Example

In this noncompliant code example, a decimal constant is mistakenly prefaced with zeros so that all the constants are a fixed lengthWhen using integer constants, for example, as in:

Code Block
bgColor#FFCCCC
langc
i_array[0] = 2719;
i_array[1] = 4435;
i_array[2] = 0042;

It seems as if the element in Although it may appear that i_array is meant to hold [2] is assigned the decimal value 42 instead of what actually gets stored, which is the , it is actually assigned the decimal value 34.

Compliant

...

Solution

To avoid using wrong values and to make the code more readable, don't pad do not preface constants with zeroes if the value is meant to be decimal.:

Code Block
bgColor#CCCCFF
langc
i_array[0] = 2719;
i_array[1] = 4435;
i_array[2] =   42;

Risk

...

Assessment

Misinterpreting Misrepresenting decimal values as octal could can lead to an incorrect value being written into codeincorrect comparisons and assignments.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

DCL18-C

low

Low

unlikely

Unlikely

low

Low

P3

L3

References

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V
octal-constantFully checked
Axivion Bauhaus Suite
Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V
CertC-DCL18
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
LANG.TYPE.OCOctal constant
Helix QAC
Include Page
Helix QAC_V
Helix QAC_V
C0339, C1272
Klocwork
Include Page
Klocwork_V
Klocwork_V
MISRA.TOKEN.OCTAL.ESCAPE
MISRA.TOKEN.OCTAL.INT

LDRA tool suite 
Include Page
LDRA_V
LDRA_V
83 SFully Implemented
Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

CERT_C-DCL18-a
CERT_C-DCL18-b

Octal and hexadecimal escape sequences shall be terminated
Octal constants (other than zero) shall not be used

PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

9001

Fully supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rec. DCL18-CChecks for use of octal constants (rec. fully covered)


PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V536
RuleChecker
Include Page
RuleChecker_V
RuleChecker_V
octal-constantFully checked
SonarQube C/C++ Plugin
Include Page
SonarQube C/C++ Plugin_V
SonarQube C/C++ Plugin_V
OctalConstantAndSequence

Related Guidelines

MISRA C:2012Rule 7.1 (required)


...

Image Added Image Added Image Added Wiki Markup\[[ISO/IEC 9899:1999|cplusplus:AA. C References#ISO/IEC 9899-1999]\] Section 6.4.4.1 "Integer constants" \[cplusplus:MISRA 04\] Section 6.7 Rule 7.1