Made invalidPointerCast message inconclusive for casting from floating point type* to char* (Fixed #3639)

This commit is contained in:
PKEuS 2012-03-25 12:55:39 +02:00
parent 2e2fd77b83
commit 989ac449e4
3 changed files with 24 additions and 10 deletions

View File

@ -333,7 +333,7 @@ static std::string analyzeType(const Token* tok)
} }
if (tok->str() == "float") if (tok->str() == "float")
return "float"; return "float";
if (Token::Match(tok, "unsigned| int|long|short|char|size_t")) if (Token::Match(tok, "int|long|short|char|size_t"))
return "integer"; return "integer";
return ""; return "";
} }
@ -398,16 +398,19 @@ void CheckOther::invalidPointerCast()
std::string fromType = analyzeType(fromTok); std::string fromType = analyzeType(fromTok);
std::string toType = analyzeType(toTok); std::string toType = analyzeType(toTok);
if (fromType != toType && !fromType.empty() && !toType.empty() && (toType != "integer" || _settings->isEnabled("portability"))) if (fromType != toType && !fromType.empty() && !toType.empty() && (toType != "integer" || _settings->isEnabled("portability")) && (toTok->str() != "char" || _settings->inconclusive))
invalidPointerCastError(tok, fromType, toType); invalidPointerCastError(tok, fromType, toType, toTok->str() == "char");
} }
} }
void CheckOther::invalidPointerCastError(const Token* tok, const std::string& from, const std::string& to) void CheckOther::invalidPointerCastError(const Token* tok, const std::string& from, const std::string& to, bool inconclusive)
{ {
if (to == "integer") // If we cast something to int*, this can be useful to play with its binary data representation if (to == "integer") { // If we cast something to int*, this can be useful to play with its binary data representation
if (!inconclusive)
reportError(tok, Severity::portability, "invalidPointerCast", "Casting from " + from + "* to integer* is not portable due to different binary data representations on different platforms"); reportError(tok, Severity::portability, "invalidPointerCast", "Casting from " + from + "* to integer* is not portable due to different binary data representations on different platforms");
else else
reportInconclusiveError(tok, Severity::portability, "invalidPointerCast", "Casting from " + from + "* to char* might be not portable due to different binary data representations on different platforms");
} else
reportError(tok, Severity::warning, "invalidPointerCast", "Casting between " + from + "* and " + to + "* which have an incompatible binary data representation"); reportError(tok, Severity::warning, "invalidPointerCast", "Casting between " + from + "* and " + to + "* which have an incompatible binary data representation");
} }

View File

@ -268,7 +268,7 @@ private:
void invalidPrintfArgTypeError_int(const Token* tok, unsigned int numFormat, char c); void invalidPrintfArgTypeError_int(const Token* tok, unsigned int numFormat, char c);
void invalidPrintfArgTypeError_float(const Token* tok, unsigned int numFormat, char c); void invalidPrintfArgTypeError_float(const Token* tok, unsigned int numFormat, char c);
void cstyleCastError(const Token *tok); void cstyleCastError(const Token *tok);
void invalidPointerCastError(const Token* tok, const std::string& from, const std::string& to); void invalidPointerCastError(const Token* tok, const std::string& from, const std::string& to, bool inconclusive);
void dangerousUsageStrtolError(const Token *tok); void dangerousUsageStrtolError(const Token *tok);
void sprintfOverlappingDataError(const Token *tok, const std::string &varname); void sprintfOverlappingDataError(const Token *tok, const std::string &varname);
void udivError(const Token *tok, bool inconclusive); void udivError(const Token *tok, bool inconclusive);
@ -331,7 +331,7 @@ private:
c.sizeofForNumericParameterError(0); c.sizeofForNumericParameterError(0);
c.coutCerrMisusageError(0, "cout"); c.coutCerrMisusageError(0, "cout");
c.doubleFreeError(0, "varname"); c.doubleFreeError(0, "varname");
c.invalidPointerCastError(0, "float", "double"); c.invalidPointerCastError(0, "float", "double", false);
// style/warning // style/warning
c.cstyleCastError(0); c.cstyleCastError(0);

View File

@ -783,7 +783,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void checkInvalidPointerCast(const char code[], bool portability = false) { void checkInvalidPointerCast(const char code[], bool portability = false, bool inconclusive = false) {
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
@ -791,6 +791,7 @@ private:
settings.addEnabled("style"); settings.addEnabled("style");
if (portability) if (portability)
settings.addEnabled("portability"); settings.addEnabled("portability");
settings.inconclusive = inconclusive;
// Tokenize.. // Tokenize..
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);
@ -849,6 +850,16 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Casting between double* and float* which have an incompatible binary data representation\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (warning) Casting between double* and float* which have an incompatible binary data representation\n", errout.str());
checkInvalidPointerCast("void test(float* data) {\n" // #3639
" f.write((char*)data,sizeof(float));\n"
"}", true, false);
ASSERT_EQUALS("", errout.str());
checkInvalidPointerCast("void test(float* data) {\n"
" f.write((char*)data,sizeof(float));\n"
"}", true, true);
ASSERT_EQUALS("[test.cpp:2]: (portability) Casting from float* to char* might be not portable due to different binary data representations on different platforms\n", errout.str());
checkInvalidPointerCast("long long* test(float* f) {\n" checkInvalidPointerCast("long long* test(float* f) {\n"
" return (long long*)f;\n" " return (long long*)f;\n"