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

            Bug ID: 96951
           Summary: strncpy truncation warning does not recognize
                    truncation check
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fw at gcc dot gnu.org
  Target Milestone: ---

This code example produces a warning:

#include <string.h>

struct buffer {
  char string[10];
};

int
f (struct buffer *p, const char *s)
{
  strncpy (p->string, s, sizeof (p->string));
  if (p->string[sizeof (p->string) - 1] != '\0')
    return -1;
  return 0;
}

t.c: In function ‘f’:
t.c:10:3: warning: ‘strncpy’ specified bound 10 equals destination size
[-Wstringop-truncation]
   10 |   strncpy (p->string, s, sizeof (p->string));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There is an explicit truncation check, however, so the warning does not apply.

Suggested by Kim Barrett here:
https://mail.openjdk.java.net/pipermail/hotspot-dev/2020-September/042890.html

-- 
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill

Reply via email to