Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
int insert(int index, int *list, int size, int value) {
  if (size != 0) {
    index = (index + 1) % size;
    list[index] = value;
    return index;
  }
  else {
    return -1;
  }
}

This code also violates ERR02-C. Avoid in-band error indicators.

Non-Compliant Code Example

Taking the absolute value of the modulo operation returns a positive value:

...

The most appropriate solution in this case is to use unsigned types to eliminate and any possible implementation defined behavior, as in this compliant solution: . For compliance with ERR02-C, we fill a 'result' argument with the mathematical result, and we return nonzero only if the operation succeeds.

Code Block
bgColor#ccccff
int insert(size_t* result, size_t index, int *list, size_t size, int value) {
  if (size != 0 && size != SIZE_MAX) {
    index = (index + 1) % size;
    list[index] = value;
    *result = index;
    return index1;
  }
  else {
    return -10;
  }
}

Risk Assessment

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

INT10-C

low

unlikely

high

P1

L3

...