public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/109208] New: gcc doesn't detect when sizes are booleans
@ 2023-03-20  9:16 dcb314 at hotmail dot com
  2023-03-20 18:00 ` [Bug c/109208] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: dcb314 at hotmail dot com @ 2023-03-20  9:16 UTC (permalink / raw)
  To: gcc-bugs

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))
                                                           ^
$

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c/109208] gcc doesn't detect when sizes are booleans
  2023-03-20  9:16 [Bug c/109208] New: gcc doesn't detect when sizes are booleans dcb314 at hotmail dot com
@ 2023-03-20 18:00 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-20 18:00 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-03-20 18:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-20  9:16 [Bug c/109208] New: gcc doesn't detect when sizes are booleans dcb314 at hotmail dot com
2023-03-20 18:00 ` [Bug c/109208] " pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).