Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
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.