Fixed #4390 (False alarm 'Object pointed by an auto_ptr is destroyed using operator delete. You should not use auto_ptr for pointers obtained with operator new[].')

This commit is contained in:
Daniel Marjamäki 2013-05-01 11:11:57 +02:00
parent b2798e929d
commit 287782a679
2 changed files with 29 additions and 23 deletions

View File

@ -1308,34 +1308,30 @@ void CheckStl::checkAutoPointer()
(Token::simpleMatch(tok->tokAt(-3), "< std :: auto_ptr") && Token::Match(tok->tokAt(-4), STL_CONTAINER_LIST))) { (Token::simpleMatch(tok->tokAt(-3), "< std :: auto_ptr") && Token::Match(tok->tokAt(-4), STL_CONTAINER_LIST))) {
autoPointerContainerError(tok); autoPointerContainerError(tok);
} else { } else {
const Token *tok2 = tok->tokAt(2); const Token *tok2 = tok->linkAt(1);
while (tok2) {
if (Token::Match(tok2, "> %var%")) { if (Token::Match(tok2, "> %var%")) {
const Token *tok3 = tok2->tokAt(2); const Token *tok3 = tok2->tokAt(2);
if (Token::Match(tok3, "( new %type%") && hasArrayEndParen(tok3)) { if (Token::Match(tok3, "( new %type%") && hasArrayEndParen(tok3)) {
autoPointerArrayError(tok2->next());
}
while (tok3 && tok3->str() != ";") {
tok3 = tok3->next();
}
if (tok3) {
tok3 = tok3->tokAt(-2);
if (Token::simpleMatch(tok3->previous(), "[ ] )")) {
autoPointerArrayError(tok2->next()); autoPointerArrayError(tok2->next());
break; } else if (tok3->varId()) {
} const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid% = new %type%", tok3->varId());
while (tok3 && tok3->str() != ";") { if (decltok && hasArrayEnd(decltok)) {
tok3 = tok3->next();
}
if (tok3) {
tok3 = tok3->tokAt(-2);
if (Token::simpleMatch(tok3->previous(), "[ ] )")) {
autoPointerArrayError(tok2->next()); autoPointerArrayError(tok2->next());
} else if (tok3->varId()) {
const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid% = new %type%", tok3->varId());
if (decltok && hasArrayEnd(decltok)) {
autoPointerArrayError(tok2->next());
}
} }
if (tok2->next()->varId()) { }
autoPtrVarId.insert(tok2->next()->varId()); if (tok2->next()->varId()) {
} autoPtrVarId.insert(tok2->next()->varId());
break;
} }
} }
tok2 = tok2->next();
} }
} }
} else { } else {

View File

@ -2145,6 +2145,16 @@ private:
// ticket #2967 (segmentation fault) // ticket #2967 (segmentation fault)
check("auto_ptr<x>\n"); check("auto_ptr<x>\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// ticket #4390
check("auto_ptr<ConnectionStringReadStorage> CreateRegistryStringStorage() {\n"
" return auto_ptr<ConnectionStringReadStorage>(new RegistryConnectionStringStorage());\n"
"}\n"
"\n"
"void LookupWindowsUserAccountName() {\n"
" auto_ptr_array<char> domainName(new char[42]);\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void uselessCalls() { void uselessCalls() {