Skip to content

Repo sync for protected CLA branch #4343

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
Dec 17, 2022
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
4 changes: 2 additions & 2 deletions docs/code-quality/c26829.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ helpviewer_keywords: ["C26829"]

## Remarks

Unwrapping empty `std::optional` values is undefined behavior. Such operation is considered a security vulnerability as it can result in a crash, reading uninitialized memory, or other unexpected behavior. This check will attempt to find cases where the value of the `std::optional` is known to be empty and unwrapped. You can also enable [C26830](../code-quality/c26830.md) for a stricter analysis.
Unwrapping empty `std::optional` values is undefined behavior. Such operation is considered a security vulnerability as it can result in a crash, reading uninitialized memory, or other unexpected behavior. This check will attempt to find cases where a `std::optional` is known to be empty and unwrapped. You can also enable [C26830](../code-quality/c26830.md), [C26859](../code-quality/c26859.md), and [C26860](../code-quality/c26860.md) for a stricter analysis.

## Example

Expand All @@ -27,4 +27,4 @@ void f(std::optional<int> maybeEmpty)
}
```

To solve this problem, make sure the code never unwraps an empty optional.
To solve this problem, make sure the code never unwraps an empty optional. Alternatively, use the `value` method and make sure you handle the `std::bad_optional_access` exception.
4 changes: 2 additions & 2 deletions docs/code-quality/c26830.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ helpviewer_keywords: ["C26830"]

## Remarks

Unwrapping empty `std::optional` values is undefined behavior. Such operation is considered a security vulnerability as it can result in a crash, reading uninitialized memory, or other unexpected behavior. This check will attempt to find cases where the value of the `std::optional` isn't checked for emptiness before unwrap operations. You can enable [C26829](../code-quality/c26829.md) only for a more permissive analysis.
Unwrapping empty `std::optional` values is undefined behavior. Such operation is considered a security vulnerability as it can result in a crash, reading uninitialized memory, or other unexpected behavior. This check will attempt to find cases where a `std::optional` isn't checked for emptiness before unwrap operations. You can enable [C26829](../code-quality/c26829.md) only for a more permissive analysis.

## Example

Expand All @@ -28,4 +28,4 @@ void f(std::optional<int> maybeEmpty)
}
```

To solve this problem, make sure the code never unwraps an empty optional.
To solve this problem, make sure the code never unwraps an empty optional. Alternatively, use the `value` method and make sure you handle the `std::bad_optional_access` exception.
30 changes: 30 additions & 0 deletions docs/code-quality/c26859.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: Warning C26859
description: "Describes the Microsoft C/C++ code analysis warning C26859, its causes, and how to address it."
ms.date: 12/15/2022
f1_keywords: ["C26859", "UNWRAP_EMPTY_OPTIONAL_VALUE"]
helpviewer_keywords: ["C26859"]
---
# Warning C26859

> Empty optional '*variable*' is unwrapped, will throw exception

## Remarks

Unwrapping empty `std::optional` values via the `value` method will throw an exception. Such operation can result in a crash when the exception isn't handled. This check will attempt to find cases where a `std::optional` is known to be empty and unwrapped using the `value` method. You can also enable [C26829](../code-quality/c26829.md), [C26830](../code-quality/c26830.md), and [C26860](../code-quality/c26860.md) for a stricter analysis.

## Example

```cpp
void f(std::optional<int> maybeEmpty)
{
std::optional<int> empty;
std::optional<int> nonEmpty{5};
nonEmpty.value() = 42; // No warning
empty.value() = 42; // warning: C26859
if (!maybeEmpty)
maybeEmpty.value() = 42; // warning: C26859
}
```

To solve this problem, make sure the code never unwraps an empty optional.
31 changes: 31 additions & 0 deletions docs/code-quality/c26860.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: Warning C26860
description: "Describes the Microsoft C/C++ code analysis warning C26860, its causes, and how to address it."
ms.date: 12/15/2022
f1_keywords: ["C26860", "UNWRAP_EMPTY_OPTIONAL_VALUE_MAYBE"]
helpviewer_keywords: ["C26860"]
---
# Warning C26860

> Potentially empty optional '*variable*' is unwrapped, may throw exception

## Remarks

Unwrapping empty `std::optional` values via the `value` method will throw an exception. Such operation can result in a crash when the exception isn't handled. This check will attempt to find cases where a `std::optional` isn't checked for emptiness before unwrapping it via the `value` method. You can enable [C26829](../code-quality/c26829.md), and [C26859](../code-quality/c26859.md) only for a more permissive analysis.

## Example

```cpp
std::optional<int> getOptional();

void f(std::optional<int> maybeEmpty)
{
if (maybeEmpty)
maybeEmpty.value() = 42; // No warning
maybeEmpty.value() = 5; // warning: C26860
std::optional<int> o = getOptional();
o.value() = 42; // warning: C26860
}
```

To solve this problem, make sure the code never unwraps an empty optional.
4 changes: 4 additions & 0 deletions docs/code-quality/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ items:
href: ../code-quality/c26829.md
- name: Warning C26830
href: ../code-quality/c26830.md
- name: Warning C26859
href: ../code-quality/c26859.md
- name: Warning C26860
href: ../code-quality/c26860.md
- name: Warning C28020
href: ../code-quality/c28020.md
- name: Warning C28021
Expand Down