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.