https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85931
Bug ID: 85931
Summary: -Wsizeof-pointer-memaccess for strncpy with size of
source
Product: gcc
Version: 8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: msebor at gcc dot gnu.org
Target Milestone: ---
Since the size of the source is the same as the size of the destination, the
value in issuing a warning in the test case below seems dubious. It could
still be a mistake, but in practical terms whether it's the size of the source
or that of the destination that's used makes no difference. An argument for
not diagnosing it is that specifying the size of the source is also idiomatic
in some code bases.
$ cat t.c && gcc -S -Wall t.c
struct S { char a[8], b[8]; };
void f (struct S *s)
{
__builtin_strncpy (s->a, s->b, sizeof s->b);
}
t.c: In function ‘f’:
t.c:5:41: warning: argument to ‘sizeof’ in ‘__builtin_strncpy’ call is the same
expression as the source; did you mean to use the size of the destination?
[-Wsizeof-pointer-memaccess]
__builtin_strncpy (s->a, s->b, sizeof s->b);
^