Skip to content

Commit 8ab0ef1

Browse files
committed
Merging changes synced from https://github.com/MicrosoftDocs/cpp-docs-pr (branch live)
2 parents c0d1322 + 362434b commit 8ab0ef1

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

docs/code-quality/c26829.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ helpviewer_keywords: ["C26829"]
1111
1212
## Remarks
1313

14-
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.
14+
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.
1515

1616
## Example
1717

@@ -27,4 +27,4 @@ void f(std::optional<int> maybeEmpty)
2727
}
2828
```
2929
30-
To solve this problem, make sure the code never unwraps an empty optional.
30+
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.

docs/code-quality/c26830.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ helpviewer_keywords: ["C26830"]
1111
1212
## Remarks
1313

14-
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.
14+
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.
1515

1616
## Example
1717

@@ -28,4 +28,4 @@ void f(std::optional<int> maybeEmpty)
2828
}
2929
```
3030
31-
To solve this problem, make sure the code never unwraps an empty optional.
31+
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.

docs/code-quality/c26859.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: Warning C26859
3+
description: "Describes the Microsoft C/C++ code analysis warning C26859, its causes, and how to address it."
4+
ms.date: 12/15/2022
5+
f1_keywords: ["C26859", "UNWRAP_EMPTY_OPTIONAL_VALUE"]
6+
helpviewer_keywords: ["C26859"]
7+
---
8+
# Warning C26859
9+
10+
> Empty optional '*variable*' is unwrapped, will throw exception
11+
12+
## Remarks
13+
14+
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.
15+
16+
## Example
17+
18+
```cpp
19+
void f(std::optional<int> maybeEmpty)
20+
{
21+
std::optional<int> empty;
22+
std::optional<int> nonEmpty{5};
23+
nonEmpty.value() = 42; // No warning
24+
empty.value() = 42; // warning: C26859
25+
if (!maybeEmpty)
26+
maybeEmpty.value() = 42; // warning: C26859
27+
}
28+
```
29+
30+
To solve this problem, make sure the code never unwraps an empty optional.

docs/code-quality/c26860.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: Warning C26860
3+
description: "Describes the Microsoft C/C++ code analysis warning C26860, its causes, and how to address it."
4+
ms.date: 12/15/2022
5+
f1_keywords: ["C26860", "UNWRAP_EMPTY_OPTIONAL_VALUE_MAYBE"]
6+
helpviewer_keywords: ["C26860"]
7+
---
8+
# Warning C26860
9+
10+
> Potentially empty optional '*variable*' is unwrapped, may throw exception
11+
12+
## Remarks
13+
14+
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.
15+
16+
## Example
17+
18+
```cpp
19+
std::optional<int> getOptional();
20+
21+
void f(std::optional<int> maybeEmpty)
22+
{
23+
if (maybeEmpty)
24+
maybeEmpty.value() = 42; // No warning
25+
maybeEmpty.value() = 5; // warning: C26860
26+
std::optional<int> o = getOptional();
27+
o.value() = 42; // warning: C26860
28+
}
29+
```
30+
31+
To solve this problem, make sure the code never unwraps an empty optional.

docs/code-quality/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,10 @@ items:
607607
href: ../code-quality/c26829.md
608608
- name: Warning C26830
609609
href: ../code-quality/c26830.md
610+
- name: Warning C26859
611+
href: ../code-quality/c26859.md
612+
- name: Warning C26860
613+
href: ../code-quality/c26860.md
610614
- name: Warning C28020
611615
href: ../code-quality/c28020.md
612616
- name: Warning C28021

0 commit comments

Comments
 (0)