Skip to content

Commit

Permalink
Added how to fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Kamiński committed Jan 24, 2024
1 parent 363246f commit 1fbe7e4
Showing 1 changed file with 142 additions and 25 deletions.
167 changes: 142 additions & 25 deletions rules/S6871/cfamily/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -163,55 +163,172 @@ Pod p2(1, 0); // Complaint

== How to fix it

Provide initial value for all elements of the array or fields of the
aggregate or zero-initialize the object.
This issue can be addressed by either:

TODO: should I have separate examples or just roll into why this is an issue
TODO: if a separate example mentions brace elision
* providing intial value for the element or field in initializer of in class defintion ({cpp}14 or later)
* using idomatic zero-initialization syntax

=== Code examples

Provide values for all elements of field in intializer.

==== Noncompliant code example

[source,text,diff-id=1,diff-type=noncompliant]
[source,cpp,diff-id=1,diff-type=noncompliant]
----
FIXME
struct Pod {
int x;
int y;
};
struct PodPair {
Pod first;
Pod second;
};
struct ArrayMember {
int id;
int vals[4];
};
int a1[5] = {1, 2, 3}; // Noncompliant
Pod p1{1}; // Noncomplaint
int c1[2][2] = {{1}, {2}}; // Noncomplaint
Pod c2[3] = {{1, 2}, {2}}; // Noncomplaint
PodPair c3 = {{1}}; // Noncomplaint
ArrayMember c4 = {1, 2, 3}; // Noncomplaint
----

==== Compliant solution

[source,text,diff-id=1,diff-type=compliant]
[source,c,diff-id=1,diff-type=compliant]
-----
struct Pod {
int x;
int y;
};
struct PodPair {
Pod first;
Pod second;
};
struct ArrayMember {
int id;
int vals[4];
};
int a1[5] = {1, 2, 3, 0, 0}; // Compliant
Pod p1{1, 0}; // Complaint
int c1[2][2] = {{1, 0}, {2, 0}}; // Complaint
Pod c2[3] = {{1, 2}, {2, 0}, {0, 0}}; // Complaint
PodPair c3 = {{1, 0}, {0, 0}}; // Complaint
ArrayMember c4 = {1, 2, 3, 0, 0, 0}; // Complaint
----
FIXME

Or use zero-initialization syntax for `c2` and `c3`:
-----
Pod c2[3] = {{1, 2}, {2, 0}, {}}; // Complaint
PodPair c3{{1, 0}, {}}; // Complaint
----

== Resources
=== Code examples

=== External coding guidelines
Use idomatic syntax for zero-intialization.

* MISRA C:2012, 9.3 - Arrays shall not be partially initialized.
==== Noncompliant code example

[source,c,diff-id=2,diff-type=noncompliant]
----
struct ArrayMember {
int id;
int vals[4];
};
int a1[5] = {0, 0, 0}; // Noncompliant
int c1[2][3] = {{0, 0}, {0, 0}}; // Noncomplaint
ArrayMember c2 = {11}; // Noncomplaint
----

==== Compliant solution

[source,c,diff-id=2,diff-type=compliant]
-----
struct ArrayMember {
int id;
int vals[4];
};
int a1[5] = {0}; // Compliant
int c1[2][3] = {0}; // Complaint
ArrayMember c2 = {11, {0}}; // Complaint
----

Or in case of {cpp}:
[source,cpp]
-----
int a1[5]{}; // Compliant
int c1[2][3] = {{}, {}}; // Complaint
ArrayMember c2 = {11, {}}; // Complaint
----

=== Code examples

For {cpp14} or later, provide default value for the field in class.

==== Noncompliant code example

[source,cpp,diff-id=3,diff-type=noncompliant]
----
struct Pod {
int x;
int y;
};
struct ArrayMember {
int id;
int vals[4];
};
Pod p1{1}; // Noncompliant
ArrayMember m1{11}; // Noncomplaint
----

==== Compliant solution

[source,cpp,diff-id=3,diff-type=compliant]
-----
struct Pod {
int x;
int y = 2;
};
struct ArrayMember {
int id;
int vals[4]{1, 2, 3, 4};
};
Pod p1{1}; // Compliant
ArrayMember m1{11}; // Complaint
----


== Resources

=== Documentation

* {cpp} reference -- https://en.cppreference.com/w/cpp/language/aggregate_initialization[Aggregate initialization]
* {cpp} reference -- https://en.cppreference.com/w/cpp/language/value_initialization[Value-initialization]

=== Related rules

* S835 - Braces should be used to indicate and match the structure in the non-zero initialization of arrays and structures
* S6872 - Aggregates should be initialized with braces in non-generic code
=== External coding guidelines

//=== How does this work?
* MISRA C:2012, 9.3 - Arrays shall not be partially initialized.

//=== Pitfalls

//=== Going the extra mile
=== Related rules

* S835 - Braces should be used to indicate and match the structure in the non-zero initialization of arrays and structures
* S6872 - Aggregates should be initialized with braces in non-generic code

//== Resources
//=== Documentation
//=== Articles & blog posts
//=== Conference presentations
//=== Standards
//=== External coding guidelines
//=== Benchmarks

0 comments on commit 1fbe7e4

Please sign in to comment.