Remove duplicate error reporting function

This commit is contained in:
Dmitry-Me 2017-11-14 21:46:17 +03:00
parent 7f48283b98
commit 4fc0a7998e
2 changed files with 15 additions and 29 deletions

View File

@ -982,50 +982,50 @@ void CheckIO::checkFormatString(const Token * const tok,
case 'o': case 'o':
specifier += *i; specifier += *i;
if (argInfo.typeToken->tokType() == Token::eString) if (argInfo.typeToken->tokType() == Token::eString)
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
else if (argInfo.isArrayOrPointer() && !argInfo.element) { else if (argInfo.isArrayOrPointer() && !argInfo.element) {
// use %p on pointers and arrays // use %p on pointers and arrays
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
} else if (argInfo.isKnownType()) { } else if (argInfo.isKnownType()) {
if (!Token::Match(argInfo.typeToken, "bool|short|long|int|char|wchar_t")) { if (!Token::Match(argInfo.typeToken, "bool|short|long|int|char|wchar_t")) {
if (!(!argInfo.isArrayOrPointer() && argInfo.element)) if (!(!argInfo.isArrayOrPointer() && argInfo.element))
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
} else { } else {
switch (specifier[0]) { switch (specifier[0]) {
case 'h': case 'h':
if (specifier[1] == 'h') { if (specifier[1] == 'h') {
if (argInfo.typeToken->str() != "char") if (argInfo.typeToken->str() != "char")
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
} else if (argInfo.typeToken->str() != "short") } else if (argInfo.typeToken->str() != "short")
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
break; break;
case 'l': case 'l':
if (specifier[1] == 'l') { if (specifier[1] == 'l') {
if (argInfo.typeToken->str() != "long" || !argInfo.typeToken->isLong()) if (argInfo.typeToken->str() != "long" || !argInfo.typeToken->isLong())
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
} else if (argInfo.typeToken->str() != "long" || argInfo.typeToken->isLong()) } else if (argInfo.typeToken->str() != "long" || argInfo.typeToken->isLong())
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
break; break;
case 'j': case 'j':
if (!(argInfo.typeToken->originalName() == "intmax_t" || if (!(argInfo.typeToken->originalName() == "intmax_t" ||
argInfo.typeToken->originalName() == "uintmax_t")) argInfo.typeToken->originalName() == "uintmax_t"))
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
break; break;
case 'z': case 'z':
if (!typesMatch(argInfo.typeToken->originalName(), "size_t")) if (!typesMatch(argInfo.typeToken->originalName(), "size_t"))
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
break; break;
case 't': case 't':
if (!typesMatch(argInfo.typeToken->originalName(), "ptrdiff_t")) if (!typesMatch(argInfo.typeToken->originalName(), "ptrdiff_t"))
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
break; break;
case 'I': case 'I':
if (specifier.find("I64") != std::string::npos) { if (specifier.find("I64") != std::string::npos) {
if (argInfo.typeToken->str() != "long" || !argInfo.typeToken->isLong()) if (argInfo.typeToken->str() != "long" || !argInfo.typeToken->isLong())
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
} else if (specifier.find("I32") != std::string::npos) { } else if (specifier.find("I32") != std::string::npos) {
if (argInfo.typeToken->str() != "int" || argInfo.typeToken->isLong()) if (argInfo.typeToken->str() != "int" || argInfo.typeToken->isLong())
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
} else if (!(typesMatch(argInfo.typeToken->originalName(), "size_t") || } else if (!(typesMatch(argInfo.typeToken->originalName(), "size_t") ||
typesMatch(argInfo.typeToken->originalName(), "ptrdiff_t") || typesMatch(argInfo.typeToken->originalName(), "ptrdiff_t") ||
argInfo.typeToken->originalName() == "WPARAM" || argInfo.typeToken->originalName() == "WPARAM" ||
@ -1033,11 +1033,11 @@ void CheckIO::checkFormatString(const Token * const tok,
argInfo.typeToken->originalName() == "LONG_PTR" || argInfo.typeToken->originalName() == "LONG_PTR" ||
argInfo.typeToken->originalName() == "LPARAM" || argInfo.typeToken->originalName() == "LPARAM" ||
argInfo.typeToken->originalName() == "LRESULT")) argInfo.typeToken->originalName() == "LRESULT"))
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
break; break;
default: default:
if (!Token::Match(argInfo.typeToken, "bool|char|short|wchar_t|int")) if (!Token::Match(argInfo.typeToken, "bool|char|short|wchar_t|int"))
invalidPrintfArgTypeError_int(tok, numFormat, specifier, &argInfo); invalidPrintfArgTypeError_uint(tok, numFormat, specifier, &argInfo);
break; break;
} }
} }
@ -1870,19 +1870,7 @@ static void printfFormatType(std::ostream& os, const std::string& specifier, boo
} }
os << "\'"; os << "\'";
} }
void CheckIO::invalidPrintfArgTypeError_int(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo)
{
const Severity::SeverityType severity = getSeverity(argInfo);
if (!_settings->isEnabled(severity))
return;
std::ostringstream errmsg;
errmsg << "%" << specifier << " in format string (no. " << numFormat << ") requires ";
printfFormatType(errmsg, specifier, true);
errmsg << " but the argument type is ";
argumentType(errmsg, argInfo);
errmsg << ".";
reportError(tok, severity, "invalidPrintfArgType_int", errmsg.str(), CWE686, false);
}
void CheckIO::invalidPrintfArgTypeError_uint(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo) void CheckIO::invalidPrintfArgTypeError_uint(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo)
{ {
const Severity::SeverityType severity = getSeverity(argInfo); const Severity::SeverityType severity = getSeverity(argInfo);

View File

@ -131,7 +131,6 @@ private:
void invalidPrintfArgTypeError_s(const Token* tok, unsigned int numFormat, const ArgumentInfo* argInfo); void invalidPrintfArgTypeError_s(const Token* tok, unsigned int numFormat, const ArgumentInfo* argInfo);
void invalidPrintfArgTypeError_n(const Token* tok, unsigned int numFormat, const ArgumentInfo* argInfo); void invalidPrintfArgTypeError_n(const Token* tok, unsigned int numFormat, const ArgumentInfo* argInfo);
void invalidPrintfArgTypeError_p(const Token* tok, unsigned int numFormat, const ArgumentInfo* argInfo); void invalidPrintfArgTypeError_p(const Token* tok, unsigned int numFormat, const ArgumentInfo* argInfo);
void invalidPrintfArgTypeError_int(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
void invalidPrintfArgTypeError_uint(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo); void invalidPrintfArgTypeError_uint(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
void invalidPrintfArgTypeError_sint(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo); void invalidPrintfArgTypeError_sint(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
void invalidPrintfArgTypeError_float(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo); void invalidPrintfArgTypeError_float(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
@ -158,7 +157,6 @@ private:
c.invalidPrintfArgTypeError_s(nullptr, 1, nullptr); c.invalidPrintfArgTypeError_s(nullptr, 1, nullptr);
c.invalidPrintfArgTypeError_n(nullptr, 1, nullptr); c.invalidPrintfArgTypeError_n(nullptr, 1, nullptr);
c.invalidPrintfArgTypeError_p(nullptr, 1, nullptr); c.invalidPrintfArgTypeError_p(nullptr, 1, nullptr);
c.invalidPrintfArgTypeError_int(nullptr, 1, "X", nullptr);
c.invalidPrintfArgTypeError_uint(nullptr, 1, "u", nullptr); c.invalidPrintfArgTypeError_uint(nullptr, 1, "u", nullptr);
c.invalidPrintfArgTypeError_sint(nullptr, 1, "i", nullptr); c.invalidPrintfArgTypeError_sint(nullptr, 1, "i", nullptr);
c.invalidPrintfArgTypeError_float(nullptr, 1, "f", nullptr); c.invalidPrintfArgTypeError_float(nullptr, 1, "f", nullptr);