public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* gcc-9.2.0 and spurious warnings
@ 2019-12-12  8:20 Josef Wolf
  2019-12-12 20:41 ` Jeff Law
  2019-12-13  0:20 ` Martin Sebor
  0 siblings, 2 replies; 8+ messages in thread
From: Josef Wolf @ 2019-12-12  8:20 UTC (permalink / raw)
  To: gcc-help

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!


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

-- 
Josef Wolf
jw@raven.inka.de

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

end of thread, other threads:[~2019-12-15  0:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-12  8:20 gcc-9.2.0 and spurious warnings Josef Wolf
2019-12-12 20:41 ` Jeff Law
2019-12-13  7:10   ` Josef Wolf
2019-12-13  0:20 ` Martin Sebor
2019-12-13  7:00   ` Josef Wolf
2019-12-13  9:55     ` Segher Boessenkool
2019-12-13 10:30       ` Josef Wolf
2019-12-15  0:21     ` Martin Sebor

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