public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "gburca-gnu at ebixio dot com" <gcc-bugzilla@gcc.gnu.org> 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 [thread overview] Message-ID: <bug-109609-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109609 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 using the commented out strncpy() call, the output is as expected. See also how the generated assembly changes when the printf() inside the function is commented out. Expected output: Src: edcba Dst: edcba Actual output: Src: edcba Dst: <random uninitialized data> https://godbolt.org/z/5E51Gdh9s Compile args: g++ -O2 file.cpp 8<----------- file.cpp -------------- #include <cstring> #include <stdio.h> #include <algorithm> static constexpr unsigned N = 23; char dst[N + 1]; void invert(const char *id) { constexpr int MAX_LEN = 13; char buf[MAX_LEN]; char *ptr = buf + sizeof(buf); // start from the end of buf *(--ptr) = '\0'; // terminate string while (*id && ptr > buf) { *(--ptr) = *(id++); // copy id backwards } printf("Src: %s\n", ptr); strncpy(dst, ptr, N); // copy ptr/buf to dst //strncpy(dst, ptr, std::min<size_t>(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=/home/gb/.fighome/runtime/gcc/12.2.0-1/bin/g++ COLLECT_LTO_WRAPPER=/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/staging --build=x86_64-linux-gnu --disable-multilib --disable-multiarch --enable-clocale=gnu --enable-languages=c,c++,fortran --enable-ld=yes --enable-gold=yes --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-linker-build-id --enable-lto --enable-plugins --enable-threads=posix --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-pkgversion=''\''internal_build'\''' --with-system-zlib --disable-werror --with-libelf=/data/work/chhq-sudbld10-001-CENTOS7/15df1254b8bb2e5c/scratch/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='-v' '-save-temps' '-O2' '-shared-libgcc' '-mtune=generic' '-march=x86-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=generic -march=x86-64 -O2 -fpch-preprocess
next reply other threads:[~2023-04-24 19:34 UTC|newest] Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-04-24 19:34 gburca-gnu at ebixio dot com [this message] 2023-04-24 19:38 ` [Bug middle-end/109609] " pinskia at gcc dot gnu.org 2023-04-24 19:40 ` pinskia at gcc dot gnu.org 2023-04-24 19:47 ` [Bug middle-end/109609] [12/13/14 Regression] " jakub at gcc dot gnu.org 2023-04-24 19:50 ` pinskia at gcc dot gnu.org 2023-04-24 19:52 ` [Bug middle-end/109609] [12/13/14 Regression] tail call for function even when passing a ptr which references a local array still jakub at gcc dot gnu.org 2023-04-24 19:56 ` pinskia at gcc dot gnu.org 2023-04-24 20:30 ` gburca-gnu at ebixio dot com 2023-04-24 20:52 ` jakub at gcc dot gnu.org 2023-04-25 6:30 ` rguenth at gcc dot gnu.org 2023-04-25 7:06 ` rguenth at gcc dot gnu.org 2023-04-25 7:15 ` rguenth at gcc dot gnu.org 2023-04-25 7:37 ` jakub at gcc dot gnu.org 2023-04-25 7:42 ` jakub at gcc dot gnu.org 2023-04-25 12:54 ` rguenth at gcc dot gnu.org 2023-04-25 14:53 ` cvs-commit at gcc dot gnu.org 2023-04-25 14:53 ` [Bug middle-end/109609] [12/13 " rguenth at gcc dot gnu.org 2023-04-25 16:14 ` gburca-gnu at ebixio dot com 2023-04-26 9:34 ` cvs-commit at gcc dot gnu.org 2023-04-26 10:41 ` cvs-commit at gcc dot gnu.org 2023-04-26 10:43 ` rguenth at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-109609-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).