rspec/rules/S1314/cfamily/rule.adoc

31 lines
1.5 KiB
Plaintext
Raw Normal View History

Integer literals starting with a zero are octal rather than decimal values. While using octal values is fully supported, most developers do not have experience with them. They may not recognize octal values as such, mistaking them instead for decimal values.
2020-06-30 12:47:33 +02:00
Hexadecimal literals (``++0xdeadbeef++``) and binary literals (``++0b0101'0110'00011++``, available since {cpp}14), on the other hand, have a clear marker (``++0x++`` or ``++0b++``) and can be used to define the binary representation of a value.
== Noncompliant Code Example
----
int myNumber = 010; // Noncompliant. myNumber will hold 8, not 10 - was this really expected?
----
== Compliant Solution
----
int myNumber = 8; // Use decimal when representing the value 8
// or
int myNumber = 0b1000; // Use binary or hexadecimal for a bit mask
----
== Exceptions
Octal values have traditionally been used for user permissions in Posix file systems, and this rule will ignore octal literals used in this context.
2020-06-30 12:47:33 +02:00
== See
* MISRA C:2004, 7.1 - Octal constants (other than zero) and octal escape sequences shall not be used.
2021-01-26 14:30:57 +01:00
* MISRA {cpp}:2008, 2-13-2 - Octal constants (other than zero) and octal escape sequences (other than "\0") shall not be used
2020-06-30 12:47:33 +02:00
* MISRA C:2012, 7.1 - Octal constants shall not be used
* https://wiki.sei.cmu.edu/confluence/x/atYxBQ[CERT, DCL18-C.] - Do not begin integer constants with 0 when specifying a decimal value
* https://wiki.sei.cmu.edu/confluence/x/7DZGBQ[CERT, DCL50-J.] - Use visually distinct identifiers