I’m interested in adding a rule to coverity checkers and want to consult if it’s feasible and what is needed to do to achieve. I’m talking about C programming, and I want to restrict the access to arrays using a defined enumerator – and not any integer index.
For example I have two arrays: oranges
and apples
of sizes 5 and 10 cells accordingly.
In order to avoid misuse of the arrays, I want to define two enums (or typedefs if needed), one for oranges
and one for apples
:
Enum apples {
A0 = 0,
A1 = 1,
A2 = 2,
A3 = 3,
A4 = 4,
}
Enum oranges {
O0 = 0,
O1 = 1,
O2 = 2,
O3 = 3,
O4 = 4,
O5 = 5,
O6 = 6,
O7 = 7,
O8 = 8,
O9 = 9,
}
And I want to add a rule to coverity that checks each access to these arrays. For example:
Apples[A4]; //success
Apples[O0]; // coverity error
Apples[2]; // coverity error
Is it possible to add such rule ?
//USING MODULUS OPERATOR TO AVOID OUT OF BOUND