Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixing a typo

...

In this noncompliant code example, a std::vector is used in place of a pointer and size pair. The function performs a range check to ensure that pos does not exceed the upper bound of the array container but fails to check the lower bound for table. Because pos is declared as a (signed) int, this parameter can assume a negative value, resulting in a write outside the bounds of the std::vector object.

Code Block
bgColor#ffcccc
langcpp
#include <vector>
 
void insert_in_table(std::vector<int> &table, int pos, int value) {
  if (pos >= table_.size()) {
    // Handle error
    return;
  }
  table[pos] = value;
}

...

Code Block
bgColor#ccccff
langcpp
#include <vector>
 
void insert_in_table(std::vector<int> &table, std::size_t pos, int value) {
  if (pos >= table_.size()) {
    // Handle error
    return;
  }
  table[pos] = value;
}

...