Skip to content

Commit 6c0bb5e

Browse files
Fix #12365 checkLibraryFunction for std::string constructor (#5893)
I wonder why both `%name%` and `%type%` match `return`.
1 parent cae6719 commit 6c0bb5e

4 files changed

Lines changed: 15 additions & 2 deletions

File tree

lib/checkfunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ void CheckFunctions::checkLibraryMatchFunctions()
662662
continue;
663663

664664
const Token* start = tok;
665-
while (Token::Match(start->tokAt(-2), "%name% ::"))
665+
while (Token::Match(start->tokAt(-2), "%name% ::") && !start->tokAt(-2)->isKeyword())
666666
start = start->tokAt(-2);
667667
if (mSettings->library.detectContainerOrIterator(start))
668668
continue;

lib/checkleakautovar.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,8 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin
939939
const bool isLeakIgnore = mSettings->library.isLeakIgnore(mSettings->library.getFunctionName(tokName));
940940
if (mSettings->library.getReallocFuncInfo(tokName))
941941
return;
942+
if (tokName->next()->valueType() && tokName->next()->valueType()->container && tokName->next()->valueType()->container->stdStringLike)
943+
return;
942944

943945
const Token * const tokFirstArg = tokOpeningPar->next();
944946
if (!tokFirstArg || tokFirstArg->str() == ")") {

test/testfunctions.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,11 @@ class TestFunctions : public TestFixture {
20412041
" if (t.at(0)) {}\n"
20422042
"};\n", "test.cpp", &s);
20432043
ASSERT_EQUALS("", errout.str());
2044+
2045+
check("::std::string f(const char* c) {\n" // #12365
2046+
" return ::std::string(c);\n"
2047+
"}\n", "test.cpp", &s);
2048+
ASSERT_EQUALS("", errout.str());
20442049
}
20452050

20462051
void checkUseStandardLibrary1() {

test/testleakautovar.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1757,8 +1757,14 @@ class TestLeakAutoVar : public TestFixture {
17571757
" auto s = new S;\n"
17581758
" v.push_back(std::unique_ptr<S>(s));\n"
17591759
" }\n"
1760-
"}\n", &s);
1760+
"}\n", /*cpp*/ true, &s);
17611761
ASSERT_EQUALS("", errout.str()); // don't crash
1762+
1763+
check("void g(size_t len) {\n" // #12365
1764+
" char* b = new char[len + 1]{};\n"
1765+
" std::string str = std::string(b);\n"
1766+
"}", /*cpp*/ true, &s);
1767+
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: b\n", errout.str());
17621768
}
17631769

17641770
void goto1() {

0 commit comments

Comments
 (0)