Fixed false positive memoryLeak #6013

This commit is contained in:
PKEuS 2014-09-30 13:02:02 +02:00
parent 936043d47d
commit ccb5384645
2 changed files with 19 additions and 0 deletions

View File

@ -402,6 +402,9 @@ void CheckLeakAutoVar::functionCall(const Token *tok, VarInfo *varInfo, const in
std::map<unsigned int, std::string> &possibleUsage = varInfo->possibleUsage;
for (const Token *arg = tok->tokAt(2); arg; arg = arg->nextArgument()) {
if (arg->str() == "new")
arg = arg->next();
if ((Token::Match(arg, "%var% [-,)]") && arg->varId() > 0) ||
(Token::Match(arg, "& %var%") && arg->next()->varId() > 0)) {

View File

@ -108,6 +108,8 @@ private:
TEST_CASE(configuration4);
TEST_CASE(ptrptr);
TEST_CASE(nestedAllocation);
}
void check(const char code[]) {
@ -696,6 +698,20 @@ private:
"}");
ASSERT_EQUALS("[test.c:3]: (error) Memory leak: p\n", errout.str());
}
void nestedAllocation() {
check("void QueueDSMCCPacket(unsigned char *data, int length) {\n"
" unsigned char *dataCopy = malloc(length * sizeof(unsigned char));\n"
" m_dsmccQueue.enqueue(new DSMCCPacket(dataCopy));\n"
"}");
ASSERT_EQUALS("[test.c:4]: (information) --check-library: Function DSMCCPacket() should have <use>/<ignore> configuration\n", errout.str());
check("void QueueDSMCCPacket(unsigned char *data, int length) {\n"
" unsigned char *dataCopy = malloc(length * sizeof(unsigned char));\n"
" m_dsmccQueue.enqueue(new DSMCCPacket(somethingunrelated));\n"
"}");
ASSERT_EQUALS("[test.c:4]: (error) Memory leak: dataCopy\n", errout.str());
}
};
REGISTER_TEST(TestLeakAutoVar)