LIB-std-sort-overrun-pos (C++ only)
In this section:
Synopsis
Using std::sort might cause buffer overrun.
Enabled by default
No
Severity/Certainty
Medium/Medium

Full description
Using std::sort might cause a buffer overrun. 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 might 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);
}