@@ -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 );
0 commit comments