Skip to content

Commit 6a082e0

Browse files
committed
tweak
1 parent 23ec0b7 commit 6a082e0

5 files changed

Lines changed: 53 additions & 42 deletions

File tree

lib/checkother.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,15 @@ void CheckOther::cstyleCastError(const Token *tok, bool isPtr)
408408
"which kind of cast is expected.", CWE398, Certainty::normal);
409409
}
410410

411-
void CheckOther::warningDangerousOldStyleTypeCast()
411+
void CheckOther::warningDangerousTypeCast()
412412
{
413413
// Only valid on C++ code
414414
if (!mTokenizer->isCPP())
415415
return;
416416
if (!mSettings->severity.isEnabled(Severity::warning) && !mSettings->isPremiumEnabled("cstyleCast"))
417417
return;
418418

419-
logChecker("CheckOther::warningDangerousOldStyleTypeCast"); // warning,c++
419+
logChecker("CheckOther::warningDangerousTypeCast"); // warning,c++
420420

421421
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
422422
for (const Scope * scope : symbolDatabase->functionScopes) {
@@ -440,7 +440,7 @@ void CheckOther::dangerousTypeCastError(const Token *tok, bool isPtr)
440440
{
441441
const std::string type = isPtr ? "pointer" : "reference";
442442
reportError(tok, Severity::warning, "dangerousTypeCast",
443-
"Potentially dangerous C style type cast of " + type + " to object",
443+
"Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast",
444444
CWE398, Certainty::normal);
445445
}
446446

@@ -460,15 +460,23 @@ void CheckOther::warningIntToPointerCast()
460460
continue;
461461
if (!tok->valueType() || tok->valueType()->pointer == 0)
462462
continue;
463-
if (!MathLib::isIntHex(from->str()) && from->getKnownIntValue() != 0)
464-
intToPointerCastError(tok);
463+
if (!MathLib::isIntHex(from->str()) && from->getKnownIntValue() != 0) {
464+
std::string format;
465+
if (MathLib::isDec(from->str()))
466+
format = "decimal";
467+
else if (MathLib::isOct(from->str()))
468+
format = "octal";
469+
else
470+
continue;
471+
intToPointerCastError(tok, format);
472+
}
465473
}
466474
}
467475

468-
void CheckOther::intToPointerCastError(const Token *tok)
476+
void CheckOther::intToPointerCastError(const Token *tok, const std::string& format)
469477
{
470478
reportError(tok, Severity::portability, "intToPointerCast",
471-
"Casting non-zero integer literal in decimal or octal format to pointer.",
479+
"Casting non-zero " + format + " integer literal to pointer.",
472480
CWE398, Certainty::normal);
473481
}
474482

@@ -4498,6 +4506,7 @@ void CheckOther::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
44984506

44994507
// Checks
45004508
checkOther.warningOldStylePointerCast();
4509+
checkOther.warningDangerousTypeCast();
45014510
checkOther.warningIntToPointerCast();
45024511
checkOther.suspiciousFloatingPointCast();
45034512
checkOther.invalidPointerCast();
@@ -4566,7 +4575,7 @@ void CheckOther::getErrorMessages(ErrorLogger *errorLogger, const Settings *sett
45664575
c.checkCastIntToCharAndBackError(nullptr, "func_name");
45674576
c.cstyleCastError(nullptr);
45684577
c.dangerousTypeCastError(nullptr, true);
4569-
c.intToPointerCastError(nullptr);
4578+
c.intToPointerCastError(nullptr, "decimal");
45704579
c.suspiciousFloatingPointCastError(nullptr);
45714580
c.passedByValueError(nullptr, false);
45724581
c.constVariableError(nullptr, nullptr);

lib/checkother.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class CPPCHECKLIB CheckOther : public Check {
8181
void warningOldStylePointerCast();
8282

8383
/** @brief Dangerous type cast */
84-
void warningDangerousOldStyleTypeCast();
84+
void warningDangerousTypeCast();
8585

8686
/** @brief Casting non-hexadecimal integer literal to pointer */
8787
void warningIntToPointerCast();
@@ -205,7 +205,7 @@ class CPPCHECKLIB CheckOther : public Check {
205205
void clarifyStatementError(const Token* tok);
206206
void cstyleCastError(const Token *tok, bool isPtr = true);
207207
void dangerousTypeCastError(const Token *tok, bool isPtr);
208-
void intToPointerCastError(const Token *tok);
208+
void intToPointerCastError(const Token *tok, const std::string& format);
209209
void suspiciousFloatingPointCastError(const Token *tok);
210210
void invalidPointerCastError(const Token* tok, const std::string& from, const std::string& to, bool inconclusive, bool toIsInt);
211211
void passedByValueError(const Variable* var, bool inconclusive, bool isRangeBasedFor = false);

man/checkers/dangerousTypeCast.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# dangerousTypeCast
33

4-
**Message**: Potentially dangerous C style type cast of pointer to object<br/>
4+
**Message**: Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast<br/>
55
**Category**: Type Safety<br/>
66
**Severity**: Warning<br/>
77
**Language**: C++, not applicable for C code

test/testgarbage.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,7 @@ class TestGarbage : public TestFixture {
18321832
ASSERT_THROW_INTERNAL(checkCode("void f(){x=0,return return''[]()}"), SYNTAX);
18331833
ASSERT_THROW_INTERNAL(checkCode("void f(){x='0'++'0'(return)[];}"), SYNTAX); // #9063
18341834
(void)checkCode("void f(){*(int *)42=0;}"); // no syntax error
1835+
ignore_errout(); // we are not interested in the output
18351836
ASSERT_THROW_INTERNAL(checkCode("void f() { x= 'x' > typedef name5 | ( , ;){ } (); }"), SYNTAX); // #9067
18361837
ASSERT_THROW_INTERNAL(checkCode("void f() { x= {}( ) ( 'x')[ ] (); }"), SYNTAX); // #9068
18371838
ASSERT_THROW_INTERNAL(checkCode("void f() { x= y{ } name5 y[ ] + y ^ name5 ^ name5 for ( ( y y y && y y y && name5 ++ int )); }"), SYNTAX); // #9069

0 commit comments

Comments
 (0)