Skip to content

Commit d840005

Browse files
committed
Fixed danmar#7656 (stlcstr - false positive)
1 parent f68fa72 commit d840005

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/checkstl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,17 +1100,17 @@ void CheckStl::string_c_str()
11001100
}
11011101

11021102
bool local = false;
1103-
bool ptr = false;
1103+
bool ptrOrRef = false;
11041104
const Variable* lastVar = nullptr;
11051105
const Function* lastFunc = nullptr;
11061106
bool funcStr = false;
11071107
if (Token::Match(tok2, "%var% .")) {
11081108
local = isLocal(tok2);
1109-
ptr = tok2->variable() && tok2->variable()->isPointer();
1109+
ptrOrRef = tok2->variable() && (tok2->variable()->isPointer() || tok2->variable()->isReference());
11101110
}
11111111
while (tok2) {
11121112
if (Token::Match(tok2, "%var% .|::")) {
1113-
if (ptr)
1113+
if (ptrOrRef)
11141114
local = false;
11151115
lastVar = tok2->variable();
11161116
tok2 = tok2->tokAt(2);

test/teststl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,16 @@ class TestStl : public TestFixture {
23652365
" return mapInfo.author.c_str();\n"
23662366
"}");
23672367
ASSERT_EQUALS("[test.cpp:6]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
2368+
2369+
check("struct S {\n" // #7656
2370+
" std::string data;\n"
2371+
"};\n"
2372+
"const S& getS();\n"
2373+
"const char* test() {\n"
2374+
" const struct S &s = getS();\n"
2375+
" return s.data.c_str();\n"
2376+
"}");
2377+
ASSERT_EQUALS("", errout.str());
23682378
}
23692379

23702380
void autoPointer() {

0 commit comments

Comments
 (0)