From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 04F693858C56; Thu, 21 Jul 2022 16:26:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 04F693858C56 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug analyzer/106385] New: Support for std::optional in -fanalyzer Date: Thu, 21 Jul 2022 16:26:08 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: analyzer X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: enhancement X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: dmalcolm at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter blocked target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Jul 2022 16:26:09 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106385 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-stu= dio-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 precondit= ion 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 o) { int i =3D 0; if (o.has_value()) i =3D *o; // safe if (o) // conversion to bool, equivalent to has_value() i =3D *o; // safe i =3D o.value_or(1); // safe if (rand() % 2) i =3D o.value(); // safe, throws if no value else i =3D *o; // unsafe! i =3D *o; // "safe" because we already accessed it once. } Similarly for std::expected, which is a union of two types with accessors f= or the result value or error value, only one of which is present. Some accesso= rs 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 access= ors 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=3D97110 [Bug 97110] [meta-bug] tracker bug for supporting C++ in -fanalyzer=