Skip to content

std::optional - unchecked deref security concern #1359

@tiagomacarios

Description

@tiagomacarios

With #878 - : Don't zero storage in default constructor wouldn't we have a security concern around the use of optional?

Take the following code as an example:

#include <optional>
#include <iostream>

int main()
{
    std::optional<int> i = std::nullopt;
    std::cout << *i << '\n';
    return *i;
}

Looking at the godbolt output for clang I get a random number on every execution. I am expecting the same behavior for msvc going forward. I understand that there are performance concerns as well and I am wondering if we could have a debug only check for optional (just like we do buffer overrun checks for vector).

What I have in mind is something similar to llvm::expected behavior. It is has a member variable that tracks if the object was ever checked. If during the lifetime of the object it's contents where never checked the program is ill formed.

I think this makes sense in two accounts:

  • the security concern
  • misuse of optional when a value could be enough

Downsides:

  • ABI breaking - Could be protected under a #if (just like LLVM)
  • Possible use confusion

If the above makes sense I can try to create a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSomething can be improvedfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions