https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114876
Bug ID: 114876 Summary: -fprintf-return-value mishandles %lc with a '\0' argument. Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: collin.funk1 at gmail dot com Target Milestone: --- I noticed some test failures in some Gnulib test cases earlier [1]. I'm using Fedora 40's GCC 14.0 package. $ uname -a Linux fedora 6.8.7-300.fc40.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 17 19:21:08 UTC 2024 x86_64 GNU/Linux $ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/14/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,m2,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-libstdcxx-backtrace --with-libstdcxx-zoneinfo=/usr/share/zoneinfo --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-14.0.1-20240411/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none,amdgcn-amdhsa --enable-offload-defaulted --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 14.0.1 20240411 (Red Hat 14.0.1-0) (GCC) $ ldd --version ldd (GNU libc) 2.39 Here is a test program: =================================================== int main (void) { char buffer[5000]; wint_t ch = (wint_t) '\0'; int result = sprintf (buffer, "%lc%lc%lc%lc", ch, ch, ch, ch); printf ("%d\n", result); return 0; } =================================================== I believe that this program should print 4. POSIX states "Upon successful completion, the sprintf() function shall return the number of bytes written to s, excluding the terminating null byte." So in total 5 '\0' characters written to the buffer and 4 returned because the extra terminating one is excluded. Here is some runs with different optimization settings: # -O0 passes. $ make gcc -Wall -Wextra -g -O0 -o a.out main.c ./a.out 4 # -01 fails, likewise for -O2 and -O3. $ make gcc -Wall -Wextra -g -O1 -o a.out main.c ./a.out 0 # -O0 -fprintf-return-value passes. $ make gcc -Wall -Wextra -g -O0 -fprintf-return-value -o a.out main.c ./a.out 4 # -01 -fprintf-return-value fails. $ make gcc -Wall -Wextra -g -O1 -fprintf-return-value -o a.out main.c ./a.out 0 # -O1 -fno-printf-return-value passes, likewise for -O2 and -O3 with -fno-printf-return-value. $ make gcc -Wall -Wextra -g -O1 -fno-printf-return-value -o a.out main.c ./a.out 4 I also built a barebones gcc-13.2 compiler from the tarball on the GNU FTP server. It seems to behave in the same way. $ gcc-13.2 -v Using built-in specs. COLLECT_GCC=gcc-13.2 COLLECT_LTO_WRAPPER=/home/collin/.local/libexec/gcc/x86_64-pc-linux-gnu/13.2.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ./configure --prefix=/home/collin/.local --program-suffix=-13.2 --enable-languages=c,c++ --enable-threads=posix --enable-linker-build-id --disable-multilib --disable-multiarch --with-tune=generic --disable-bootstrap Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 13.2.0 (GCC) I've never submitted a GCC bug, so please let me know if you need any more information. Thanks! [1] https://lists.gnu.org/archive/html/bug-gnulib/2024-04/msg00453.html