public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/107473] New: Unexpected warning / error with strncpy
@ 2022-10-31 14:20 lli at a10networks dot com
  2022-11-10 15:24 ` [Bug tree-optimization/107473] " marxin at gcc dot gnu.org
  2023-06-07  8:12 ` yinyuefengyi at gmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: lli at a10networks dot com @ 2022-10-31 14:20 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 107473
           Summary: Unexpected warning / error with strncpy
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lli at a10networks dot com
  Target Milestone: ---

When I compile this source code, I hit -Werror=stringop-truncation.

#include <string.h>
#include <stdio.h>
#include <limits.h>

static char a[PATH_MAX];
static char b[PATH_MAX];
void my_test() 
{
        snprintf(b, sizeof(b), "/linux/path");
        strncpy(a,b,sizeof(a)-1);
}

root:/sources# gcc -Wall -Werror -Wextra -O2 -c test4.c -o test4.o
test4.c: In function 'my_test':
test4.c:10:9: error: 'strncpy' output may be truncated copying 4095 bytes from
a string of length 4095 [-Werror=stringop-truncation]
   10 |         strncpy(a,b,sizeof(a)-1);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

If I compile same file without O2, there is no such issue.

If I add '\0' to the end of a, there is no such issue.

static char a[PATH_MAX];
static char b[PATH_MAX];
void my_test()
{
        snprintf(b, sizeof(b), "/linux/path");
        strncpy(a,b,sizeof(a)-1);
        a[sizeof(a)-1]='\0';
}

If I add memset before strncpy, I hit the issue again.

static char a[PATH_MAX];
static char b[PATH_MAX];
void my_test()
{
        snprintf(b, sizeof(b), "/linux/path");
        memset(a, 0, sizeof(a));
        strncpy(a,b,sizeof(a)-1);
        a[sizeof(a)-1]='\0';
}

root:/sources# gcc -Wall -Werror -Wextra -O2 -c test4.c -o test4.o
test4.c: In function 'my_test':
test4.c:11:9: error: 'strncpy' output may be truncated copying 4095 bytes from
a string of length 4095 [-Werror=stringop-truncation]
   11 |         strncpy(a,b,sizeof(a)-1);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

GCC info:
root:/sources# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-host-pie
--enable-host-bind-now --enable-languages=c,c++,fortran,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 --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array --without-isl --enable-offload-targets=nvptx-none
--without-cuda-driver --enable-gnu-indirect-function --enable-cet
--with-tune=generic --with-arch_64=x86-64-v2 --with-arch_32=x86-64
--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 11.2.1 20220127 (Red Hat 11.2.1-9) (GCC)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug tree-optimization/107473] Unexpected warning / error with strncpy
  2022-10-31 14:20 [Bug c/107473] New: Unexpected warning / error with strncpy lli at a10networks dot com
@ 2022-11-10 15:24 ` marxin at gcc dot gnu.org
  2023-06-07  8:12 ` yinyuefengyi at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-11-10 15:24 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-11-10
                 CC|                            |marxin at gcc dot gnu.org,
                   |                            |msebor at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #1 from Martin Liška <marxin at gcc dot gnu.org> ---
Started with r10-2814-g22fca489eaf98f26.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug tree-optimization/107473] Unexpected warning / error with strncpy
  2022-10-31 14:20 [Bug c/107473] New: Unexpected warning / error with strncpy lli at a10networks dot com
  2022-11-10 15:24 ` [Bug tree-optimization/107473] " marxin at gcc dot gnu.org
@ 2023-06-07  8:12 ` yinyuefengyi at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: yinyuefengyi at gmail dot com @ 2023-06-07  8:12 UTC (permalink / raw)
  To: gcc-bugs

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

Xionghu Luo (luoxhu at gcc dot gnu.org) <yinyuefengyi at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yinyuefengyi at gmail dot com

--- Comment #2 from Xionghu Luo (luoxhu at gcc dot gnu.org) <yinyuefengyi at gmail dot com> ---
*** Bug 110151 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-06-07  8:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-31 14:20 [Bug c/107473] New: Unexpected warning / error with strncpy lli at a10networks dot com
2022-11-10 15:24 ` [Bug tree-optimization/107473] " marxin at gcc dot gnu.org
2023-06-07  8:12 ` yinyuefengyi at gmail dot com

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).