[Bug c++/90940] New: Miscompilation of ternary operator with throw expression in return statement

moritz.klammler at cetitec dot com gcc-bugzilla@gcc.gnu.org
Wed Jun 19 14:09:00 GMT 2019


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

            Bug ID: 90940
           Summary: Miscompilation of ternary operator with throw
                    expression in return statement
           Product: gcc
           Version: 9.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: moritz.klammler at cetitec dot com
  Target Milestone: ---

After upgrading from GCC 8.3 to GCC 9.1 today, our code started to break. We
were able to eventually condense it down to the following presumably minimal
example.

    #include <string>

    std::string foo()
    {
        /*const*/ auto s = std::string(42, '*');
        return s.empty() ? throw "empty" : s;
    }

When written as shown, the destructor of `s` will be called and cause a
double-free on the string's buffer.  If the variable `s` is declared `const`,
GCC encounters an ICE.

    test.cxx: In function ‘std::string foo()’:
    test.cxx:7:40: internal compiler error: in ocp_convert, at cp/cvt.c:766
        7 |     return s.empty() ? throw "empty" : s;
          |                                        ^
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See <https://bugs.archlinux.org/> for instructions.

Assigning the result of the conditional expression to another variable and
returning that makes the ICE go away.


More information about the Gcc-bugs mailing list