Skip to main content

IAR Embedded Workbench for RL78 5.20

LIB-std-sort-overrun (C++ only)

In this section:
Synopsis

A buffer overrun is caused by use of std::sort.

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

A buffer overrun is caused by use of std::sort. std::sort can take a pointer to an array and a pointer to the end of the array as arguments, but if the pointer to the end of the array actually points beyond the end of the array being sorted, a buffer overrun will occur.

Coding standards
CWE 676

Use of Potentially Dangerous Function

CWE 122

Heap-based Buffer Overflow

CWE 121

Stack-based Buffer Overflow

CWE 119

Improper Restriction of Operations within the Bounds of a Memory Buffer

Code examples

The following code example fails the check and will give a warning:

#include <algorithm>

void example(void) {
  int a[10] = {0,1,2,3,4,5,6,7,8,9};
  std::sort(a, a+11);
}

The following code example passes the check and will not give a warning about this issue:

#include <algorithm>

void example(void) {
  int a[10] = {0,1,2,3,4,5,6,7,8,9};
  std::sort(a, a+5);
}