Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.TOKEN.OCTAL.ESCAPE

Usage of octal escape sequences.

MISRA-C Rule 7.1 (required): Octal constants (other than zero) and octal escape sequences shall not be used.

This rule is also covered by MISRA.TOKEN.OCTAL.INT.

[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):

Example

code[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++ 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.INT.

[Implementation 2.13.2(1, 2)]

Rationale

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 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.

Example

The 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.