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

--- Comment #6 from anon63 <anon63 at protonmail dot com> ---
Dear Martin, 

Thank you for all these details.

Well, re-reading what I wrote in Comment 0, I think I should explain what I
meant by "Lots of codebases can't be compiled with -Werror flag now.".

Since the gcc-7 -> gcc-8 transition on my laptop, I see a lot of these warnings
(and a Google search on -Wstringop-truncation gives lots of results for 2018)
but I'm working on a project where security matters (IoT OS) and we use -Werror
on Travis-CI but that does not reflect accurately the common case. My
assumption "Lots of codebases can't be compiled with -Werror flag now." was not
"I have seen lots of codebases where the build fails" but rather "It will not
be easy for those projects to use more strict CFLAGS in a next future". I am
not a distribution packages maintainer so I am probably not the right person
for an informed point of view.

Anyway, thanks a lot for your time. As I was more interested by the
global/local variable case than by the constant case, I think I will go for
something strange like this : 

////////////////////////////////////////////////////////
char *strncpy (char *, const char *, __SIZE_TYPE__);

struct S {
    char dest[5];
};

const char src[] = "1234567890";
void function(struct S *s)
{
    const char *src2=src;                    // this indirection avoids a gcc
>= 8
    strncpy(s->dest,src2,sizeof(s->dest)-1); // false positive
stringop-truncation warning
    s->dest[sizeof(s->dest)-1]='\0';
}
////////////////////////////////////////////////////////

and the indirection doesn't change the assembly : 

////////////////////////////////////////////////////////
gcc -O2 -Wall -Wextra -c a.c && objdump -d a.o

a.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <function>:
   0:   c7 07 31 32 33 34       movl   $0x34333231,(%rdi)
   6:   c6 47 04 00             movb   $0x0,0x4(%rdi)
   a:   c3                      retq   
////////////////////////////////////////////////////////

Best,

Reply via email to