Skip to content

Repo sync for protected branch #5019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Zero if successful. If there's a failure due to an invalid parameter, the invali

The **`ctime_s`** function converts a time value stored as a [`time_t`](../standard-types.md) structure into a character string. The *`sourceTime`* value is typically obtained from a call to [`time`](time-time32-time64.md), which returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC). The return value string contains exactly 26 characters and has the form:

`Wed Jan 02 02:03:55 1980\n\0`
`Wed Jan 2 02:03:55 1980\n\0`

A 24-hour clock is used. All fields have a constant width. The new line character ('\n') and the null character ('\0') occupy the last two positions of the string.

Expand Down
43 changes: 43 additions & 0 deletions docs/code-quality/c26459.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
description: "Learn more about: Warning C26459"
title: Warning C26459
ms.date: 4/10/2024
f1_keywords: ["C26459", "NO_RAW_POINTER_IN_STL_RANGE_CHECKED"]
helpviewer_keywords: ["C26459"]
---
# Warning C26459

> 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)

## Remarks

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.

Code analysis name: `NO_RAW_POINTER_IN_STL_RANGE_CHECKED`

## Example

The following code demonstrates undefined behavior because there isn't any bounds checking and `copy_if` writes beyond the provided storage.

```cpp
void f()
{
std::vector<int> myints = { 10, 20, 30, 40, 50, 60, 70 };
int mydestinationArr[7] = { 10, 20, 80 };

std::copy_if(myints.begin(), myints.end(), mydestinationArr, [](int i) { return !(i<0); }); // Warning: C26459
}
```

To fix the warning, use `gsl::span` to make sure the output range is bounds checked:

```cpp
void f()
{
std::vector<int> myints = { 10, 20, 30, 40, 50, 60, 70 };
int mydestinationArr[7] = { 10, 20, 80 };
gsl::span<int> mySpan{mydestinationArr};

std::copy_if(myints.begin(), myints.end(), mySpan.begin(), [](int i) { return !(i<0); }); // No warning
}
```
2 changes: 2 additions & 0 deletions docs/code-quality/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ items:
href: ../code-quality/c26456.md
- name: Warning C26457
href: ../code-quality/c26457.md
- name: Warning C26459
href: ../code-quality/c26459.md
- name: Warning C26460
href: ../code-quality/c26460.md
- name: Warning C26461
Expand Down