Refactorings, and try to ensure that analyzer info is stored/loaded properly

This commit is contained in:
Daniel Marjamäki 2018-01-15 19:12:31 +01:00
parent f2b2e87fb6
commit c331a10fa7
1 changed files with 24 additions and 19 deletions

View File

@ -53,6 +53,8 @@ static const struct CWE CWE676(676U);
static const struct CWE CWE908(908U); static const struct CWE CWE908(908U);
static const struct CWE CWE825(825U); static const struct CWE CWE825(825U);
static const std::string UNSAFE_FUNCTION_ARG("unsafeFunctionArg");
static const std::string UNINITIALIZED_FUNCTION_ARG("uninitializedFunctionArg");
void CheckUninitVar::check() void CheckUninitVar::check()
{ {
@ -1283,26 +1285,25 @@ void CheckUninitVar::deadPointerError(const Token *pointer, const Token *alias)
"Dead pointer usage. Pointer '" + strpointer + "' is dead if it has been assigned '" + stralias + "' at line " + MathLib::toString(alias ? alias->linenr() : 0U) + ".", CWE825, false); "Dead pointer usage. Pointer '" + strpointer + "' is dead if it has been assigned '" + stralias + "' at line " + MathLib::toString(alias ? alias->linenr() : 0U) + ".", CWE825, false);
} }
static void writeFunctionArgXml(const CheckUninitVar::MyFileInfo::FunctionArg &fa, const std::string &elementName, std::ostream &out)
{
out << " <" << elementName
<< " functionName=\"" << fa.functionName << '\"'
<< " argnr=\"" << fa.argnr << '\"'
<< " variableName=\"" << fa.variableName << "\""
<< " fileName=\"" << fa.location.fileName << '\"'
<< " linenr=\"" << fa.location.linenr << '\"'
<< "/>\n";
}
std::string CheckUninitVar::MyFileInfo::toString() const std::string CheckUninitVar::MyFileInfo::toString() const
{ {
std::ostringstream ret; std::ostringstream ret;
for (std::list<CheckUninitVar::MyFileInfo::FunctionArg>::const_iterator it = unsafeFunctionArgs.begin(); it != unsafeFunctionArgs.end(); ++it) { for (std::list<CheckUninitVar::MyFileInfo::FunctionArg>::const_iterator it = unsafeFunctionArgs.begin(); it != unsafeFunctionArgs.end(); ++it) {
ret << " <unsafefunctionarg" writeFunctionArgXml(*it, UNSAFE_FUNCTION_ARG, ret);
<< " functionName=\"" << it->functionName << '\"'
<< " argnr=\"" << it->argnr << '\"'
<< " variableName=\"" << it->variableName << "\""
<< " fileName=\"" << it->location.fileName << '\"'
<< " linenr=\"" << it->location.linenr << '\"'
<< "/>\n";
} }
for (std::list<CheckUninitVar::MyFileInfo::FunctionArg>::const_iterator it = uninitializedFunctionArgs.begin(); it != uninitializedFunctionArgs.end(); ++it) { for (std::list<CheckUninitVar::MyFileInfo::FunctionArg>::const_iterator it = uninitializedFunctionArgs.begin(); it != uninitializedFunctionArgs.end(); ++it) {
ret << " <uninitializedFunctionArgs" writeFunctionArgXml(*it, UNINITIALIZED_FUNCTION_ARG, ret);
<< " functionName=\"" << it->functionName << '\"'
<< " argnr=\"" << it->argnr << '\"'
<< " variableName=\"" << it->variableName << "\""
<< " fileName=\"" << it->location.fileName << '\"'
<< " linenr=\"" << it->location.linenr << '\"'
<< "/>\n";
} }
return ret.str(); return ret.str();
} }
@ -1367,9 +1368,9 @@ Check::FileInfo *CheckUninitVar::getFileInfo(const Tokenizer *tokenizer, const S
Check::FileInfo * CheckUninitVar::loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const Check::FileInfo * CheckUninitVar::loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const
{ {
MyFileInfo *fileInfo = new MyFileInfo; MyFileInfo *fileInfo = nullptr;
for (const tinyxml2::XMLElement *e = xmlElement->FirstChildElement(); e; e = e->NextSiblingElement()) { for (const tinyxml2::XMLElement *e = xmlElement->FirstChildElement(); e; e = e->NextSiblingElement()) {
if (std::strcmp(e->Name(),"unsafefunction")!=0 && std::strcmp(e->Name(),"uninitializedFunctionArgs")!=0) if (e->Name() != UNSAFE_FUNCTION_ARG && e->Name() != UNINITIALIZED_FUNCTION_ARG)
continue; continue;
const char *functionName = e->Attribute("functionName"); const char *functionName = e->Attribute("functionName");
if (!functionName) if (!functionName)
@ -1380,17 +1381,21 @@ Check::FileInfo * CheckUninitVar::loadFileInfoFromXml(const tinyxml2::XMLElement
const char *fileName = e->Attribute("fileName"); const char *fileName = e->Attribute("fileName");
if (!fileName) if (!fileName)
continue; continue;
const char *linenr = e->Attribute("argnr"); const char *linenr = e->Attribute("linenr");
if (!linenr || !MathLib::isInt(linenr)) if (!linenr || !MathLib::isInt(linenr))
continue; continue;
const char *variableName = e->Attribute("variableName"); const char *variableName = e->Attribute("variableName");
if (!variableName) if (!variableName)
continue; continue;
const MyFileInfo::FunctionArg fa(functionName, MathLib::toLongNumber(argnr), fileName, MathLib::toLongNumber(linenr), variableName); const MyFileInfo::FunctionArg fa(functionName, MathLib::toLongNumber(argnr), fileName, MathLib::toLongNumber(linenr), variableName);
if (std::strcmp(e->Name(), "unsafefunction") == 0) if (!fileInfo)
fileInfo = new MyFileInfo;
if (e->Name() == UNSAFE_FUNCTION_ARG)
fileInfo->unsafeFunctionArgs.push_back(fa); fileInfo->unsafeFunctionArgs.push_back(fa);
else else if (e->Name() == UNINITIALIZED_FUNCTION_ARG)
fileInfo->uninitializedFunctionArgs.push_back(fa); fileInfo->uninitializedFunctionArgs.push_back(fa);
else
throw InternalError(nullptr, "Wrong analyze info");
} }
return fileInfo; return fileInfo;
} }