@@ -1459,7 +1459,9 @@ void CheckOther::passedByValueError(const Token *tok, const std::string &parname
14591459
14601460void CheckOther::checkCharVariable ()
14611461{
1462- if (!_settings->isEnabled (" warning" ))
1462+ const bool warning = _settings->isEnabled (" warning" );
1463+ const bool portability = _settings->isEnabled (" portability" );
1464+ if (!warning && !portability)
14631465 return ;
14641466
14651467 const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase ();
@@ -1468,12 +1470,16 @@ void CheckOther::checkCharVariable()
14681470 const Scope * scope = symbolDatabase->functionScopes [i];
14691471 for (const Token* tok = scope->classStart ; tok != scope->classEnd ; tok = tok->next ()) {
14701472 if (Token::Match (tok, " %var% [" )) {
1471- if (!tok->variable () || !tok->variable ()->isArray ())
1473+ if (!tok->variable ())
1474+ continue ;
1475+ if (!tok->variable ()->isArray () && !tok->variable ()->isPointer ())
14721476 continue ;
14731477 const Token *index = tok->next ()->astOperand2 ();
1474- if (astIsSignedChar (index) && index->getValueGE (0x80 , _settings))
1475- charArrayIndexError (tok);
1476- } else if (Token::Match (tok, " [&|^]" ) && tok->astOperand2 () && tok->astOperand1 ()) {
1478+ if (warning && tok->variable ()->isArray () && astIsSignedChar (index) && index->getValueGE (0x80 , _settings))
1479+ signedCharArrayIndexError (tok);
1480+ if (portability && astIsUnknownSignChar (index) && index->getValueGE (0x80 , _settings))
1481+ unknownSignCharArrayIndexError (tok);
1482+ } else if (warning && Token::Match (tok, " [&|^]" ) && tok->astOperand2 () && tok->astOperand1 ()) {
14771483 bool warn = false ;
14781484 if (astIsSignedChar (tok->astOperand1 ())) {
14791485 const ValueFlow::Value *v1 = tok->astOperand1 ()->getValueLE (-1 , _settings);
@@ -1502,17 +1508,27 @@ void CheckOther::checkCharVariable()
15021508 }
15031509}
15041510
1505- void CheckOther::charArrayIndexError (const Token *tok)
1511+ void CheckOther::signedCharArrayIndexError (const Token *tok)
15061512{
15071513 reportError (tok,
15081514 Severity::warning,
1509- " charArrayIndex " ,
1515+ " signedCharArrayIndex " ,
15101516 " Signed 'char' type used as array index.\n "
15111517 " Signed 'char' type used as array index. If the value "
15121518 " can be greater than 127 there will be a buffer underflow "
15131519 " because of sign extension." );
15141520}
15151521
1522+ void CheckOther::unknownSignCharArrayIndexError (const Token *tok)
1523+ {
1524+ reportError (tok,
1525+ Severity::portability,
1526+ " unknownSignCharArrayIndex" ,
1527+ " 'char' type used as array index.\n "
1528+ " 'char' type used as array index. Values greater that 127 will be "
1529+ " treated depending on whether 'char' is signed or unsigned on target platform." );
1530+ }
1531+
15161532void CheckOther::charBitOpError (const Token *tok)
15171533{
15181534 reportError (tok,
0 commit comments