On 12/12/19 1:10 AM, Josef Wolf wrote:
Hello, I upgraded from gcc-8.2.0 to gcc-9.2.0. With gcc-9.2.0, I am getting new warnings for old code, which I have not seen with gcc-8.2.0 and earlier gcc versions. What's more is, that those warnings disappear when OTHER (totally unrelated) parts of the code is removed. Here is one example (some helper functions for parsing). With the code attached below, I get this warning: $ LANG= PATH=$PATH:/usr/local/crossgcc/bin m68k-unknown-elf-gcc -ansi -pedantic -Wall -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -std=c89 -Wnull-dereference -g -O2 -fno-toplevel-reorder -mcpu32 -c -o t.o t.c t.c: In function 'get_word': t.c:53:5: warning: 'strncpy' destination unchanged after copying no bytes [-Wstringop-truncation] 53 | strncpy (*r, p, q-p); /* copy */ | ^~~~~~~~~~~~~~~~~~~~ I don't understand this warning at all. The freshly allocated memory is big enough to hold the copied word including the trailing NUL character. And the NUL character is appended just behind the copied word. What am I supposed to do to get rid of the warning? When I remove the next_word() function, which is TOTALLY UNRELATED to the code in question, the warning disappears!
Jeff already explained what the warning means. I don't see it with fresh trunk, either natively or with an m68k-unknown-elf cross, so I can't really say why GCC 9 thinks the two pointers are equal. You can tell by looking at the optimization dumps (use -ftree-dump-all and grep the output for strncpy with a zero argument). What I can say is that strncpy isn't really meant to be used this way: to copy exactly as many bytes as the third argument says. That's what memcpy is for. The warning was added because strncpy is frequently misused and a common source of bugs. The warning tries to avoid triggering for the safe uses when it can detect they are safe, but it's imperfect and prone to false alarms. To avoid it I would suggest using memcpy instead. It will likely also be faster. Alternatively, you can try calling the function only when the difference is non-zero. Martin
Here is the code: #include <ctype.h> #include <string.h> #include <stdlib.h> char *skip_spaces (char *p); char *skip_word (char *p); char *next_word (char *p); char *get_word (char *p, char **r); void halt_system (int restart, char *fmt, ...) __attribute__((noreturn)); /* skip spaces and tabs */ char *skip_spaces (char *p) { if (!p) return NULL; while (((*p==' ') || (*p=='\t'))) p++; return (p); } /* skip word (separated by spaces/tabs) */ char *skip_word (char *p) { while (p && *p && (*p!=' ') && (*p!='\t')) p++; return (p); } /* serch next word (separated by spaces/tabs) */ char *next_word (char *p) { return (skip_spaces (skip_word (p))); } /* This function will extract a word and store it in freshly allocated memory */ char *get_word (char *p, char **r) { char *q; if (!p) return NULL; p = skip_spaces (p); /* look for start of word */ q = skip_word (p); /* look for end of word */ if (!(*r=malloc (q-p+1))) /* allocate memory */ halt_system (1, "Out of memory in get_word()"); /* !!!!!! The warning at the next line is: 'strncpy' destination unchanged after copying no bytes [-Wstringop-truncation] This warning disappears when the next_word() function is removed. */ strncpy (*r, p, q-p); /* copy */ (*r)[q-p] = 0; /* mark the end */ return (skip_spaces (q)); /* return pointer to next word */ }