CTU: Refactoring; getErrorPath

This commit is contained in:
Daniel Marjamäki 2018-12-26 15:56:10 +01:00
parent e39be48c92
commit a6e227a73c
4 changed files with 92 additions and 74 deletions

View File

@ -593,10 +593,7 @@ void CheckNullPointer::arithmeticError(const Token *tok, const ValueFlow::Value
std::string CheckNullPointer::MyFileInfo::toString() const
{
std::ostringstream ret;
for (const CTU::FileInfo::UnsafeUsage &u : unsafeUsage)
ret << u.toString();
return ret.str();
return CTU::toString(unsafeUsage);
}
bool CheckNullPointer::isUnsafeFunction(const Scope *scope, int argnr, const Token **tok) const
@ -676,17 +673,12 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo *ctu, const std::
if (!fi)
continue;
for (const CTU::FileInfo::UnsafeUsage &unsafeUsage : fi->unsafeUsage) {
for (const CTU::FileInfo::FunctionCall &functionCall : ctu->functionCalls) {
if (functionCall.valueType != ValueFlow::Value::ValueType::INT)
continue;
if (functionCall.argvalue != 0)
continue;
const std::list<ErrorLogger::ErrorMessage::FileLocation> &locationList =
ctu->getErrorPath(functionCall,
ctu->getErrorPath(CTU::FileInfo::InvalidValueType::null,
unsafeUsage,
nestedCallsMap,
"Dereferencing argument ARG that is null");
"Dereferencing argument ARG that is null",
nullptr);
if (locationList.empty())
continue;
@ -699,7 +691,7 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo *ctu, const std::
errorLogger.reportErr(errmsg);
foundErrors = true;
}
break;
}
}

View File

@ -1281,10 +1281,7 @@ void CheckUninitVar::deadPointerError(const Token *pointer, const Token *alias)
std::string CheckUninitVar::MyFileInfo::toString() const
{
std::ostringstream ret;
for (const CTU::FileInfo::UnsafeUsage &u : unsafeUsage)
ret << u.toString();
return ret.str();
return CTU::toString(unsafeUsage);
}
bool CheckUninitVar::isUnsafeFunction(const Scope *scope, int argnr, const Token **tok) const
@ -1330,7 +1327,6 @@ Check::FileInfo *CheckUninitVar::getFileInfo() const
const Function *const function = scope.function;
// "Unsafe" functions unconditionally reads data before it is written..
CheckNullPointer checkNullPointer(mTokenizer, mSettings, mErrorLogger);
for (int argnr = 0; argnr < function->argCount(); ++argnr) {
const Token *tok;
if (isUnsafeFunction(&scope, argnr, &tok))
@ -1366,28 +1362,27 @@ bool CheckUninitVar::analyseWholeProgram(const CTU::FileInfo *ctu, const std::li
if (!fi)
continue;
for (const CTU::FileInfo::UnsafeUsage &unsafeUsage : fi->unsafeUsage) {
for (const CTU::FileInfo::FunctionCall &functionCall : ctu->functionCalls) {
if (functionCall.valueType != ValueFlow::Value::ValueType::UNINIT)
continue;
const CTU::FileInfo::FunctionCall *functionCall = nullptr;
const std::list<ErrorLogger::ErrorMessage::FileLocation> &locationList =
ctu->getErrorPath(functionCall,
ctu->getErrorPath(CTU::FileInfo::InvalidValueType::uninit,
unsafeUsage,
nestedCallsMap,
"Using argument ARG");
"Using argument ARG",
&functionCall);
if (locationList.empty())
continue;
const ErrorLogger::ErrorMessage errmsg(locationList,
emptyString,
Severity::error,
"Using argument " + unsafeUsage.argumentName + " that points at uninitialized variable " + functionCall.argumentExpression,
"Using argument " + unsafeUsage.argumentName + " that points at uninitialized variable " + functionCall->argumentExpression,
"ctuuninitvar",
CWE908, false);
errorLogger.reportErr(errmsg);
foundErrors = true;
}
break;
}
}
return foundErrors;

View File

@ -82,6 +82,14 @@ std::string CTU::FileInfo::UnsafeUsage::toString() const
return out.str();
}
std::string CTU::toString(const std::list<CTU::FileInfo::UnsafeUsage> &unsafeUsage)
{
std::ostringstream ret;
for (const CTU::FileInfo::UnsafeUsage &u : unsafeUsage)
ret << u.toString();
return ret.str();
}
CTU::FileInfo::NestedCall::NestedCall(const Tokenizer *tokenizer, const Scope *scope, unsigned int argnr_, const Token *tok)
:
id(getFunctionId(tokenizer, scope->function)),
@ -331,15 +339,30 @@ static std::string replacestr(std::string s, const std::string &from, const std:
return s;
}
std::list<ErrorLogger::ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(const CTU::FileInfo::FunctionCall &functionCall,
std::list<ErrorLogger::ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueType invalidValue,
const CTU::FileInfo::UnsafeUsage &unsafeUsage,
const std::map<std::string, std::list<CTU::FileInfo::NestedCall>> &nestedCallsMap,
const char info[]) const
const char info[],
const FunctionCall * * const functionCallPtr) const
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
for (const FunctionCall &functionCall : functionCalls) {
if (invalidValue == CTU::FileInfo::InvalidValueType::null &&
(functionCall.valueType != ValueFlow::Value::ValueType::INT || functionCall.argvalue != 0)) {
continue;
}
if (invalidValue == CTU::FileInfo::InvalidValueType::uninit &&
functionCall.valueType != ValueFlow::Value::ValueType::UNINIT) {
continue;
}
if (!findPath(functionCall, unsafeUsage, nestedCallsMap))
return locationList;
continue;
if (functionCallPtr)
*functionCallPtr = &functionCall;
std::string value1;
if (functionCall.valueType == ValueFlow::Value::ValueType::INT)
@ -362,3 +385,6 @@ std::list<ErrorLogger::ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(c
return locationList;
}
return locationList;
}

View File

@ -91,12 +91,17 @@ namespace CTU {
void loadFromXml(const tinyxml2::XMLElement *xmlElement);
std::map<std::string, std::list<NestedCall>> getNestedCallsMap() const;
std::list<ErrorLogger::ErrorMessage::FileLocation> getErrorPath(const FunctionCall &functionCall,
enum InvalidValueType { null, uninit };
std::list<ErrorLogger::ErrorMessage::FileLocation> getErrorPath(InvalidValueType invalidValue,
const UnsafeUsage &unsafeUsage,
const std::map<std::string, std::list<NestedCall>> &nestedCallsMap,
const char info[]) const;
const char info[],
const FunctionCall * * const functionCallPtr) const;
};
std::string toString(const std::list<FileInfo::UnsafeUsage> &unsafeUsage);
std::string getFunctionId(const Tokenizer *tokenizer, const Function *function);
/** @brief Parse current TU and extract file info */