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

            Bug ID: 101631
           Summary: gcc permits object reference to object outside of its
                    lifetime during constant evaluation
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fsb4000 at yandex dot ru
  Target Milestone: ---

Hi.

The bug has come up during the implementation of SSO for constexpr std::string
which can be found here:
https://github.com/microsoft/STL/pull/1735#discussion_r674285711

The issue stems from an obscure
rule(https://eel.is/c++draft/class.union.general#6) that allows switching the
active member of a union by assigning to an element of the array member.
(Obligatory godbolt: https://godbolt.org/z/6qG7v9eYx)

While the usage on line 12 is indeed correct, for std::string we need to go
through char_traits which is imitated by perform_assignment. However, the
lifetime of the subobject buf[5] only starts right before the assignment inside
of perform_assignment, so on line 25 we form an object reference to an object
outside of its lifetime which is ill formed.

gcc should emit an appropriate diagnostic.

Found by: Michael Schellenberger Costa

I hope it helps.

Reply via email to