public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100197] New: g++ emits spurious Wstring-compare warnings on strcmp()
@ 2021-04-22  3:35 jaf at meyersound dot com
  2021-04-22 16:03 ` [Bug tree-optimization/100197] " msebor at gcc dot gnu.org
  2022-07-28 12:28 ` rajpal.gusain at gmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: jaf at meyersound dot com @ 2021-04-22  3:35 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100197
           Summary: g++ emits spurious Wstring-compare warnings on
                    strcmp()
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jaf at meyersound dot com
  Target Milestone: ---

Created attachment 50653
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50653&action=edit
preprocessed file from teststring.cpp

Tested with g++ (Ubuntu 10.20-13ubuntu1) 10.2.0 (as installed via "sudp apt-get
install g++") on an Ubuntu 20.10 64-bit VM running inside VMWare Fusion on a
2018 Mac Mini.

To reproduce, compile the attached .cpp file, like this:

$ g++ -O2 -W -c teststring.cpp
In member function ‘bool String::operator==(const char*) const’,
    inlined from ‘int main(int, char**)’ at teststring.cpp:52:21:
teststring.cpp:27:21: warning: ‘int strcmp(const char*, const char*)’ of a
string of length 10 and an array of size 8 evaluates to nonzero
[-Wstring-compare]
   27 |       return (strcmp(myStr, rhs) == 0);
      |               ~~~~~~^~~~~~~~~~~~
teststring.cpp: In function ‘int main(int, char**)’:
teststring.cpp:52:16: note: in this expression
   52 |    if ((rand())&&(s == "1234567890")) printf("Mwahey!\n");
      |        ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~

Note that the emitted warnings are (AFAICT) spurious, because in the example
program the two strings that get passed to strcmp() are both "1234567890".  The
8-byte array (_smallBuffer[8]) that the warning mentions is not actually used.

It seems like maybe the compiler is assuming that the
IsArrayDynamicallyAllocated() method in the example will always return false,
which is not a valid assumption.

The fault only happens when I specifed -O2 or -O3, it doesn't happen if I leave
optimization disabled or specify -O1.

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

end of thread, other threads:[~2022-07-28 12:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-22  3:35 [Bug c++/100197] New: g++ emits spurious Wstring-compare warnings on strcmp() jaf at meyersound dot com
2021-04-22 16:03 ` [Bug tree-optimization/100197] " msebor at gcc dot gnu.org
2022-07-28 12:28 ` rajpal.gusain at gmail dot com

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).