public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/101451] New: Incorrect -Wstringop-truncation warning
@ 2021-07-14 13:26 quentin at armitage dot org.uk
  2021-08-11  2:18 ` [Bug tree-optimization/101451] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: quentin at armitage dot org.uk @ 2021-07-14 13:26 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101451
           Summary: Incorrect -Wstringop-truncation warning
           Product: gcc
           Version: 11.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: quentin at armitage dot org.uk
  Target Milestone: ---

Created attachment 51151
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51151&action=edit
The .i file produced by the compile command

Command: gcc -v -save-temps -Wstringop-truncation -O2 -o strncat1 strncat1.c

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-languages=c,c++,fortran,objc,obj-c++,ada,go,d,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
--with-isl=/builddir/build/BUILD/gcc-11.1.1-20210531/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --enable-cet --with-tune=generic
--with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.1.1 20210531 (Red Hat 11.1.1-3) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o'
'strncat1' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/11/cc1 -E -quiet -v strncat1.c
-mtune=generic -march=x86-64 -Wstringop-truncation -O2 -fpch-preprocess -o
strncat1.i
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/11/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/11/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/11/include
 /usr/local/include
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o'
'strncat1' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/11/cc1 -fpreprocessed strncat1.i -quiet
-dumpbase strncat1.c -dumpbase-ext .c -mtune=generic -march=x86-64 -O2
-Wstringop-truncation -version -o strncat1.s
GNU C17 (GCC) version 11.1.1 20210531 (Red Hat 11.1.1-3) (x86_64-redhat-linux)
        compiled by GNU C version 11.1.1 20210531 (Red Hat 11.1.1-3), GMP
version 6.2.0, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version
isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C17 (GCC) version 11.1.1 20210531 (Red Hat 11.1.1-3) (x86_64-redhat-linux)
        compiled by GNU C version 11.1.1 20210531 (Red Hat 11.1.1-3), GMP
version 6.2.0, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version
isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 147699dd2c10be2630d381074a06fb9f
strncat1.c: In function ‘bad’:
strncat1.c:12:30: warning: ‘strncat’ output may be truncated copying 15 bytes
from a string of length 15 [-Wstringop-truncation]
   12 |         do { dest[0] = '\0'; strncat(dest, src, sizeof(dest) - 1); }
while (0);
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o'
'strncat1' '-mtune=generic' '-march=x86-64'
 as -v --64 -o strncat1.o strncat1.s
GNU assembler version 2.35.1 (x86_64-redhat-linux) using BFD version version
2.35.1-41.fc34
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/11/:/usr/libexec/gcc/x86_64-redhat-linux/11/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/11/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/11/:/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/11/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o'
'strncat1' '-mtune=generic' '-march=x86-64' '-dumpdir' 'strncat1.'
 /usr/libexec/gcc/x86_64-redhat-linux/11/collect2 -plugin
/usr/libexec/gcc/x86_64-redhat-linux/11/liblto_plugin.so
-plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
-plugin-opt=-fresolution=strncat1.res -plugin-opt=-pass-through=-lgcc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id
--no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker
/lib64/ld-linux-x86-64.so.2 -o strncat1
/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/crt1.o
/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/crti.o
/usr/lib/gcc/x86_64-redhat-linux/11/crtbegin.o
-L/usr/lib/gcc/x86_64-redhat-linux/11
-L/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64 -L/lib/../lib64
-L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/11/../../.. strncat1.o
-lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state
--as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-redhat-linux/11/crtend.o
/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/crtn.o
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o'
'strncat1' '-mtune=generic' '-march=x86-64' '-dumpdir' 'strncat1.'



System type
===========
Fedora 34

Command line triggering bug
===========================
gcc -Wstringop-truncation -O2 -o strncat1 strncat1.c

Expected behaviour
==================
No -Wstringop-truncation warning

Actual behaviour
================
A -Wstringop-truncation warning is issued

Other information
=================
The warning is not issued unless both -Wstringop-truncation and -O2 are
specified.

In my code I also get a -Wstringop-truncation warning when using strcpy_safe
defined as follows:
#define strcpy_safe(dst, src) \
        do { strncpy(dst, src, sizeof(dst) - 1); dst[sizeof(dst) - 1] = '\0'; }
while (0)

However, I cannot produce a simple test case to reproduce it.

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

* [Bug tree-optimization/101451] Incorrect -Wstringop-truncation warning
  2021-07-14 13:26 [Bug c/101451] New: Incorrect -Wstringop-truncation warning quentin at armitage dot org.uk
@ 2021-08-11  2:18 ` pinskia at gcc dot gnu.org
  2021-08-12 14:06 ` quentin at armitage dot org.uk
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-11  2:18 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-08-11
             Status|UNCONFIRMED                 |WAITING
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The warning in the attached testcase is correct.  strncat if truncated does not
put a null character.
The reason why you only get the warning once is because GCC decided the
functions are the same. If you swap the order of good and bad in the source you
will get a warning for the good function.

If you add:
dest[sizeof(dest) - 1] = 0;
After the strncat, the warning goes away.


strncat in this case will only do "sizeof(dest) - 1" in copying and will not
copy the null character.

>However, I cannot produce a simple test case to reproduce it.
Do you have a full testcase that you can share, we will try to reduce it and
see why it is still failing.

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

* [Bug tree-optimization/101451] Incorrect -Wstringop-truncation warning
  2021-07-14 13:26 [Bug c/101451] New: Incorrect -Wstringop-truncation warning quentin at armitage dot org.uk
  2021-08-11  2:18 ` [Bug tree-optimization/101451] " pinskia at gcc dot gnu.org
@ 2021-08-12 14:06 ` quentin at armitage dot org.uk
  2021-08-12 14:08 ` quentin at armitage dot org.uk
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: quentin at armitage dot org.uk @ 2021-08-12 14:06 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Quentin Armitage <quentin at armitage dot org.uk> ---
Created attachment 51297
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51297&action=edit
ipvswrapper.i for strncpy truncated warning

The following warning is generated:

gcc -c -o ipvswrapper.o1 -O2 -Wstringop-truncation ipvswrapper.i
In file included from /usr/include/string.h:519,
                 from ../../lib/timer.h:28,
                 from ../../keepalived/include/vrrp.h:40,
                 from ../../keepalived/include/ipvswrapper.h:29,
                 from ipvswrapper.c:34:
In function ‘strncpy’,
    inlined from ‘ipvs_set_srule’ at ipvswrapper.c:494:7:
/usr/include/bits/string_fortified.h:95:10: warning: ‘__builtin_strncpy’ output
may be truncated copying 15 bytes from a string of length 15
[-Wstringop-truncation]
   95 |   return __builtin___strncpy_chk (__dest, __src, __len,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   96 |                                   __glibc_objsize (__dest));
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~

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

* [Bug tree-optimization/101451] Incorrect -Wstringop-truncation warning
  2021-07-14 13:26 [Bug c/101451] New: Incorrect -Wstringop-truncation warning quentin at armitage dot org.uk
  2021-08-11  2:18 ` [Bug tree-optimization/101451] " pinskia at gcc dot gnu.org
  2021-08-12 14:06 ` quentin at armitage dot org.uk
@ 2021-08-12 14:08 ` quentin at armitage dot org.uk
  2021-08-12 20:53 ` msebor at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: quentin at armitage dot org.uk @ 2021-08-12 14:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Quentin Armitage <quentin at armitage dot org.uk> ---
According to the man page for strncat:
  As with strcat(), the resulting string in dest is always null-terminated.

  If  src  contains  n  or  more  bytes,  strncat()  writes  n+1  bytes to dest
(n from src plus the terminating null byte).  Therefore, the size of dest must
be at least strlen(dest)+n+1.

Based on the above, in the test case strncat should copy 15 bytes (sizeof(dest)
- 1), and then add a terminating null byte.

I think the following code snippet demonstrates that:

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


int main(__attribute__((unused)) int argc, __attribute__((unused)) char **argv)
{
        char dst[16] = "012345678901234";
        unsigned char *p = dst;

        dst[5] = '\0';

        strncat(dst, "abcdefg", 5);
        for (int i = 0; i < 16; i++)
                printf("0x%2.2x ", *p++);
        printf("\n");
}

>Do you have a full testcase that you can share, we will try to reduce it and see why it is still failing.

I have attached ipvswrapper.i, which when compiled with -O2
--Wstringop-truncation produces:
/usr/include/bits/string_fortified.h:95:10: warning: ‘__builtin_strncpy’ output
may be truncated copying 15 bytes from a string of length 15
[-Wstringop-truncation]
   95 |   return __builtin___strncpy_chk (__dest, __src, __len,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   96 |                                   __glibc_objsize (__dest));

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

* [Bug tree-optimization/101451] Incorrect -Wstringop-truncation warning
  2021-07-14 13:26 [Bug c/101451] New: Incorrect -Wstringop-truncation warning quentin at armitage dot org.uk
                   ` (2 preceding siblings ...)
  2021-08-12 14:08 ` quentin at armitage dot org.uk
@ 2021-08-12 20:53 ` msebor at gcc dot gnu.org
  2021-08-12 21:05 ` quentin at armitage dot org.uk
  2021-08-12 21:09 ` quentin at armitage dot org.uk
  5 siblings, 0 replies; 7+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-08-12 20:53 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
                 CC|                            |msebor at gcc dot gnu.org
             Status|WAITING                     |RESOLVED
             Blocks|                            |88781

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
The warning is by design.  For strncat() which always appends a nul it's
documented to trigger if the call truncates the source string.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88781
[Bug 88781] [meta-bug] bogus/missing -Wstringop-truncation warnings

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

* [Bug tree-optimization/101451] Incorrect -Wstringop-truncation warning
  2021-07-14 13:26 [Bug c/101451] New: Incorrect -Wstringop-truncation warning quentin at armitage dot org.uk
                   ` (3 preceding siblings ...)
  2021-08-12 20:53 ` msebor at gcc dot gnu.org
@ 2021-08-12 21:05 ` quentin at armitage dot org.uk
  2021-08-12 21:09 ` quentin at armitage dot org.uk
  5 siblings, 0 replies; 7+ messages in thread
From: quentin at armitage dot org.uk @ 2021-08-12 21:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Quentin Armitage <quentin at armitage dot org.uk> ---
In the code of my original example (which I have simplified)

===================================
#include <string.h>

static char dest[16];
static char src[16] = "012345678901234";

int main(__attribute__(void)
{
  do { dest[0] = '\0'; strncat(dest, src, sizeof(dest) - 1); } while (0);
}
==================================

there is no truncation since 15 bytes are copied to dest, with a NUL byte
added, and the maximum length of a string in src is 15 bytes plus terminating
NUL byte.

Also, the warning is not generated if -O2 is not specified, but it is generated
if -O2 is specified (-O1 does not generate the warning). To me this doesn't
seem right.

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

* [Bug tree-optimization/101451] Incorrect -Wstringop-truncation warning
  2021-07-14 13:26 [Bug c/101451] New: Incorrect -Wstringop-truncation warning quentin at armitage dot org.uk
                   ` (4 preceding siblings ...)
  2021-08-12 21:05 ` quentin at armitage dot org.uk
@ 2021-08-12 21:09 ` quentin at armitage dot org.uk
  5 siblings, 0 replies; 7+ messages in thread
From: quentin at armitage dot org.uk @ 2021-08-12 21:09 UTC (permalink / raw)
  To: gcc-bugs

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

Quentin Armitage <quentin at armitage dot org.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |---

--- Comment #6 from Quentin Armitage <quentin at armitage dot org.uk> ---
I am reopening due to my last comment, and also the strncpy issue is not
resolved.

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

end of thread, other threads:[~2021-08-12 21:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-14 13:26 [Bug c/101451] New: Incorrect -Wstringop-truncation warning quentin at armitage dot org.uk
2021-08-11  2:18 ` [Bug tree-optimization/101451] " pinskia at gcc dot gnu.org
2021-08-12 14:06 ` quentin at armitage dot org.uk
2021-08-12 14:08 ` quentin at armitage dot org.uk
2021-08-12 20:53 ` msebor at gcc dot gnu.org
2021-08-12 21:05 ` quentin at armitage dot org.uk
2021-08-12 21:09 ` quentin at armitage dot org.uk

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