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

            Bug ID: 109208
           Summary: gcc doesn't detect when sizes are booleans
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dcb314 at hotmail dot com
  Target Milestone: ---

Some standard C functions accept sizes as arguments. memcmp, strcmp etc.

gcc doesn't detect when programmers get the ( and ) wrong and provide a
boolean.

An example would be:

# include <memory.h>

extern void g( const char *);

void f( const char * response)
{
        if (!response || strncmp(response, "GDM ", strlen("GDM ") != 0)) 
        {
                g( response);
        }
}

$ ~/gcc/results/bin/gcc -c -g -O2 -Wall -Wextra mar20b.cc
$ 

Here is clang:

$ clang++ -c  mar20b.cc
mar20b.cc:7:60: warning: size argument in 'strncmp' call is a comparison
[-Wmemsize-comparison]
        if (!response || strncmp(response, "GDM ", strlen("GDM ") != 0)) 
                                                   ~~~~~~~~~~~~~~~^~~~
mar20b.cc:7:19: note: did you mean to compare the result of 'strncmp' instead?
        if (!response || strncmp(response, "GDM ", strlen("GDM ") != 0)) 
                         ^                                            ~
                                                                 )
mar20b.cc:7:45: note: explicitly cast the argument to size_t to silence this
warning
        if (!response || strncmp(response, "GDM ", strlen("GDM ") != 0)) 
                                                   ^
                                                   (size_t)(          )
1 warning generated.

and here is cppcheck:

$ ~/cppcheck/trunk/cppcheck mar20b.cc
mar20b.cc:7:60: error: Invalid strncmp() argument nr 3. A non-boolean value is
required. [invalidFunctionArgBool]
 if (!response || strncmp(response, "GDM ", strlen("GDM ") != 0))
                                                           ^
$

Reply via email to