From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id B12BC385842B; Mon, 24 Apr 2023 19:34:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B12BC385842B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682364842; bh=2HSEkb3MrlXPYLGoT8pBtPvMMrBmGw9wPYKZi2gy4Ms=; h=From:To:Subject:Date:From; b=n48XriIDVUnMFtJuJGuvR8xvche7vO5yHFXtg8LO5gEJ/nsfz2hjX/CHM5+84mDW9 PcMWH4+9Pfedu5eLLw3wGEhx/qwqgjKrG7DQDpJDCqaWw62x4bY495ds+L+teKGhj3 llbbI3dsCDQpOKuIBeRayhLbfDZr5T8Hfxk2DB+c= From: "gburca-gnu at ebixio dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/109609] New: Invalid strncpy/strncat optimization in GCC 12 Date: Mon, 24 Apr 2023 19:34:01 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: gburca-gnu at ebixio dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109609 Bug ID: 109609 Summary: Invalid strncpy/strncat optimization in GCC 12 Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: gburca-gnu at ebixio dot com Target Milestone: --- The following code prints uninitialized garbage when compiled with -O2 or -= O3 in all versions of GCC 12 (including trunk). It works correctly in GCC 11. Replacing strncpy() with strncat() gives the same results, however when usi= ng the commented out strncpy() call, the output is as expected. See also how t= he generated assembly changes when the printf() inside the function is comment= ed out. Expected output: Src: edcba Dst: edcba Actual output: Src: edcba Dst: https://godbolt.org/z/5E51Gdh9s Compile args: g++ -O2 file.cpp 8<----------- file.cpp -------------- #include #include #include static constexpr unsigned N =3D 23; char dst[N + 1]; void invert(const char *id) { constexpr int MAX_LEN =3D 13; char buf[MAX_LEN]; char *ptr =3D buf + sizeof(buf); // start from the end of buf *(--ptr) =3D '\0'; // terminate string while (*id && ptr > buf) { *(--ptr) =3D *(id++); // copy id backwards } printf("Src: %s\n", ptr); strncpy(dst, ptr, N); // copy ptr/buf to dst //strncpy(dst, ptr, std::min(N, strlen(ptr))); } int main() { invert("abcde"); printf("Dst: %s\n", dst); } 8<----------- file.cpp -------------- g++ -v -save-temps -O2 file.cpp Using built-in specs. COLLECT_GCC=3D/home/gb/.fighome/runtime/gcc/12.2.0-1/bin/g++ COLLECT_LTO_WRAPPER=3D/home/gb/.fighome/runtime/gcc/12.2.0-1/bin/../libexec= /gcc/x86_64-linux-gnu/12.2.0/lto-wrapper Target: x86_64-linux-gnu Configured with: ../gcc-12.2.0/configure --prefix /data/work/chhq-sudbld10-001-CENTOS7/15df1254b8bb2e5c/scratch/gcc/12.2.0/st= aging --build=3Dx86_64-linux-gnu --disable-multilib --disable-multiarch --enable-clocale=3Dgnu --enable-languages=3Dc,c++,fortran --enable-ld=3Dyes --enable-gold=3Dyes --enable-libstdcxx-debug --enable-libstdcxx-time=3Dyes --enable-linker-build-id --enable-lto --enable-plugins --enable-threads=3Dp= osix --host=3Dx86_64-linux-gnu --target=3Dx86_64-linux-gnu --with-pkgversion=3D''\''internal_build'\''' --with-system-zlib --disable-w= error --with-libelf=3D/data/work/chhq-sudbld10-001-CENTOS7/15df1254b8bb2e5c/scrat= ch/gcc/12.2.0/build/libelf-0.8.13 Thread model: posix Supported LTO compression algorithms: zlib gcc version 12.2.0 ('internal_build') COLLECT_GCC_OPTIONS=3D'-v' '-save-temps' '-O2' '-shared-libgcc' '-mtune=3Dg= eneric' '-march=3Dx86-64' /home/gb/.fighome/runtime/gcc/12.2.0-1/bin/../libexec/gcc/x86_64-linux-gnu/= 12.2.0/cc1plus -E -quiet -v -iprefix /home/gb/.fighome/runtime/gcc/12.2.0-1/bin/../lib/gcc/x86_64-linux-gnu/12.2= .0/ -D_GNU_SOURCE file.cpp -mtune=3Dgeneric -march=3Dx86-64 -O2 -fpch-preproces= s=