Exception Safety: detect unsafe multiple new in a execution path (#831)
This commit is contained in:
parent
797e9aeaf5
commit
616a760b6c
|
@ -166,4 +166,31 @@ void CheckExceptionSafety::unsafeNew()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allocating multiple local variables..
|
||||||
|
std::set<unsigned int> localVars;
|
||||||
|
unsigned int countNew = 0;
|
||||||
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
||||||
|
{
|
||||||
|
if (tok->str() == "{" || tok->str() == "}")
|
||||||
|
{
|
||||||
|
localVars.clear();
|
||||||
|
countNew = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Token::Match(tok, "[;{}] %type% * %var% ;"))
|
||||||
|
{
|
||||||
|
tok = tok->tokAt(3);
|
||||||
|
if (tok->varId())
|
||||||
|
localVars.insert(tok->varId());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Token::Match(tok, "; %var% = new"))
|
||||||
|
{
|
||||||
|
if (tok->next()->varId() && localVars.find(tok->next()->varId()) != localVars.end())
|
||||||
|
++countNew;
|
||||||
|
if (countNew >= 2)
|
||||||
|
unsafeNewError(tok->next());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,7 @@ public:
|
||||||
{
|
{
|
||||||
CheckExceptionSafety checkExceptionSafety(tokenizer, settings, errorLogger);
|
CheckExceptionSafety checkExceptionSafety(tokenizer, settings, errorLogger);
|
||||||
checkExceptionSafety.destructors();
|
checkExceptionSafety.destructors();
|
||||||
|
checkExceptionSafety.unsafeNew();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -83,6 +83,13 @@ private:
|
||||||
" b = new B;\n"
|
" b = new B;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (style) Exception safety: unsafe use of 'new'\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (style) Exception safety: unsafe use of 'new'\n", errout.str());
|
||||||
|
|
||||||
|
check("void a()\n"
|
||||||
|
"{\n"
|
||||||
|
" A *a1 = new A;\n"
|
||||||
|
" A *a2 = new A;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:4]: (style) Exception safety: unsafe use of 'new'\n", errout.str());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue