[Bug c/54381] New: -Wsizeof-pointer-memaccess refers to "destination" for strncmp

gerald at pfeifer dot com gcc-bugzilla@gcc.gnu.org
Sun Aug 26 23:31:00 GMT 2012


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54381

             Bug #: 54381
           Summary: -Wsizeof-pointer-memaccess refers to "destination" for
                    strncmp
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: gerald@pfeifer.com


In this example

  #include <string.h>

  int f(const char *s) {
    const char *t = "a string";

    return strncmp(t,s,sizeof t);
  }

-Wsizeof-pointer-memaccess nicely detects that sizeof probably is
not what the programmer meant.

  x.c: In function ‘f’:
  x.c:6:29: warning: argument to ‘sizeof’ in ‘strncmp’ call is the same
expression as the destination; did you mean to provide an explicit
length?[-Wsizeof-pointer-memaccess]
   return strncmp(t,s,sizeof t);
                             ^

However, t is not really a "destination".  The first two parameters
of strcmp and strncmp both are sources.



More information about the Gcc-bugs mailing list