|
| 1 | +--- |
| 2 | +description: "Learn more about: Warning C26459" |
| 3 | +title: Warning C26459 |
| 4 | +ms.date: 4/10/2024 |
| 5 | +f1_keywords: ["C26459", "NO_RAW_POINTER_IN_STL_RANGE_CHECKED"] |
| 6 | +helpviewer_keywords: ["C26459"] |
| 7 | +--- |
| 8 | +# Warning C26459 |
| 9 | + |
| 10 | +> You called an STL function '%function%' with a raw pointer parameter at position '%position%' that may be unsafe - this relies on the caller to check that the passed values are correct. Consider wrapping your range in a gsl::span and pass as a span iterator (stl.1) |
| 11 | +
|
| 12 | +## Remarks |
| 13 | + |
| 14 | +Out of bound writes are one of the leading causes of remote code execution vulnerabilities. One remedy is to use bounds checked data structures like `gsl::span`. This warning identifies cases where Standard Template Library (STL) algorithms operate on raw pointers as output ranges. Raw pointers aren't bounds checked. To prevent vulnerabilities, use `gsl::span` instead. |
| 15 | + |
| 16 | +Code analysis name: `NO_RAW_POINTER_IN_STL_RANGE_CHECKED` |
| 17 | + |
| 18 | +## Example |
| 19 | + |
| 20 | +The following code demonstrates undefined behavior because there isn't any bounds checking and `copy_if` writes beyond the provided storage. |
| 21 | + |
| 22 | +```cpp |
| 23 | +void f() |
| 24 | +{ |
| 25 | + std::vector<int> myints = { 10, 20, 30, 40, 50, 60, 70 }; |
| 26 | + int mydestinationArr[7] = { 10, 20, 80 }; |
| 27 | + |
| 28 | + std::copy_if(myints.begin(), myints.end(), mydestinationArr, [](int i) { return !(i<0); }); // Warning: C26459 |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +To fix the warning, use `gsl::span` to make sure the output range is bounds checked: |
| 33 | + |
| 34 | +```cpp |
| 35 | +void f() |
| 36 | +{ |
| 37 | + std::vector<int> myints = { 10, 20, 30, 40, 50, 60, 70 }; |
| 38 | + int mydestinationArr[7] = { 10, 20, 80 }; |
| 39 | + gsl::span<int> mySpan{mydestinationArr}; |
| 40 | + |
| 41 | + std::copy_if(myints.begin(), myints.end(), mySpan.begin(), [](int i) { return !(i<0); }); // No warning |
| 42 | +} |
| 43 | +``` |
0 commit comments