Skip to content

Commit adc47f1

Browse files
author
Daniel Marjamäki
committed
Fixed cppcheck-opensource#1487 (fix gcc compiler warnings)
1 parent 271a74a commit adc47f1

9 files changed

Lines changed: 84 additions & 84 deletions

File tree

Makefile

Lines changed: 39 additions & 39 deletions
Large diffs are not rendered by default.

lib/checkbufferoverrun.cpp

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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*> &parameters)
1343+
unsigned int CheckBufferOverrun::countSprintfLength(const std::string &input_string, const std::list<const Token*> &parameters)
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;

lib/checkbufferoverrun.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ class CheckBufferOverrun : public Check
7979
* @param parameters given parameters to sprintf
8080
* @return minimum length of resulting string
8181
*/
82-
static int countSprintfLength(const std::string &input_string, const std::list<const Token*> &parameters);
82+
static unsigned int countSprintfLength(const std::string &input_string, const std::list<const Token*> &parameters);
8383

8484
/**
8585
* @brief %Check code that matches: "sprintf ( %varid% , %str% [,)]" when varid is not 0,
8686
* and report found errors.
8787
* @param tok The "sprintf" token.
8888
* @param size The size of the buffer where sprintf is writing.
8989
*/
90-
void checkSprintfCall(const Token *tok, int size);
90+
void checkSprintfCall(const Token *tok, const unsigned int size);
9191

9292
/** Check for buffer overruns - locate struct variables and check them with the .._CheckScope function */
9393
void checkStructVariable();
@@ -176,8 +176,8 @@ class CheckBufferOverrun : public Check
176176
void checkFunctionCall(const Token &tok, const unsigned int par, const ArrayInfo &arrayInfo);
177177

178178
void arrayIndexOutOfBounds(const Token *tok, int size, int index);
179-
void arrayIndexOutOfBounds(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<int> &index);
180-
void arrayIndexOutOfBounds(const std::list<const Token *> &callstack, const ArrayInfo &arrayInfo, const std::vector<int> &index);
179+
void arrayIndexOutOfBounds(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<unsigned int> &index);
180+
void arrayIndexOutOfBounds(const std::list<const Token *> &callstack, const ArrayInfo &arrayInfo, const std::vector<unsigned int> &index);
181181
void bufferOverrun(const Token *tok, const std::string &varnames = "");
182182
void strncatUsage(const Token *tok);
183183
void outOfBounds(const Token *tok, const std::string &what);

lib/checkclass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ bool CheckClass::argsMatch(const Token *first, const Token *second, const std::s
777777

778778
if (Token::Match(second->next(), param.c_str()))
779779
{
780-
second = second->tokAt(depth * 2);
780+
second = second->tokAt(int(depth) * 2);
781781
}
782782
else if (depth > 1)
783783
{
@@ -794,7 +794,7 @@ bool CheckClass::argsMatch(const Token *first, const Token *second, const std::s
794794

795795
if (Token::Match(second->next(), param.c_str()))
796796
{
797-
second = second->tokAt((depth - 1) * 2);
797+
second = second->tokAt((int(depth) - 1) * 2);
798798
}
799799
}
800800
}
@@ -1892,7 +1892,7 @@ void CheckClass::checkConst()
18921892
const std::string s(previous->str());
18931893
for (std::string::size_type pos = 0; pos < s.size(); ++pos)
18941894
{
1895-
unsigned char ch = s[pos];
1895+
const char ch = s[pos];
18961896
if (!(ch == '_' || (ch >= 'A' && ch <= 'Z')))
18971897
{
18981898
allupper = false;

lib/filelister.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ std::string FileLister::simplifyPath(const char *originalPath)
7171
if (subPath.length() > 0)
7272
pathParts.push_back(subPath);
7373

74-
for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i)
74+
for (unsigned int i = 0; i < pathParts.size(); ++i)
7575
{
7676
if (pathParts[i] == ".." && i > 1 && pathParts.size() > i + 1)
7777
{

lib/preprocessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
219219
unsigned char previous = 0;
220220
std::vector<std::string> suppressionIDs;
221221

222-
for (std::string::size_type i = static_cast<unsigned char>(hasbom(str) ? 3 : 0); i < str.length(); ++i)
222+
for (std::string::size_type i = hasbom(str) ? 3U : 0U; i < str.length(); ++i)
223223
{
224224
unsigned char ch = static_cast<unsigned char>(str[i]);
225225
if (ch & 0x80)

lib/token.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ std::string Token::stringifyList(bool varid, const char *title, const std::vecto
684684

685685
unsigned int lineNumber = 0;
686686
int fileInd = -1;
687-
std::map<unsigned int, unsigned int> lineNumbers;
687+
std::map<int, unsigned int> lineNumbers;
688688
for (const Token *tok = this; tok; tok = tok->next())
689689
{
690690
bool fileChange = false;
@@ -697,8 +697,8 @@ std::string Token::stringifyList(bool varid, const char *title, const std::vecto
697697

698698
fileInd = static_cast<int>(tok->_fileIndex);
699699
ret << "\n\n##file ";
700-
if (fileNames.size() > static_cast<unsigned int>(fileInd))
701-
ret << fileNames.at(fileInd);
700+
if (fileNames.size() > tok->_fileIndex)
701+
ret << fileNames.at(tok->_fileIndex);
702702
else
703703
ret << fileInd;
704704

test/testthreadexecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class TestThreadExecutor : public TestFixture
4545
* Execute check using n jobs for y files which are have
4646
* identical data, given within data.
4747
*/
48-
void check(int jobs, int files, int result, const std::string &data)
48+
void check(unsigned int jobs, int files, int result, const std::string &data)
4949
{
5050
errout.str("");
5151
output.str("");

tools/dmake.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ int main(int argc, char **argv)
163163
<< "-Wno-long-long "
164164
<< "-Wfloat-equal "
165165
<< "-Wcast-qual "
166+
<< "-Wsign-conversion "
166167
<< "-g -D_GLIBCXX_DEBUG\n";
167168
}
168169
fout << "CXX=g++\n";

0 commit comments

Comments
 (0)