MISRA.TOKEN.OCTAL.INTUsage of octal integer constants.
MISRA C 2012 Rule 7.1: Octal constants shall not be used[Koenig 9] Category: Required Analysis: Decidable, Single Translation Unit Applies to: C90, C99 RationaleDevelopers writing constants that have a leading zero might expect them to be interpreted as decimal constants. Note: this rule does not apply to octal escape sequences because the use of a leading \ character means that there is less scope for confusion. ExceptionThe integer constant zero (written as a single numeric digit), is strictly speaking an octal constant, but is a permitted exception to this rule. Exampleextern uint16_t code[ 10 ]; code[ 1 ] = 109; /* Compliant - decimal 109 */ code[ 2 ] = 100; /* Compliant - decimal 100 */ code[ 3 ] = 052; /* Non-Compliant - decimal 42 */ code[ 4 ] = 071; /* Non-Compliant - decimal 57 */ MISRA-C 2004 Rule 7.1 (required): Octal constants (other than zero) and octal escape sequences shall not be used.Usage of octal integer constants. This rule is also covered by MISRA.TOKEN.OCTAL.ESCAPE. [Koenig 9] Any integer constant beginning with a "0" (zero) is treated as octal. So there is a danger, for example, with writing fixed length constants. For example, the following array initialisation for 3-digit bus messages would not do as expected (052 is octal, i.e. 42 decimal): Examplecode[1] = 109; /* equivalent to decimal 109 */ code[2] = 100; /* equivalent to decimal 100 */ code[3] = 052; /* equivalent to decimal 42 */ code[4] = 071; /* equivalent to decimal 57 */ Octal escape sequences can be problematic because the inadvertent introduction of a decimal digit ends the octal escape and introduces another character. The value of the first expression in the following example is implementation-defined because the character constant consists of two characters, "\10" and "9". The second character constant expression below contains the single character "\100". Its value will be implementation-defined if character 64 is not represented in the basic execution character set. code[5] = '\109'; /* implementation-defined, two character constant */ code[6] = '\100'; /* set to 64, or implementation-defined */ It is better not to use octal constants or escape sequences at all, and to check statically for any occurrences. 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. MISRA-C++ 2008 Rule 2-13-2 (required): Octal constants (other than zero) and octal escape sequences (other than "\0") shall not be used.This rule is also covered by MISRA.TOKEN.OCTAL.ESCAPE. [Implementation 2.13.2(1, 2)] RationaleAny 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 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. ExampleThe following array initialization for 3-digit decimal bus messages would not behave as expected: code[ 1 ] = 109; // Compliant - decimal 109 code[ 2 ] = 100; // Compliant - decimal 100 code[ 3 ] = 052; // Non-compliant - equivalent to decimal 42 code[ 4 ] = 071; // Non-compliant - equivalent to decimal 57 The value of the first expression in the following example is implementation-defined because the character constant consists of two characters, "\10" and "9". The second character constant expression contains the single character "\100". code[ 5 ] = '\109'; // Non-compliant - implementation-defined, // two character constant code[ 6 ] = '\100'; // Non-compliant - set to 64. |