public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/106385] New: Support for std::optional in -fanalyzer
@ 2022-07-21 16:26 redi at gcc dot gnu.org
  0 siblings, 0 replies; only message in thread
From: redi at gcc dot gnu.org @ 2022-07-21 16:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106385

            Bug ID: 106385
           Summary: Support for std::optional in -fanalyzer
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
            Blocks: 97110
  Target Milestone: ---

Check that std::optional::has_value() (or equivalent) is checked before
accessing the contained value of a std::optional.

See
https://devblogs.microsoft.com/cppblog/new-stdoptional-checks-in-visual-studio-2022-version-17-3-preview-3/

std::optional either contains a value, or is empty. Some accessors for the
value are safe and will throw if no value is present, some have a precondition
that a value is present and do not check. The analyzer could flag when the
unchecked accessor is used without a preceeding check for a value.

void f(std::optional<int> o)
{
  int i = 0;

  if (o.has_value())
    i = *o; // safe
  if (o) // conversion to bool, equivalent to has_value()
    i = *o; // safe

  i = o.value_or(1);  // safe

  if (rand() % 2)
    i = o.value(); // safe, throws if no value
  else
    i = *o; // unsafe!

  i = *o; // "safe" because we already accessed it once.
}


Similarly for std::expected, which is a union of two types with accessors for
the result value or error value, only one of which is present. Some accessors
are safe and will throw an exception, others are unchecked and have
preconditions.

We don't need to do this for std::variant, as there are no unchecked accessors
for it (std::get will throw if the alternative you ask for isn't active, and
std::visit handles any alternative being active).


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97110
[Bug 97110] [meta-bug] tracker bug for supporting C++ in -fanalyzer

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-21 16:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-21 16:26 [Bug analyzer/106385] New: Support for std::optional in -fanalyzer redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).