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

            Bug ID: 106350
           Summary: O3 memcpy warning when prepending a length-1 string
                    literal to a constant std::string
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: me+gccbugzilla at s5 dot pm
  Target Milestone: ---

Created attachment 53318
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53318&action=edit
Log produced by Compiler Explorer when using gcc (trunk)

The following code produces a warning under `-std=c++20 -O3 -Wrestrict`:

    #include <string>

    std::string memcpy_bug() {
        return "x" + std::string("yz");
    }

(1) If `"x"` is changed to be a string literal of any length other than 1, the
warning does not occur
(2) If `std::string("yz")` is pulled out into a variable (i.e. `std::string
var("yz");` and `return "x" + var;`) the warning does not occur
(3) If the optimization mode used is O1, O2, or Os, the warning does not occur
(4) If the compiler version is downgraded to GCC 11.3, the warning does not
occur

Appending a length-1 literal does not have this issue.

Attached is the log produced by Compiler Explorer on GCC trunk commit
c70a48a8f8f6a43b35f783b5672c9a3c0a363c31.

Possibly related to <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329>. I
made the decision to submit this independently due to (2), but I don't doubt
that it's likely a duplicate.

Reply via email to