From 40719c56dbb3461549869d213c9dd8f1b116098e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 28 Nov 2012 08:48:48 +0100 Subject: [PATCH] Fixed #4183 (false positive with method named c_str()) --- lib/checkstl.cpp | 7 +++++-- test/teststl.cpp | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index bb0d3c560..ccab39baa 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1194,8 +1194,11 @@ void CheckStl::string_c_str() tok2 = tok->next()->link(); else tok2 = tok2->previous(); - if (tok2 && Token::simpleMatch(tok2->tokAt(-4), ". c_str ( )")) - string_c_strParam(tok, i->second); + if (tok2 && Token::simpleMatch(tok2->tokAt(-4), ". c_str ( )")) { + const Variable* var = symbolDatabase->getVariableFromVarId(tok2->tokAt(-5)->varId()); + if (var && Token::Match(var->typeStartToken(), "const| std :: string|wstring")) + string_c_strParam(tok, i->second); + } } } diff --git a/test/teststl.cpp b/test/teststl.cpp index c5ffaa442..3d45eb4cc 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -1864,6 +1864,15 @@ private: " return hello().c_str();\n" "}\n"); ASSERT_EQUALS("[test.cpp:11]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str()); + + // #4183 - using MyStringClass.c_str() + check("void a(const std::string &str);\n" + "\n" + "void b() {\n" + " MyStringClass s;\n" + " a(s.c_str());\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void autoPointer() {