Fix nullptr deref (#4262)

* Fix some FNs related to c_str()

* Format, fix FP

* Fix nullptr deref

* Fix merge
This commit is contained in:
chrchr-github 2022-07-11 22:58:08 +02:00 committed by GitHub
parent e8b0f12367
commit f9337b725b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View File

@ -1954,7 +1954,7 @@ void CheckStl::string_c_str()
if (var->isPointer())
string_c_strError(tok);
} else if (printPerformance && Token::Match(tok->tokAt(2), "%var% . c_str|data ( ) ;")) {
if (tok->variable()->isStlStringType() && tok->tokAt(2)->variable()->isStlStringType())
if (tok->variable() && tok->variable()->isStlStringType() && tok->tokAt(2)->variable() && tok->tokAt(2)->variable()->isStlStringType())
string_c_strAssignment(tok);
}
} else if (printPerformance && tok->function() && Token::Match(tok, "%name% ( !!)") && tok->str() != scope.className) {
@ -1989,11 +1989,12 @@ void CheckStl::string_c_str()
}
}
} else if (printPerformance && Token::Match(tok, "%var% (|{ %var% . c_str|data ( )") && tok->variable()->isStlStringType() && tok->tokAt(2)->variable()->isStlStringType()) {
} else if (printPerformance && Token::Match(tok, "%var% (|{ %var% . c_str|data ( )") &&
tok->variable() && tok->variable()->isStlStringType() && tok->tokAt(2)->variable() && tok->tokAt(2)->variable()->isStlStringType()) {
string_c_strConstructor(tok);
} else if (printPerformance && tok->next() && tok->next()->variable() && tok->next()->variable()->isStlStringType() && tok->valueType() && tok->valueType()->type == ValueType::CONTAINER &&
((Token::Match(tok->previous(), "%var% + %var% . c_str|data ( )") && tok->previous()->variable()->isStlStringType()) ||
(Token::Match(tok->tokAt(-5), "%var% . c_str|data ( ) + %var%") && tok->tokAt(-5)->variable()->isStlStringType()))) {
((Token::Match(tok->previous(), "%var% + %var% . c_str|data ( )") && tok->previous()->variable() && tok->previous()->variable()->isStlStringType()) ||
(Token::Match(tok->tokAt(-5), "%var% . c_str|data ( ) + %var%") && tok->tokAt(-5)->variable() && tok->tokAt(-5)->variable()->isStlStringType()))) {
string_c_strConcat(tok);
}

View File

@ -4043,6 +4043,13 @@ private:
" return a.c_str() + b;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (performance) Concatenating the result of c_str() and a std::string is slow and redundant.\n", errout.str());
check("std::vector<double> v;\n" // don't crash
"int i;\n"
"void f() {\n"
" const double* const QM_R__ buf(v.data() + i);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void uselessCalls() {