CTR50-CPP. Guarantee that container indices and iterators are within the valid range
Ensuring that array references are within the bounds of the array is almost entirely the responsibility of the programmer. Likewise, when using standard template library vectors, the programmer is responsible for ensuring integer indexes are within the bounds of the vector.
Noncompliant Code Example (Pointers)
This noncompliant code example shows a function, insert_in_table() , that has two int parameters, pos and value , both of which can be influenced by data originating from untrusted sources. The function performs a range check to ensure that pos does not exceed the upper bound of the array, specified by tableSize , but fails to check the lower bound. Because pos is declared as a (signed) int , this parameter can assume a negative value, resulting in a write outside the bounds of the memory referenced by table .