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

            Bug ID: 81391
           Summary: Use of parenthesis disables warning about incorrect
                    size parameter
           Product: gcc
           Version: 5.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugzi...@poradnik-webmastera.com
  Target Milestone: ---

When code below is compiled, gcc complains only about first call to strncpy.
2nd call has the same problem as the 1st one (sizeof pointer used as buffer
size), but there is no warning about it - parenthesis added around sizeof()
silenced this warning. It is recommended to use additional parenthesis around
params in macrofunctions, so some bugs there may went unnoticed.

Tested with gcc 4.8.5 (Linux) and gcc 5.4.0 (Cygwin).

#include <string.h>

int main()
{
    char buf[10], *ptr = buf;
    strncpy(ptr, "a", sizeof(ptr));
    strncpy(ptr, "a", (sizeof(ptr)));

    return 0;
}

$ gcc -o test test.c -Wall -O2
test.c: In function ‘main’:
test.c:6:29: warning: argument to ‘sizeof’ in ‘strncpy’ call is the same
expression as the destination; did you mean to provide an explicit length?
[-Wsizeof-pointer-memaccess]
     strncpy(ptr, "a", sizeof(ptr));
                             ^

Reply via email to