@@ -1417,7 +1417,9 @@ void CheckOther::passedByValueError(const Token *tok, const std::string &parname
14171417
14181418void CheckOther::checkCharVariable ()
14191419{
1420- if (!_settings->isEnabled (" warning" ))
1420+ const bool warning = _settings->isEnabled (" warning" );
1421+ const bool portability = _settings->isEnabled (" portability" );
1422+ if (!warning && !portability)
14211423 return ;
14221424
14231425 const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase ();
@@ -1426,12 +1428,16 @@ void CheckOther::checkCharVariable()
14261428 const Scope * scope = symbolDatabase->functionScopes [i];
14271429 for (const Token* tok = scope->classStart ; tok != scope->classEnd ; tok = tok->next ()) {
14281430 if (Token::Match (tok, " %var% [" )) {
1429- if (!tok->variable () || !tok->variable ()->isArray ())
1431+ if (!tok->variable ())
1432+ continue ;
1433+ if (!tok->variable ()->isArray () && !tok->variable ()->isPointer ())
14301434 continue ;
14311435 const Token *index = tok->next ()->astOperand2 ();
1432- if (astIsSignedChar (index) && index->getValueGE (0x80 , _settings))
1433- charArrayIndexError (tok);
1434- } else if (Token::Match (tok, " [&|^]" ) && tok->astOperand2 () && tok->astOperand1 ()) {
1436+ if (warning && tok->variable ()->isArray () && astIsSignedChar (index) && index->getValueGE (0x80 , _settings))
1437+ signedCharArrayIndexError (tok);
1438+ if (portability && astIsUnknownSignChar (index) && index->getValueGE (0x80 , _settings))
1439+ unknownSignCharArrayIndexError (tok);
1440+ } else if (warning && Token::Match (tok, " [&|^]" ) && tok->astOperand2 () && tok->astOperand1 ()) {
14351441 bool warn = false ;
14361442 if (astIsSignedChar (tok->astOperand1 ())) {
14371443 const ValueFlow::Value *v1 = tok->astOperand1 ()->getValueLE (-1 , _settings);
@@ -1460,17 +1466,27 @@ void CheckOther::checkCharVariable()
14601466 }
14611467}
14621468
1463- void CheckOther::charArrayIndexError (const Token *tok)
1469+ void CheckOther::signedCharArrayIndexError (const Token *tok)
14641470{
14651471 reportError (tok,
14661472 Severity::warning,
1467- " charArrayIndex " ,
1473+ " signedCharArrayIndex " ,
14681474 " Signed 'char' type used as array index.\n "
14691475 " Signed 'char' type used as array index. If the value "
14701476 " can be greater than 127 there will be a buffer underflow "
14711477 " because of sign extension." );
14721478}
14731479
1480+ void CheckOther::unknownSignCharArrayIndexError (const Token *tok)
1481+ {
1482+ reportError (tok,
1483+ Severity::portability,
1484+ " unknownSignCharArrayIndex" ,
1485+ " 'char' type used as array index.\n "
1486+ " 'char' type used as array index. Values greater that 127 will be "
1487+ " treated depending on whether 'char' is signed or unsigned on target platform." );
1488+ }
1489+
14741490void CheckOther::charBitOpError (const Token *tok)
14751491{
14761492 reportError (tok,
0 commit comments