Skip to content

Commit db8e755

Browse files
authored
fixed some type mismatches (based on sign mismatch compiler warnings) (#6983)
1 parent 14400be commit db8e755

11 files changed

+41
-41
lines changed

lib/checkbufferoverrun.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static int getMinFormatStringOutputLength(const std::vector<const Token*> &param
8686
bool i_d_x_f_found = false;
8787
int parameterLength = 0;
8888
int inputArgNr = formatStringArgNr;
89-
for (int i = 1; i + 1 < formatString.length(); ++i) {
89+
for (std::size_t i = 1; i + 1 < formatString.length(); ++i) {
9090
if (formatString[i] == '\\') {
9191
if (i < formatString.length() - 1 && formatString[i + 1] == '0')
9292
break;
@@ -255,7 +255,7 @@ static std::vector<ValueFlow::Value> getOverrunIndexValues(const Token* tok,
255255

256256
bool overflow = false;
257257
std::vector<ValueFlow::Value> indexValues;
258-
for (int i = 0; i < dimensions.size() && i < indexTokens.size(); ++i) {
258+
for (std::size_t i = 0; i < dimensions.size() && i < indexTokens.size(); ++i) {
259259
MathLib::bigint size = dimensions[i].num;
260260
if (!isArrayIndex)
261261
size++;
@@ -600,7 +600,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
600600
case Library::ArgumentChecks::MinSize::Type::ARGVALUE: {
601601
if (arg && arg->hasKnownIntValue()) {
602602
MathLib::bigint myMinsize = arg->getKnownIntValue();
603-
const unsigned int baseSize = tokenizer->sizeOfType(minsize.baseType);
603+
const int baseSize = tokenizer->sizeOfType(minsize.baseType);
604604
if (baseSize != 0)
605605
myMinsize *= baseSize;
606606
return myMinsize <= bufferSize;
@@ -616,7 +616,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
616616
break;
617617
case Library::ArgumentChecks::MinSize::Type::VALUE: {
618618
MathLib::bigint myMinsize = minsize.value;
619-
const unsigned int baseSize = tokenizer->sizeOfType(minsize.baseType);
619+
const int baseSize = tokenizer->sizeOfType(minsize.baseType);
620620
if (baseSize != 0)
621621
myMinsize *= baseSize;
622622
return myMinsize <= bufferSize;
@@ -848,7 +848,7 @@ void CheckBufferOverrun::argumentSize()
848848
if (calldata->variable()->dimensions().size() != argument->dimensions().size())
849849
continue;
850850
bool err = false;
851-
for (int d = 0; d < argument->dimensions().size(); ++d) {
851+
for (std::size_t d = 0; d < argument->dimensions().size(); ++d) {
852852
const auto& dim1 = calldata->variable()->dimensions()[d];
853853
const auto& dim2 = argument->dimensions()[d];
854854
if (!dim1.known || !dim2.known)

lib/checkclass.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ void CheckClass::privateFunctions()
13221322
bool used = checkFunctionUsage(pf, scope); // Usage in this class
13231323
// Check in friend classes
13241324
const std::vector<Type::FriendInfo>& friendList = scope->definedType->friendList;
1325-
for (int i = 0; i < friendList.size() && !used; i++) {
1325+
for (std::size_t i = 0; i < friendList.size() && !used; i++) {
13261326
if (friendList[i].type)
13271327
used = checkFunctionUsage(pf, friendList[i].type->classScope);
13281328
else
@@ -2715,7 +2715,7 @@ void CheckClass::initializerListOrder()
27152715
}
27162716
}
27172717

2718-
for (int j = 0; j < vars.size(); j++) {
2718+
for (std::size_t j = 0; j < vars.size(); j++) {
27192719
// check for use of uninitialized arguments
27202720
for (const auto& arg : vars[j].initArgs)
27212721
if (vars[j].var->index() < arg->index())

lib/checkother.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3765,14 +3765,14 @@ void CheckOther::funcArgOrderDifferent(const std::string & functionName,
37653765
!definitions.empty() ? definitions[0] ? definitions[0] : definition : nullptr
37663766
};
37673767
std::string msg = "$symbol:" + functionName + "\nFunction '$symbol' argument order different: declaration '";
3768-
for (int i = 0; i < declarations.size(); ++i) {
3768+
for (std::size_t i = 0; i < declarations.size(); ++i) {
37693769
if (i != 0)
37703770
msg += ", ";
37713771
if (declarations[i])
37723772
msg += declarations[i]->str();
37733773
}
37743774
msg += "' definition '";
3775-
for (int i = 0; i < definitions.size(); ++i) {
3775+
for (std::size_t i = 0; i < definitions.size(); ++i) {
37763776
if (i != 0)
37773777
msg += ", ";
37783778
if (definitions[i])

lib/clangimport.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -385,21 +385,21 @@ namespace clangimport {
385385
std::string clangimport::AstNode::getSpelling() const
386386
{
387387
if (nodeType == CompoundAssignOperator) {
388-
int typeIndex = 1;
388+
std::size_t typeIndex = 1;
389389
while (typeIndex < mExtTokens.size() && mExtTokens[typeIndex][0] != '\'')
390390
typeIndex++;
391391
// name is next quoted token
392-
int nameIndex = typeIndex + 1;
392+
std::size_t nameIndex = typeIndex + 1;
393393
while (nameIndex < mExtTokens.size() && mExtTokens[nameIndex][0] != '\'')
394394
nameIndex++;
395395
return (nameIndex < mExtTokens.size()) ? unquote(mExtTokens[nameIndex]) : "";
396396
}
397397

398398
if (nodeType == UnaryExprOrTypeTraitExpr) {
399-
int typeIndex = 1;
399+
std::size_t typeIndex = 1;
400400
while (typeIndex < mExtTokens.size() && mExtTokens[typeIndex][0] != '\'')
401401
typeIndex++;
402-
const int nameIndex = typeIndex + 1;
402+
const std::size_t nameIndex = typeIndex + 1;
403403
return (nameIndex < mExtTokens.size()) ? unquote(mExtTokens[nameIndex]) : "";
404404
}
405405

@@ -449,7 +449,7 @@ std::string clangimport::AstNode::getType(int index) const
449449

450450
std::string clangimport::AstNode::getFullType(int index) const
451451
{
452-
int typeIndex = 1;
452+
std::size_t typeIndex = 1;
453453
while (typeIndex < mExtTokens.size() && mExtTokens[typeIndex][0] != '\'')
454454
typeIndex++;
455455
if (typeIndex >= mExtTokens.size())
@@ -867,7 +867,7 @@ Token *clangimport::AstNode::createTokens(TokenList &tokenList)
867867
Token *expr1 = varDecl->createTokens(tokenList);
868868
Token *colon = addtoken(tokenList, ":");
869869
AstNodePtr range;
870-
for (int i = 0; i < 2; i++) {
870+
for (std::size_t i = 0; i < 2; i++) {
871871
if (children[i] && children[i]->nodeType == DeclStmt && children[i]->getChild(0)->nodeType == VarDecl) {
872872
range = children[i]->getChild(0)->getChild(0);
873873
break;
@@ -890,7 +890,7 @@ Token *clangimport::AstNode::createTokens(TokenList &tokenList)
890890
return nullptr;
891891
}
892892
if (nodeType == CXXMethodDecl) {
893-
for (int i = 0; i+1 < mExtTokens.size(); ++i) {
893+
for (std::size_t i = 0; i+1 < mExtTokens.size(); ++i) {
894894
if (mExtTokens[i] == "prev" && !mData->hasDecl(mExtTokens[i+1]))
895895
return nullptr;
896896
}
@@ -1288,11 +1288,11 @@ Token * clangimport::AstNode::createTokensCall(TokenList &tokenList)
12881288
f->setValueType(nullptr);
12891289
Token *par1 = addtoken(tokenList, "(");
12901290
par1->astOperand1(f);
1291-
int args = 0;
1291+
std::size_t args = 0;
12921292
while (args < children.size() && children[args]->nodeType != CXXDefaultArgExpr)
12931293
args++;
12941294
Token *child = nullptr;
1295-
for (int c = firstParam; c < args; ++c) {
1295+
for (std::size_t c = firstParam; c < args; ++c) {
12961296
if (child) {
12971297
Token *comma = addtoken(tokenList, ",");
12981298
comma->setValueType(nullptr);

lib/preprocessor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string>
542542
ifndef = true;
543543
else {
544544
const std::array<std::string, 6> match{"if", "!", "defined", "(", config, ")"};
545-
int i = 0;
545+
std::size_t i = 0;
546546
ifndef = true;
547547
for (const simplecpp::Token *t = cmdtok; i < match.size(); t = t->next) {
548548
if (!t || t->str() != match[i++]) {

lib/symboldatabase.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ void SymbolDatabase::createSymbolDatabaseVariableSymbolTable()
10391039
for (Scope& scope : scopeList) {
10401040
// add all variables
10411041
for (Variable& var: scope.varlist) {
1042-
const unsigned int varId = var.declarationId();
1042+
const int varId = var.declarationId();
10431043
if (varId)
10441044
mVariableList[varId] = &var;
10451045
// fix up variables without type
@@ -1055,7 +1055,7 @@ void SymbolDatabase::createSymbolDatabaseVariableSymbolTable()
10551055
for (Variable& arg: func.argumentList) {
10561056
// check for named parameters
10571057
if (arg.nameToken() && arg.declarationId()) {
1058-
const unsigned int declarationId = arg.declarationId();
1058+
const int declarationId = arg.declarationId();
10591059
mVariableList[declarationId] = &arg;
10601060
// fix up parameters without type
10611061
if (!arg.type() && !arg.typeStartToken()->isStandardType()) {
@@ -2117,7 +2117,7 @@ namespace {
21172117
{
21182118
if (const Scope* scope = var->nameToken()->scope()) {
21192119
auto it = std::find_if(scope->functionList.begin(), scope->functionList.end(), [&](const Function& function) {
2120-
for (std::size_t arg = 0; arg < function.argCount(); ++arg) {
2120+
for (nonneg int arg = 0; arg < function.argCount(); ++arg) {
21212121
if (var == function.getArgumentVar(arg))
21222122
return true;
21232123
}
@@ -3011,7 +3011,7 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se
30113011
// remove class name
30123012
else if (arg_path_length > 2 && first->strAt(1) != second->strAt(1)) {
30133013
std::string short_path = path;
3014-
unsigned int short_path_length = arg_path_length;
3014+
int short_path_length = arg_path_length;
30153015

30163016
// remove last " :: "
30173017
short_path.resize(short_path.size() - 4);
@@ -3034,7 +3034,7 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se
30343034

30353035
param = std::move(short_path);
30363036
if (Token::simpleMatch(second->next(), param.c_str(), param.size())) {
3037-
second = second->tokAt(int(short_path_length));
3037+
second = second->tokAt(short_path_length);
30383038
arg_path_length = 0;
30393039
}
30403040
}
@@ -5961,7 +5961,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst, Referen
59615961
// Prioritize matches in derived scopes
59625962
for (const auto& fb : { fallback1Func, fallback2Func }) {
59635963
const Function* ret = nullptr;
5964-
for (int i = 0; i < fb.size(); ++i) {
5964+
for (std::size_t i = 0; i < fb.size(); ++i) {
59655965
if (std::find(matches.cbegin(), matches.cend(), fb[i]) == matches.cend())
59665966
continue;
59675967
if (this == fb[i]->nestedIn) {
@@ -7144,7 +7144,7 @@ static const Token* parsedecl(const Token* type,
71447144
if (settings.debugnormal || settings.debugwarnings)
71457145
valuetype->setDebugPath(type, loc);
71467146
const Token * const previousType = type;
7147-
const unsigned int pointer0 = valuetype->pointer;
7147+
const int pointer0 = valuetype->pointer;
71487148
while (Token::Match(type->previous(), "%name%") && !endsWith(type->strAt(-1), ':'))
71497149
type = type->previous();
71507150
valuetype->sign = ValueType::Sign::UNKNOWN_SIGN;
@@ -7477,9 +7477,9 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
74777477
} else if (tok->isBoolean()) {
74787478
setValueType(tok, ValueType(ValueType::Sign::UNKNOWN_SIGN, ValueType::Type::BOOL, 0U));
74797479
} else if (tok->tokType() == Token::eChar || tok->tokType() == Token::eString) {
7480-
nonneg int const pointer = tok->tokType() == Token::eChar ? 0U : 1U;
7481-
nonneg int const constness = tok->tokType() == Token::eChar ? 0U : 1U;
7482-
nonneg int const volatileness = 0U;
7480+
nonneg int const pointer = tok->tokType() == Token::eChar ? 0 : 1;
7481+
nonneg int const constness = tok->tokType() == Token::eChar ? 0 : 1;
7482+
nonneg int const volatileness = 0;
74837483
ValueType valuetype(ValueType::Sign::UNKNOWN_SIGN, ValueType::Type::CHAR, pointer, constness, volatileness);
74847484

74857485
if (tok->isCpp() && mSettings.standards.cpp >= Standards::CPP20 && tok->isUtf8()) {

lib/tokenize.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ void Tokenizer::simplifyTypedefCpp()
17441744
--classLevel;
17451745
pattern.clear();
17461746

1747-
for (int i = classLevel; i < spaceInfo.size(); ++i)
1747+
for (std::size_t i = classLevel; i < spaceInfo.size(); ++i)
17481748
pattern += (spaceInfo[i].className + " :: ");
17491749

17501750
pattern += typeName->str();
@@ -1791,7 +1791,7 @@ void Tokenizer::simplifyTypedefCpp()
17911791
spaceInfo[classLevel].bodyEnd2 = tok2->link();
17921792
++classLevel;
17931793
pattern.clear();
1794-
for (int i = classLevel; i < spaceInfo.size(); ++i)
1794+
for (std::size_t i = classLevel; i < spaceInfo.size(); ++i)
17951795
pattern += spaceInfo[i].className + " :: ";
17961796

17971797
pattern += typeName->str();
@@ -1880,7 +1880,7 @@ void Tokenizer::simplifyTypedefCpp()
18801880
}
18811881

18821882
// remove qualification if present
1883-
for (int i = classLevel; i < spaceInfo.size(); ++i) {
1883+
for (std::size_t i = classLevel; i < spaceInfo.size(); ++i) {
18841884
if (!removed.empty())
18851885
removed += " ";
18861886
removed += (tok2->str() + " " + tok2->strAt(1));
@@ -2021,7 +2021,7 @@ void Tokenizer::simplifyTypedefCpp()
20212021
tok2 = tok2->next();
20222022
}
20232023

2024-
for (int i = classLevel; i < spaceInfo.size(); ++i) {
2024+
for (std::size_t i = classLevel; i < spaceInfo.size(); ++i) {
20252025
tok2->insertToken(spaceInfo[i].className);
20262026
tok2 = tok2->next();
20272027
tok2->insertToken("::");
@@ -7489,7 +7489,7 @@ void Tokenizer::simplifyStaticConst()
74897489
// move 'static' before all other qualifiers and types, ...
74907490
for (Token *tok = list.front(); tok; tok = tok->next()) {
74917491
bool continue2 = false;
7492-
for (int i = 0; i < sizeof(qualifiers)/sizeof(qualifiers[0]); i++) {
7492+
for (std::size_t i = 0; i < sizeof(qualifiers)/sizeof(qualifiers[0]); i++) {
74937493

74947494
// Keep searching for a qualifier
74957495
if (!tok->next() || tok->strAt(1) != qualifiers[i])
@@ -7499,7 +7499,7 @@ void Tokenizer::simplifyStaticConst()
74997499
Token* leftTok = tok;
75007500
bool behindOther = false;
75017501
for (; leftTok; leftTok = leftTok->previous()) {
7502-
for (int j = 0; j <= i; j++) {
7502+
for (std::size_t j = 0; j <= i; j++) {
75037503
if (leftTok->str() == qualifiers[j]) {
75047504
behindOther = true;
75057505
break;
@@ -7717,7 +7717,7 @@ void Tokenizer::simplifyInitVar()
77177717
tok1 = tok1->next();
77187718
tok1->str(";");
77197719

7720-
const int numTokens = (Token::Match(tok, "class|struct|union")) ? 2U : 1U;
7720+
const int numTokens = (Token::Match(tok, "class|struct|union")) ? 2 : 1;
77217721
TokenList::insertTokens(tok1, tok, numTokens);
77227722
tok = initVar(tok);
77237723
}
@@ -8171,7 +8171,7 @@ void Tokenizer::cppcheckError(const Token *tok) const
81718171
void Tokenizer::unhandledCharLiteral(const Token *tok, const std::string& msg) const
81728172
{
81738173
std::string s = tok ? (" " + tok->str()) : "";
8174-
for (int i = 0; i < s.size(); ++i) {
8174+
for (std::size_t i = 0; i < s.size(); ++i) {
81758175
if ((unsigned char)s[i] >= 0x80)
81768176
s.clear();
81778177
}

lib/valueflow.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4090,7 +4090,7 @@ static void valueFlowFunctionDefaultParameter(const TokenList& tokenlist, const
40904090
const Function* function = scope->function;
40914091
if (!function)
40924092
continue;
4093-
for (std::size_t arg = function->minArgCount(); arg < function->argCount(); arg++) {
4093+
for (nonneg int arg = function->minArgCount(); arg < function->argCount(); arg++) {
40944094
const Variable* var = function->getArgumentVar(arg);
40954095
if (var && var->hasDefault() && Token::Match(var->nameToken(), "%var% = %num%|%str%|%char%|%name% [,)]")) {
40964096
const std::list<ValueFlow::Value> &values = var->nameToken()->tokAt(2)->values();

test/testprocessexecutor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class TestProcessExecutorBase : public TestFixture {
120120
ASSERT_EQUALS(opt.executeCommandCalled, executeCommandCalled);
121121
ASSERT_EQUALS(opt.exe, exe);
122122
ASSERT_EQUALS(opt.args.size(), args.size());
123-
for (int i = 0; i < args.size(); ++i)
123+
for (std::size_t i = 0; i < args.size(); ++i)
124124
{
125125
ASSERT_EQUALS(opt.args[i], args[i]);
126126
}

test/testsingleexecutor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class TestSingleExecutorBase : public TestFixture {
127127
ASSERT_EQUALS(opt.executeCommandCalled, executeCommandCalled);
128128
ASSERT_EQUALS(opt.exe, exe);
129129
ASSERT_EQUALS(opt.args.size(), args.size());
130-
for (int i = 0; i < args.size(); ++i)
130+
for (std::size_t i = 0; i < args.size(); ++i)
131131
{
132132
ASSERT_EQUALS(opt.args[i], args[i]);
133133
}

test/testthreadexecutor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class TestThreadExecutorBase : public TestFixture {
121121
ASSERT_EQUALS(opt.executeCommandCalled, executeCommandCalled);
122122
ASSERT_EQUALS(opt.exe, exe);
123123
ASSERT_EQUALS(opt.args.size(), args.size());
124-
for (int i = 0; i < args.size(); ++i)
124+
for (std::size_t i = 0; i < args.size(); ++i)
125125
{
126126
ASSERT_EQUALS(opt.args[i], args[i]);
127127
}

0 commit comments

Comments
 (0)