CTU: Refactoring; getErrorPath
This commit is contained in:
parent
e39be48c92
commit
a6e227a73c
|
@ -593,10 +593,7 @@ void CheckNullPointer::arithmeticError(const Token *tok, const ValueFlow::Value
|
||||||
|
|
||||||
std::string CheckNullPointer::MyFileInfo::toString() const
|
std::string CheckNullPointer::MyFileInfo::toString() const
|
||||||
{
|
{
|
||||||
std::ostringstream ret;
|
return CTU::toString(unsafeUsage);
|
||||||
for (const CTU::FileInfo::UnsafeUsage &u : unsafeUsage)
|
|
||||||
ret << u.toString();
|
|
||||||
return ret.str();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CheckNullPointer::isUnsafeFunction(const Scope *scope, int argnr, const Token **tok) const
|
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)
|
if (!fi)
|
||||||
continue;
|
continue;
|
||||||
for (const CTU::FileInfo::UnsafeUsage &unsafeUsage : fi->unsafeUsage) {
|
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 =
|
const std::list<ErrorLogger::ErrorMessage::FileLocation> &locationList =
|
||||||
ctu->getErrorPath(functionCall,
|
ctu->getErrorPath(CTU::FileInfo::InvalidValueType::null,
|
||||||
unsafeUsage,
|
unsafeUsage,
|
||||||
nestedCallsMap,
|
nestedCallsMap,
|
||||||
"Dereferencing argument ARG that is null");
|
"Dereferencing argument ARG that is null",
|
||||||
|
nullptr);
|
||||||
if (locationList.empty())
|
if (locationList.empty())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -699,7 +691,7 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo *ctu, const std::
|
||||||
errorLogger.reportErr(errmsg);
|
errorLogger.reportErr(errmsg);
|
||||||
|
|
||||||
foundErrors = true;
|
foundErrors = true;
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1281,10 +1281,7 @@ void CheckUninitVar::deadPointerError(const Token *pointer, const Token *alias)
|
||||||
|
|
||||||
std::string CheckUninitVar::MyFileInfo::toString() const
|
std::string CheckUninitVar::MyFileInfo::toString() const
|
||||||
{
|
{
|
||||||
std::ostringstream ret;
|
return CTU::toString(unsafeUsage);
|
||||||
for (const CTU::FileInfo::UnsafeUsage &u : unsafeUsage)
|
|
||||||
ret << u.toString();
|
|
||||||
return ret.str();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CheckUninitVar::isUnsafeFunction(const Scope *scope, int argnr, const Token **tok) const
|
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;
|
const Function *const function = scope.function;
|
||||||
|
|
||||||
// "Unsafe" functions unconditionally reads data before it is written..
|
// "Unsafe" functions unconditionally reads data before it is written..
|
||||||
CheckNullPointer checkNullPointer(mTokenizer, mSettings, mErrorLogger);
|
|
||||||
for (int argnr = 0; argnr < function->argCount(); ++argnr) {
|
for (int argnr = 0; argnr < function->argCount(); ++argnr) {
|
||||||
const Token *tok;
|
const Token *tok;
|
||||||
if (isUnsafeFunction(&scope, argnr, &tok))
|
if (isUnsafeFunction(&scope, argnr, &tok))
|
||||||
|
@ -1366,28 +1362,27 @@ bool CheckUninitVar::analyseWholeProgram(const CTU::FileInfo *ctu, const std::li
|
||||||
if (!fi)
|
if (!fi)
|
||||||
continue;
|
continue;
|
||||||
for (const CTU::FileInfo::UnsafeUsage &unsafeUsage : fi->unsafeUsage) {
|
for (const CTU::FileInfo::UnsafeUsage &unsafeUsage : fi->unsafeUsage) {
|
||||||
for (const CTU::FileInfo::FunctionCall &functionCall : ctu->functionCalls) {
|
const CTU::FileInfo::FunctionCall *functionCall = nullptr;
|
||||||
if (functionCall.valueType != ValueFlow::Value::ValueType::UNINIT)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
const std::list<ErrorLogger::ErrorMessage::FileLocation> &locationList =
|
const std::list<ErrorLogger::ErrorMessage::FileLocation> &locationList =
|
||||||
ctu->getErrorPath(functionCall,
|
ctu->getErrorPath(CTU::FileInfo::InvalidValueType::uninit,
|
||||||
unsafeUsage,
|
unsafeUsage,
|
||||||
nestedCallsMap,
|
nestedCallsMap,
|
||||||
"Using argument ARG");
|
"Using argument ARG",
|
||||||
|
&functionCall);
|
||||||
if (locationList.empty())
|
if (locationList.empty())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const ErrorLogger::ErrorMessage errmsg(locationList,
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
||||||
emptyString,
|
emptyString,
|
||||||
Severity::error,
|
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",
|
"ctuuninitvar",
|
||||||
CWE908, false);
|
CWE908, false);
|
||||||
errorLogger.reportErr(errmsg);
|
errorLogger.reportErr(errmsg);
|
||||||
|
|
||||||
foundErrors = true;
|
foundErrors = true;
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return foundErrors;
|
return foundErrors;
|
||||||
|
|
32
lib/ctu.cpp
32
lib/ctu.cpp
|
@ -82,6 +82,14 @@ std::string CTU::FileInfo::UnsafeUsage::toString() const
|
||||||
return out.str();
|
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)
|
CTU::FileInfo::NestedCall::NestedCall(const Tokenizer *tokenizer, const Scope *scope, unsigned int argnr_, const Token *tok)
|
||||||
:
|
:
|
||||||
id(getFunctionId(tokenizer, scope->function)),
|
id(getFunctionId(tokenizer, scope->function)),
|
||||||
|
@ -331,15 +339,30 @@ static std::string replacestr(std::string s, const std::string &from, const std:
|
||||||
return s;
|
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 CTU::FileInfo::UnsafeUsage &unsafeUsage,
|
||||||
const std::map<std::string, std::list<CTU::FileInfo::NestedCall>> &nestedCallsMap,
|
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;
|
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))
|
if (!findPath(functionCall, unsafeUsage, nestedCallsMap))
|
||||||
return locationList;
|
continue;
|
||||||
|
|
||||||
|
if (functionCallPtr)
|
||||||
|
*functionCallPtr = &functionCall;
|
||||||
|
|
||||||
std::string value1;
|
std::string value1;
|
||||||
if (functionCall.valueType == ValueFlow::Value::ValueType::INT)
|
if (functionCall.valueType == ValueFlow::Value::ValueType::INT)
|
||||||
|
@ -362,3 +385,6 @@ std::list<ErrorLogger::ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(c
|
||||||
|
|
||||||
return locationList;
|
return locationList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return locationList;
|
||||||
|
}
|
||||||
|
|
|
@ -91,12 +91,17 @@ namespace CTU {
|
||||||
void loadFromXml(const tinyxml2::XMLElement *xmlElement);
|
void loadFromXml(const tinyxml2::XMLElement *xmlElement);
|
||||||
std::map<std::string, std::list<NestedCall>> getNestedCallsMap() const;
|
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 UnsafeUsage &unsafeUsage,
|
||||||
const std::map<std::string, std::list<NestedCall>> &nestedCallsMap,
|
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);
|
std::string getFunctionId(const Tokenizer *tokenizer, const Function *function);
|
||||||
|
|
||||||
/** @brief Parse current TU and extract file info */
|
/** @brief Parse current TU and extract file info */
|
||||||
|
|
Loading…
Reference in New Issue