rspec/rules/S834/cfamily/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

61 lines
2.0 KiB
Plaintext

== Why is this an issue?
It is possible to declare an array without explicitly specifying its size, but using an explicit size declaration is clearer, and is therefore preferred.
=== Noncompliant code example
[source,cpp]
----
int arr1 [ ]; // Noncompliant; nothing specified
int arr2 [ ] = { [0] = 1, [12] = 36, [4] = 93 }; // Noncompliant; highest index determines size. May be difficult to spot
int pirate [ ] = { 2, 4, 8, 42, 501, 90210, 7, 1776 }; // Noncompliant; size is implicit, not explicit
----
=== Compliant solution
[source,cpp]
----
int arr1 [10];
int arr2 [13] = { [0] = 1, [12] = 36, [4] = 93 };
int pirate [10] = { 2, 4, 8, 42, 501, 90210, 7, 1776 }; // Implicitly-assigned size was 8. Desired size was 10.
----
== Resources
* MISRA C:2004, 8.12 - When an array is declared with external linkage, its size shall be stated explicitly or defined implicitly by initialisation
* MISRA {cpp}:2008, 3-1-3 - When an array is declared, its size shall either be stated explicitly or defined implicitly by initialization
* MISRA C:2012, 8.11 - When an array with external linkage is declared, its size should be explicitely specified
* MISRA C:2012, 9.5 - Where designated initializers are used to initialize an array object the size of the array shall be specified explicitly
* https://wiki.sei.cmu.edu/confluence/x/6dUxBQ[CERT, ARR02-C.] - Explicitly specify array bounds, even if implicitly defined by an initializer
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Explicitly declare the size of the "xxx" array.
'''
== Comments And Links
(visible only on this page)
=== is related to: S5298
=== is related to: S835
=== on 2 Jul 2014, 20:03:32 Ann Campbell wrote:
Note that the MISRA C:2012 standard is more strict on this rule than the other two. Rule written to the most stringent requirement.
=== on 30 Jul 2014, 20:08:29 Freddy Mallet wrote:
I guess the default severity of this rule should be "Minor" and not "Blocker" [~ann.campbell.2] ?
endif::env-github,rspecator-view[]