Integer overflow or wraparound in arithmetic calculation

ID

c.integer_overflow.integer_wraparound

Severity

low

Resource

Integer Overflow

Language

C / C++

Description

The software performs a calculation that can produce an integer overflow or wraparound, when the logic assumes that the resulting value will always be larger than the original value. This can introduce other weaknesses when the calculation is used for resource management or execution control.

Rationale

The software performs a calculation that can produce an integer overflow or wraparound, when the logic assumes that the resulting value will always be larger than the original value. This can introduce other weaknesses when the calculation is used for resource management or execution control.

The following code illustrates a vulnerable pattern detected by this rule:

  int num_imgs;

  // ...

  num_imgs = get_num_imgs();
  // VULNERABLE: Integer overflow or wraparound in arithmetic calculation
  table_ptr = (img_t *)malloc(sizeof(img_t) * num_imgs);

  // ...
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.