Merk
Tilgang til denne siden krever autorisasjon. Du kan prøve å logge på eller endre kataloger.
Tilgang til denne siden krever autorisasjon. Du kan prøve å endre kataloger.
Empty optional 'variable' is unwrapped
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 a std::optional is known to be empty and unwrapped. You can also enable C26830, C26859, and C26860 for a stricter analysis.
Example
void f(std::optional<int> maybeEmpty)
{
std::optional<int> empty;
std::optional<int> nonEmpty{5};
*nonEmpty = 42; // No warning
*empty = 42; // warning: C26829
if (!maybeEmpty)
*maybeEmpty = 42; // warning: C26829
}
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.