public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "franke at computer dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/97336] New: False positive -Wstring-compare warning for strncmp()
Date: Thu, 08 Oct 2020 11:58:13 +0000	[thread overview]
Message-ID: <bug-97336-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 97336
           Summary: False positive -Wstring-compare warning for strncmp()
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: franke at computer dot org
  Target Milestone: ---

Testcase:

$ uname -srvmo
CYGWIN_NT-10.0 3.1.7(0.340/5/3) 2020-08-22 17:48 x86_64 Cygwin

$ cygcheck -f /usr/bin/gcc
gcc-core-10.2.0-1

$ gcc --version
gcc (GCC) 10.2.0
...

$ cat test.c
int f(const char * p, int n)
{
  char buf[10] = {0, };
  int i;
  for (i = 0; i < (int)sizeof(buf)-1 && i < n; i++)
    buf[i] = p[i];
  if (!__builtin_strncmp(buf, "12345", 5)
      && (i == 5 || buf[5] == ' '))
    return 1;
  return 0;
}

$ gcc -Wstring-compare -O2 -c test.c
test.c: In function ‘f’:
test.c:7:8: warning: ‘__builtin_strncmp’ of strings of length 0 and 5 and bound
of 5 evaluates to nonzero [-Wstring-compare]
    7 |   if (!__builtin_strncmp(buf, "12345", 5)
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Apparently it is assumed that the copy loop is never executed.

Any of the following single line changes removes the warning:

 {
-  char buf[10] = {0, };
+  char buf[10]; __builtin_memset(buf, 0, sizeof(buf));
   int i;


   if (!__builtin_strncmp(buf, "12345", 5)
-      && (i == 5 || buf[5] == ' '))
+      && (buf[5] == ' ' || i == 5))
     return 1;


   if (!__builtin_strncmp(buf, "12345", 5)
-      && (i == 5 || buf[5] == ' '))
+      && (!buf[5] || buf[5] == ' '))
     return 1;

             reply	other threads:[~2020-10-08 11:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-08 11:58 franke at computer dot org [this message]
2020-10-08 17:28 ` [Bug middle-end/97336] " msebor at gcc dot gnu.org
2020-10-09 12:08 ` franke at computer dot org
2020-10-09 15:32 ` msebor at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-97336-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).