CheckNullPointer: Use library instead of hardcoding
This commit is contained in:
parent
87fe5c060e
commit
b0c92c1ac1
|
@ -62,12 +62,6 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const std::vector<const Token *> args = getArguments(&tok);
|
const std::vector<const Token *> args = getArguments(&tok);
|
||||||
const Token* firstParam = args.size() > 0 ? args[0] : nullptr;
|
|
||||||
const Token* secondParam = args.size() > 1 ? args[1] : nullptr;
|
|
||||||
|
|
||||||
// 1st parameter..
|
|
||||||
if (Token::Match(&tok, "snprintf|vsnprintf|fnprintf|vfnprintf") && secondParam && secondParam->str() != "0") // Only if length (second parameter) is not zero
|
|
||||||
var.push_back(firstParam);
|
|
||||||
|
|
||||||
if (library || tok.function() != nullptr) {
|
if (library || tok.function() != nullptr) {
|
||||||
for (int argnr = 1; argnr <= args.size(); ++argnr) {
|
for (int argnr = 1; argnr <= args.size(); ++argnr) {
|
||||||
|
@ -82,30 +76,23 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Token::Match(&tok, "printf|sprintf|snprintf|fprintf|fnprintf|scanf|sscanf|fscanf|wprintf|swprintf|fwprintf|wscanf|swscanf|fwscanf")) {
|
if (library && library->formatstr_function(&tok)) {
|
||||||
std::string formatString;
|
const int formatStringArgNr = library->formatstr_argno(&tok);
|
||||||
int argnr = args.size();
|
if (formatStringArgNr < 0 || formatStringArgNr >= args.size())
|
||||||
const bool scan = Token::Match(&tok, "scanf|sscanf|fscanf|wscanf|swscanf|fwscanf");
|
return;
|
||||||
|
|
||||||
if (Token::Match(&tok, "printf|scanf|wprintf|wscanf ( %str%")) {
|
// 1st parameter..
|
||||||
formatString = firstParam->strValue();
|
if (Token::Match(&tok, "snprintf|vsnprintf|fnprintf|vfnprintf") && args.size() > 1 && !(args[1] && args[1]->hasKnownIntValue() && args[1]->getKnownIntValue() == 0)) // Only if length (second parameter) is not zero
|
||||||
argnr = 1;
|
var.push_back(args[0]);
|
||||||
} else if (Token::Match(&tok, "sprintf|fprintf|sscanf|fscanf|fwprintf|fwscanf|swscanf")) {
|
|
||||||
const Token* formatStringTok = secondParam; // Find second parameter (format string)
|
if (args[formatStringArgNr]->tokType() != Token::eString)
|
||||||
if (formatStringTok && formatStringTok->tokType() == Token::eString) {
|
return;
|
||||||
argnr = 2; // third parameter (first argument of va_args)
|
const std::string &formatString = args[formatStringArgNr]->strValue();
|
||||||
formatString = formatStringTok->strValue();
|
int argnr = formatStringArgNr + 1;
|
||||||
}
|
const bool scan = library->formatstr_scan(&tok);
|
||||||
} else if (Token::Match(&tok, "snprintf|fnprintf|swprintf") && secondParam) {
|
|
||||||
const Token* formatStringTok = args.size() > 2 ? args[2] : nullptr; // third parameter (format string)
|
|
||||||
if (formatStringTok && formatStringTok->tokType() == Token::eString) {
|
|
||||||
argnr = 3; // fourth parameter (first argument of va_args)
|
|
||||||
formatString = formatStringTok->strValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool percent = false;
|
bool percent = false;
|
||||||
for (std::string::iterator i = formatString.begin(); i != formatString.end(); ++i) {
|
for (std::string::const_iterator i = formatString.begin(); i != formatString.end(); ++i) {
|
||||||
if (*i == '%') {
|
if (*i == '%') {
|
||||||
percent = !percent;
|
percent = !percent;
|
||||||
} else if (percent) {
|
} else if (percent) {
|
||||||
|
|
|
@ -40,19 +40,7 @@ private:
|
||||||
Settings settings;
|
Settings settings;
|
||||||
|
|
||||||
void run() OVERRIDE {
|
void run() OVERRIDE {
|
||||||
// Load std.cfg configuration
|
LOAD_LIB_2(settings.library, "std.cfg");
|
||||||
{
|
|
||||||
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
||||||
"<def>\n"
|
|
||||||
" <function name=\"strcpy\">\n"
|
|
||||||
" <arg nr=\"1\"><not-null/></arg>\n"
|
|
||||||
" <arg nr=\"2\"><not-null/></arg>\n"
|
|
||||||
" </function>\n"
|
|
||||||
"</def>";
|
|
||||||
tinyxml2::XMLDocument doc;
|
|
||||||
doc.Parse(xmldata, sizeof(xmldata));
|
|
||||||
settings.library.load(doc);
|
|
||||||
}
|
|
||||||
settings.addEnabled("warning");
|
settings.addEnabled("warning");
|
||||||
|
|
||||||
TEST_CASE(nullpointerAfterLoop);
|
TEST_CASE(nullpointerAfterLoop);
|
||||||
|
|
Loading…
Reference in New Issue