@@ -61,7 +61,7 @@ void CheckBufferOverrun::arrayIndexOutOfBounds(const Token *tok, int size, int i
6161 }
6262}
6363
64- void CheckBufferOverrun::arrayIndexOutOfBounds (const Token *tok, const ArrayInfo &arrayInfo, const std::vector<int > &index)
64+ void CheckBufferOverrun::arrayIndexOutOfBounds (const Token *tok, const ArrayInfo &arrayInfo, const std::vector<unsigned int > &index)
6565{
6666 std::ostringstream oss;
6767 oss << " Array '" << arrayInfo.varname ;
@@ -80,7 +80,7 @@ void CheckBufferOverrun::arrayIndexOutOfBounds(const Token *tok, const ArrayInfo
8080 reportError (tok, Severity::error, " arrayIndexOutOfBounds" , oss.str ().c_str ());
8181}
8282
83- void CheckBufferOverrun::arrayIndexOutOfBounds (const std::list<const Token *> &callstack, const ArrayInfo &arrayInfo, const std::vector<int > &index)
83+ void CheckBufferOverrun::arrayIndexOutOfBounds (const std::list<const Token *> &callstack, const ArrayInfo &arrayInfo, const std::vector<unsigned int > &index)
8484{
8585 std::ostringstream oss;
8686 oss << " Array '" << arrayInfo.varname ;
@@ -586,17 +586,17 @@ void CheckBufferOverrun::checkFunctionCall(const Token &tok, unsigned int par, c
586586 {
587587 if (Token::Match (ftok->previous (), " [=+-*/;{}] %var% [ %num% ]" ))
588588 {
589- unsigned long index = MathLib::toLongNumber (ftok->strAt (2 ));
589+ long index = MathLib::toLongNumber (ftok->strAt (2 ));
590590 if (index >= arrayInfo.num [0 ])
591591 {
592592 std::list<const Token *> callstack;
593593 callstack.push_back (&tok);
594594 callstack.push_back (ftok);
595595
596- std::vector<int > ints ;
597- ints .push_back (index);
596+ std::vector<unsigned int > indexes ;
597+ indexes .push_back (index);
598598
599- arrayIndexOutOfBounds (callstack, arrayInfo, ints );
599+ arrayIndexOutOfBounds (callstack, arrayInfo, indexes );
600600 }
601601 }
602602 }
@@ -613,7 +613,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
613613 for (unsigned int i = 0 ; i < varname.size (); ++i)
614614 varnames += (i == 0 ? " " : " . " ) + varname[i];
615615
616- const unsigned int varc (varname.empty () ? 0 : (varname.size () - 1 ) * 2 );
616+ const unsigned char varc (varname.empty () ? 0U : (varname.size () - 1 ) * 2U );
617617
618618 if (Token::Match (tok, " return" ))
619619 {
@@ -702,7 +702,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
702702 // memset, memcmp, memcpy, strncpy, fgets..
703703 if (varid == 0 )
704704 {
705- ArrayInfo arrayInfo (0 , varnames, 1 , total_size);
705+ ArrayInfo arrayInfo (0U , varnames, 1U , static_cast < unsigned int >( total_size) );
706706 if (Token::Match (tok, (" %var% ( " + varnames + " ," ).c_str ()))
707707 checkFunctionCall (*tok, 1 , arrayInfo);
708708 if (Token::Match (tok, (" %var% ( %var% , " + varnames + " ," ).c_str ()))
@@ -748,19 +748,18 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
748748 if (for_bailout (tok2->next (), counter_varid))
749749 break ;
750750
751- ArrayInfo arrayInfo (varid, varnames, size, total_size);
751+ ArrayInfo arrayInfo (varid, varnames, ( unsigned int ) size, ( unsigned int ) total_size);
752752 parse_for_body (tok2->next (), arrayInfo, strindex, condition_out_of_bounds, counter_varid, min_counter_value, max_counter_value);
753753
754754 continue ;
755755 }
756756
757-
758757 // Writing data into array..
759758 if ((varid > 0 && Token::Match (tok, " strcpy|strcat ( %varid% , %str% )" , varid)) ||
760759 (varid == 0 && Token::Match (tok, (" strcpy|strcat ( " + varnames + " , %str% )" ).c_str ())))
761760 {
762- const long len = Token::getStrLength (tok->tokAt (varc + 4 ));
763- if (len < 0 || len >= total_size)
761+ const size_t len = Token::getStrLength (tok->tokAt (varc + 4 ));
762+ if (total_size > 0 && len >= static_cast < unsigned int >( total_size) )
764763 {
765764 bufferOverrun (tok, varid > 0 ? " " : varnames.c_str ());
766765 continue ;
@@ -788,17 +787,18 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
788787 }
789788
790789 // sprintf..
790+ // TODO: change total_size to an unsigned value and remove the "&& total_size > 0" check.
791791 const std::string sprintfPattern = varid > 0 ? std::string (" sprintf ( %varid% , %str% [,)]" ) : (" sprintf ( " + varnames + " , %str% [,)]" );
792- if (Token::Match (tok, sprintfPattern.c_str (), varid))
792+ if (Token::Match (tok, sprintfPattern.c_str (), varid) && total_size > 0 )
793793 {
794- checkSprintfCall (tok, total_size);
794+ checkSprintfCall (tok, static_cast < unsigned int >( total_size) );
795795 }
796796
797797 // snprintf..
798798 const std::string snprintfPattern = varid > 0 ? std::string (" snprintf ( %varid% , %num% ," ) : (" snprintf ( " + varnames + " , %num% ," );
799799 if (Token::Match (tok, snprintfPattern.c_str (), varid))
800800 {
801- int n = MathLib::toLongNumber (tok->strAt (4 + varc));
801+ const long n = MathLib::toLongNumber (tok->strAt (4 + varc));
802802 if (n > total_size)
803803 outOfBounds (tok->tokAt (4 + varc), " snprintf size" );
804804 }
@@ -833,16 +833,16 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
833833
834834 else if (Token::Match (tok, " %varid% [ %num% ]" , arrayInfo.varid ))
835835 {
836- std::vector<int > indexes;
836+ std::vector<unsigned int > indexes;
837837 for (const Token *tok2 = tok->next (); Token::Match (tok2, " [ %num% ]" ); tok2 = tok2->tokAt (3 ))
838838 {
839- const int index = MathLib::toLongNumber (tok2->strAt (1 ));
839+ const long index = MathLib::toLongNumber (tok2->strAt (1 ));
840840 if (index < 0 )
841841 {
842842 indexes.clear ();
843843 break ;
844844 }
845- indexes.push_back (index);
845+ indexes.push_back (static_cast < unsigned int >( index) );
846846 }
847847 if (indexes.size () == arrayInfo.num .size ())
848848 {
@@ -1090,7 +1090,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
10901090
10911091 if (Token::Match (tok, " %type% *| %var% [ %var% ] [;=]" ))
10921092 {
1093- unsigned int varpos = 1 ;
1093+ unsigned char varpos = 1 ;
10941094 if (tok->next ()->str () == " *" )
10951095 ++varpos;
10961096
@@ -1186,7 +1186,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
11861186 // manually
11871187 unsigned int sizeOfType = _tokenizer->sizeOfType (declTok->next ());
11881188 if (sizeOfType > 0 )
1189- size /= sizeOfType;
1189+ size /= static_cast < int >( sizeOfType) ;
11901190 }
11911191 }
11921192 else
@@ -1199,7 +1199,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
11991199
12001200 Token sizeTok (0 );
12011201 sizeTok.str (type);
1202- int total_size = size * _tokenizer->sizeOfType (&sizeTok);
1202+ int total_size = size * static_cast < int >( _tokenizer->sizeOfType (&sizeTok) );
12031203 if (total_size == 0 )
12041204 continue ;
12051205
@@ -1266,7 +1266,7 @@ void CheckBufferOverrun::checkStructVariable()
12661266 if (Token::simpleMatch (tok4, " ) {" ))
12671267 {
12681268 std::vector<std::string> v;
1269- checkScope (tok4->tokAt (2 ), v, arrayInfo.num [0 ], arrayInfo.num [0 ] * arrayInfo.element_size , arrayInfo.varid );
1269+ checkScope (tok4->tokAt (2 ), v, static_cast < int >( arrayInfo.num [0 ]), static_cast < int >( arrayInfo.num [0 ] * arrayInfo.element_size ) , arrayInfo.varid );
12701270 break ;
12711271 }
12721272 }
@@ -1323,7 +1323,7 @@ void CheckBufferOverrun::checkStructVariable()
13231323 continue ;
13241324
13251325 // Check variable usage..
1326- checkScope (CheckTok, varname, arrayInfo.num [0 ], arrayInfo.num [0 ] * arrayInfo.element_size , 0 );
1326+ checkScope (CheckTok, varname, static_cast < int >( arrayInfo.num [0 ]), static_cast < int >( arrayInfo.num [0 ] * arrayInfo.element_size ) , 0 );
13271327 }
13281328 }
13291329 }
@@ -1340,10 +1340,10 @@ void CheckBufferOverrun::bufferOverrun()
13401340// ---------------------------------------------------------------------------
13411341
13421342
1343- int CheckBufferOverrun::countSprintfLength (const std::string &input_string, const std::list<const Token*> ¶meters)
1343+ unsigned int CheckBufferOverrun::countSprintfLength (const std::string &input_string, const std::list<const Token*> ¶meters)
13441344{
13451345 bool percentCharFound = false ;
1346- int input_string_size = 1 ;
1346+ unsigned int input_string_size = 1 ;
13471347 bool handleNextParameter = false ;
13481348 std::string digits_string = " " ;
13491349 bool i_d_x_f_found = false ;
@@ -1408,14 +1408,14 @@ int CheckBufferOverrun::countSprintfLength(const std::string &input_string, cons
14081408
14091409 if (handleNextParameter)
14101410 {
1411- unsigned int tempDigits = std::abs (std::atoi (digits_string.c_str ()));
1411+ unsigned int tempDigits = static_cast < unsigned int >( std::abs (std::atoi (digits_string.c_str () )));
14121412 if (i_d_x_f_found)
1413- tempDigits = std::max (static_cast <int >(tempDigits), 1 );
1413+ tempDigits = std::max (static_cast <unsigned int >(tempDigits), 1U );
14141414
14151415 if (digits_string.find (' .' ) != std::string::npos)
14161416 {
14171417 const std::string endStr = digits_string.substr (digits_string.find (' .' ) + 1 );
1418- unsigned int maxLen = std::max (std::abs (std::atoi (endStr.c_str ())), 1 );
1418+ unsigned int maxLen = std::max (static_cast < unsigned int >( std::abs (std::atoi (endStr.c_str ()))), 1U );
14191419
14201420 if (input_string[i] == ' s' )
14211421 {
@@ -1451,7 +1451,7 @@ int CheckBufferOverrun::countSprintfLength(const std::string &input_string, cons
14511451 return input_string_size;
14521452}
14531453
1454- void CheckBufferOverrun::checkSprintfCall (const Token *tok, int size)
1454+ void CheckBufferOverrun::checkSprintfCall (const Token *tok, const unsigned int size)
14551455{
14561456 const Token *end = tok->next ()->link ();
14571457
@@ -1471,7 +1471,6 @@ void CheckBufferOverrun::checkSprintfCall(const Token *tok, int size)
14711471 {
14721472 if (Token::Match (tok2, " , %any% [,)]" ))
14731473 {
1474-
14751474 if (Token::Match (tok2->next (), " %str%" ))
14761475 parameters.push_back (tok2->next ());
14771476
@@ -1512,7 +1511,7 @@ void CheckBufferOverrun::checkSprintfCall(const Token *tok, int size)
15121511 }
15131512 }
15141513
1515- int len = countSprintfLength (tok->tokAt (4 + varc)->strValue (), parameters);
1514+ unsigned int len = countSprintfLength (tok->tokAt (4 + varc)->strValue (), parameters);
15161515 if (len > size)
15171516 {
15181517 bufferOverrun (tok);
@@ -1920,7 +1919,7 @@ class ExecutionPathBufferOverrun : public ExecutionPath
19201919 CheckBufferOverrun *checkBufferOverrun = dynamic_cast <CheckBufferOverrun *>(c->owner );
19211920 if (checkBufferOverrun)
19221921 {
1923- std::vector<int > index;
1922+ std::vector<unsigned int > index;
19241923 index.push_back (c->value );
19251924 checkBufferOverrun->arrayIndexOutOfBounds (tok, ai, index);
19261925 break ;
0 commit comments