Rule Definition
Any integer constant beginning with a “0” (zero) is treated as octal. Because of this, it is possible
for a zero-prefixed constant that is intended to be a decimal number to be incorrectly entered as an
octal number, contrary to developer expectations.
Octal escape sequences can also be problematic because of the inadvertent introduction of a decimal
digit (i.e. “8” or “9”) ends the octal escape and introduces another character.
The integer constant zero (written as a single numeric digit), is strictly speaking an octal constant but is a permitted exception to this rule. Additionally, “\0” is the only permitted octal escape
sequence.
Remediation
Use decimal integers rather than Octal constants
Violation Code Sample
code[ 3 ] = 052; // Non-compliant - equivalent to decimal 42
code[ 4 ] = 071; // Non-compliant - equivalent to decimal 57
code[ 5 ] = '\109'; // Non-compliant - implementation-defined,
// two character constant
code[ 6 ] = '\100'; // Non-compliant - set to 64.
Fixed Code Sample
code[ 1 ] =52; // Compliant - decimal 52
code[ 2 ] =71; // Compliant - decimal 71
________________________________________________
Exception
code[1] = 0
Reference
Standards Reference:
MISRA C++ 2008, 2-13-2: Octal constants (other than zero) and octal escape sequences (other than "\0") shall not be used
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.