From 976966fe8111342327bc0f73e3774f12f4c086ed Mon Sep 17 00:00:00 2001 From: amai2012 Date: Sat, 23 Aug 2014 09:41:40 +0200 Subject: [PATCH] #5639 String literal compared with char buffer in a struct. --- lib/checkother.cpp | 2 +- test/testother.cpp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 474ea17aa..4e64e0789 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2960,7 +2960,7 @@ void CheckOther::checkSuspiciousStringCompare() const std::string varname = varTok->expressionString(); if (litTok->type() == Token::eString) { - if (_tokenizer->isC() || (var && var->isPointer())) + if (_tokenizer->isC() || (var && var->isArrayOrPointer())) suspiciousStringCompareError(tok, varname); } else if (litTok->originalName() == "'\\0'" && var && var->isPointer()) { suspiciousStringCompareError_char(tok, varname); diff --git a/test/testother.cpp b/test/testother.cpp index dbd74ea7a..0212f6493 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5327,6 +5327,24 @@ private: check("int foo(char c) {\n" "return c == \"42\"[0];}", "test.c", false, true, false, false); ASSERT_EQUALS("", errout.str()); + + // 5639 String literal compared with char buffer in a struct + check("struct Example {\n" + " char buffer[200];\n" + "};\n" + "void foo() {\n" + " struct Example example;\n" + " if (example.buffer == \"test\") ;\n" + "}\n", "test.cpp", false, true, false, false); + ASSERT_EQUALS("[test.cpp:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout.str()); + check("struct Example {\n" + " char buffer[200];\n" + "};\n" + "void foo() {\n" + " struct Example example;\n" + " if (example.buffer == \"test\") ;\n" + "}\n", "test.c", false, true, false, false); + ASSERT_EQUALS("[test.c:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout.str()); } void suspiciousStringCompare_char() {