gcc-9.2.0 and spurious warnings

Josef Wolf jw@raven.inka.de
Thu Dec 12 08:20:00 GMT 2019


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
      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!

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

    return (p);

/* skip word (separated by spaces/tabs) */
char *skip_word (char *p)
    while (p && *p && (*p!=' ') && (*p!='\t'))

    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 */

Josef Wolf

More information about the Gcc-help mailing list