https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112507
Bug ID: 112507 Summary: Missed optimization of strcpy(3) (or stpcpy(3)) with previous strnlen(3) Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: alx at kernel dot org Target Milestone: --- The following code can be optimized to use memcpy(3), since the length of the copy is known (we've just called strnlen(3), and discarded the possibility of truncated lengths). $ cat strxcpy.c #include <stddef.h> #include <string.h> #include <sys/types.h> ssize_t strxcpy(char *restrict dst, const char *restrict src, size_t dsize) { if (strnlen(src, dsize) == dsize) return -1; return stpcpy(dst, src) - dst; } This compiles to stpcpy(3). Similar code written with strcpy(3) also keeps strcpy(3). But they could be optimized with mempcpy(3) or memcpy(3). On Mon, Nov 13, 2023 at 07:56:00AM +0100, Richard Biener wrote: > It looks like a transform for the strlen pass.