File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed
Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -1106,7 +1106,14 @@ void CheckStl::string_c_str()
11061106 bool funcStr = false ;
11071107 if (Token::Match (tok2, " %var% ." )) {
11081108 local = isLocal (tok2);
1109- ptrOrRef = tok2->variable () && (tok2->variable ()->isPointer () || tok2->variable ()->isReference ());
1109+ bool refToNonLocal = false ;
1110+ if (tok2->variable () && tok2->variable ()->isReference ()) {
1111+ const Token *refTok = tok2->variable ()->nameToken ();
1112+ refToNonLocal = true ; // safe assumption is default to avoid FPs
1113+ if (Token::Match (refTok, " %var% = %var% .|;|[" ))
1114+ refToNonLocal = !isLocal (refTok->tokAt (2 ));
1115+ }
1116+ ptrOrRef = refToNonLocal || (tok2->variable () && tok2->variable ()->isPointer ());
11101117 }
11111118 while (tok2) {
11121119 if (Token::Match (tok2, " %var% .|::" )) {
Original file line number Diff line number Diff line change @@ -2375,6 +2375,16 @@ class TestStl : public TestFixture {
23752375 " return s.data.c_str();\n "
23762376 " }" );
23772377 ASSERT_EQUALS (" " , errout.str ());
2378+
2379+ check (" struct S {\n " // #7930
2380+ " std::string data;\n "
2381+ " };\n "
2382+ " const char* test() {\n "
2383+ " S s;\n "
2384+ " std::string &ref = s.data;\n "
2385+ " return ref.c_str();\n "
2386+ " }" );
2387+ ASSERT_EQUALS (" [test.cpp:7]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n " , errout.str ());
23782388 }
23792389
23802390 void autoPointer () {
You can’t perform that action at this time.
0 commit comments