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

            Bug ID: 92475
           Summary: incorrect code with optimization
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gunther.vo...@ssw-trading.com
  Target Milestone: ---

Created attachment 47219
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47219&action=edit
preprocessed source

In the following code, the branch for length==0 (including the string constant
"AAA") is optimized away. Observed with GCC 8.3.0/1. With GCC 9.2.1 and GCC
7.4.0, the generated code is correct.

    #include <string>

    size_t
    f(char* s)
    {
        ssize_t i;

        for (i = 7; i >= 0; i--) {
                if (s[i] != ' ') {
                        if (i < 7) {
                                s[i + 1] = '\0';
                }
                        break;
                }
        }

        return static_cast<size_t>(i + 1);
    }

    void h(const char*);

    void
    g(char* s)
    {
        size_t length = f(s);
        std::string(s, length); // bug disappears when this line is removed
        h(length ? "BBB" : "AAA");
    }

Reply via email to