Skip to content

Commit e406b37

Browse files
authored
fixed #13488 - provide error location of MathLib::to*Number() (danmar#4961)
1 parent 9ad461a commit e406b37

19 files changed

+165
-83
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ $(libcppdir)/keywords.o: lib/keywords.cpp lib/config.h lib/keywords.h lib/standa
589589
$(libcppdir)/library.o: lib/library.cpp externals/tinyxml2/tinyxml2.h lib/astutils.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
590590
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/library.cpp
591591

592-
$(libcppdir)/mathlib.o: lib/mathlib.cpp externals/simplecpp/simplecpp.h lib/config.h lib/errortypes.h lib/mathlib.h lib/utils.h
592+
$(libcppdir)/mathlib.o: lib/mathlib.cpp externals/simplecpp/simplecpp.h lib/config.h lib/errortypes.h lib/mathlib.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vfvalue.h
593593
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/mathlib.cpp
594594

595595
$(libcppdir)/path.o: lib/path.cpp externals/simplecpp/simplecpp.h lib/config.h lib/path.h lib/standards.h lib/utils.h
@@ -784,7 +784,7 @@ test/testleakautovar.o: test/testleakautovar.cpp externals/simplecpp/simplecpp.h
784784
test/testlibrary.o: test/testlibrary.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
785785
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testlibrary.cpp
786786

787-
test/testmathlib.o: test/testmathlib.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h test/fixture.h
787+
test/testmathlib.o: test/testmathlib.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h
788788
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testmathlib.cpp
789789

790790
test/testmemleak.o: test/testmemleak.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/checkmemoryleak.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h

lib/checkcondition.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void CheckCondition::assignIf()
107107

108108
if (Token::Match(tok->next(), "%num% [&|]")) {
109109
bitop = tok->strAt(2).at(0);
110-
num = MathLib::toBigNumber(tok->strAt(1));
110+
num = MathLib::toBigNumber(tok->tokAt(1));
111111
} else {
112112
const Token *endToken = Token::findsimplematch(tok, ";");
113113

@@ -117,7 +117,7 @@ void CheckCondition::assignIf()
117117

118118
if (endToken && Token::Match(endToken->tokAt(-2), "[&|] %num% ;")) {
119119
bitop = endToken->strAt(-2).at(0);
120-
num = MathLib::toBigNumber(endToken->strAt(-1));
120+
num = MathLib::toBigNumber(endToken->tokAt(-1));
121121
}
122122
}
123123

@@ -170,15 +170,15 @@ bool CheckCondition::assignIfParseScope(const Token * const assignTok,
170170

171171
for (const Token *tok2 = startTok; tok2; tok2 = tok2->next()) {
172172
if ((bitop == '&') && Token::Match(tok2->tokAt(2), "%varid% %cop% %num% ;", varid) && tok2->strAt(3) == std::string(1U, bitop)) {
173-
const MathLib::bigint num2 = MathLib::toBigNumber(tok2->strAt(4));
173+
const MathLib::bigint num2 = MathLib::toBigNumber(tok2->tokAt(4));
174174
if (0 == (num & num2))
175175
mismatchingBitAndError(assignTok, num, tok2, num2);
176176
}
177177
if (Token::Match(tok2, "%varid% =", varid)) {
178178
return true;
179179
}
180180
if (bitop == '&' && Token::Match(tok2, "%varid% &= %num% ;", varid)) {
181-
const MathLib::bigint num2 = MathLib::toBigNumber(tok2->strAt(2));
181+
const MathLib::bigint num2 = MathLib::toBigNumber(tok2->tokAt(2));
182182
if (0 == (num & num2))
183183
mismatchingBitAndError(assignTok, num, tok2, num2);
184184
}
@@ -213,7 +213,7 @@ bool CheckCondition::assignIfParseScope(const Token * const assignTok,
213213
}
214214
if (Token::Match(tok2,"&&|%oror%|( %varid% ==|!= %num% &&|%oror%|)", varid)) {
215215
const Token *vartok = tok2->next();
216-
const MathLib::bigint num2 = MathLib::toBigNumber(vartok->strAt(2));
216+
const MathLib::bigint num2 = MathLib::toBigNumber(vartok->tokAt(2));
217217
if ((num & num2) != ((bitop=='&') ? num2 : num)) {
218218
const std::string& op(vartok->strAt(1));
219219
const bool alwaysTrue = op == "!=";
@@ -267,11 +267,11 @@ void CheckCondition::mismatchingBitAndError(const Token *tok1, const MathLib::bi
267267
static void getnumchildren(const Token *tok, std::list<MathLib::bigint> &numchildren)
268268
{
269269
if (tok->astOperand1() && tok->astOperand1()->isNumber())
270-
numchildren.push_back(MathLib::toBigNumber(tok->astOperand1()->str()));
270+
numchildren.push_back(MathLib::toBigNumber(tok->astOperand1()));
271271
else if (tok->astOperand1() && tok->str() == tok->astOperand1()->str())
272272
getnumchildren(tok->astOperand1(), numchildren);
273273
if (tok->astOperand2() && tok->astOperand2()->isNumber())
274-
numchildren.push_back(MathLib::toBigNumber(tok->astOperand2()->str()));
274+
numchildren.push_back(MathLib::toBigNumber(tok->astOperand2()));
275275
else if (tok->astOperand2() && tok->str() == tok->astOperand2()->str())
276276
getnumchildren(tok->astOperand2(), numchildren);
277277
}
@@ -464,8 +464,8 @@ bool CheckCondition::isOverlappingCond(const Token * const cond1, const Token *
464464
if (!isSameExpression(true, expr1, expr2, *mSettings, pure, false))
465465
return false;
466466

467-
const MathLib::bigint value1 = MathLib::toBigNumber(num1->str());
468-
const MathLib::bigint value2 = MathLib::toBigNumber(num2->str());
467+
const MathLib::bigint value1 = MathLib::toBigNumber(num1);
468+
const MathLib::bigint value2 = MathLib::toBigNumber(num2);
469469
if (cond2->str() == "&")
470470
return ((value1 & value2) == value2);
471471
return ((value1 & value2) > 0);
@@ -1267,14 +1267,14 @@ void CheckCondition::checkIncorrectLogicOperator()
12671267
if (isfloat && (op1 == "==" || op1 == "!=" || op2 == "==" || op2 == "!="))
12681268
continue;
12691269

1270-
1270+
// the expr are not the token of the value but they provide better context
12711271
const double d1 = (isfloat) ? MathLib::toDoubleNumber(value1) : 0;
12721272
const double d2 = (isfloat) ? MathLib::toDoubleNumber(value2) : 0;
1273-
const MathLib::bigint i1 = (isfloat) ? 0 : MathLib::toBigNumber(value1);
1274-
const MathLib::bigint i2 = (isfloat) ? 0 : MathLib::toBigNumber(value2);
1273+
const MathLib::bigint i1 = (isfloat) ? 0 : MathLib::toBigNumber(value1, expr1);
1274+
const MathLib::bigint i2 = (isfloat) ? 0 : MathLib::toBigNumber(value2, expr2);
12751275
const bool useUnsignedInt = (std::numeric_limits<MathLib::bigint>::max()==i1) || (std::numeric_limits<MathLib::bigint>::max()==i2);
1276-
const MathLib::biguint u1 = (useUnsignedInt) ? MathLib::toBigUNumber(value1) : 0;
1277-
const MathLib::biguint u2 = (useUnsignedInt) ? MathLib::toBigUNumber(value2) : 0;
1276+
const MathLib::biguint u1 = (useUnsignedInt) ? MathLib::toBigUNumber(value1, expr1) : 0;
1277+
const MathLib::biguint u2 = (useUnsignedInt) ? MathLib::toBigUNumber(value2, expr2) : 0;
12781278
// evaluate if expression is always true/false
12791279
bool alwaysTrue = true, alwaysFalse = true;
12801280
bool firstTrue = true, secondTrue = true;

lib/checkfunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ void CheckFunctions::memsetInvalid2ndParam()
582582
}
583583

584584
if (printWarning && secondParamTok->isNumber()) { // Check if the second parameter is a literal and is out of range
585-
const MathLib::bigint value = MathLib::toBigNumber(secondParamTok->str());
585+
const MathLib::bigint value = MathLib::toBigNumber(secondParamTok);
586586
const long long sCharMin = mSettings->platform.signedCharMin();
587587
const long long uCharMax = mSettings->platform.unsignedCharMax();
588588
if (value < sCharMin || value > uCharMax)

lib/checkleakautovar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
474474

475475
// Assigning non-zero value variable. It might be used to
476476
// track the execution for a later if condition.
477-
if (Token::Match(varTok->tokAt(2), "%num% ;") && MathLib::toBigNumber(varTok->strAt(2)) != 0)
477+
if (Token::Match(varTok->tokAt(2), "%num% ;") && MathLib::toBigNumber(varTok->tokAt(2)) != 0)
478478
notzero.insert(varTok->varId());
479479
else if (Token::Match(varTok->tokAt(2), "- %type% ;") && varTok->tokAt(3)->isUpperCaseName())
480480
notzero.insert(varTok->varId());

lib/checkother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3216,7 +3216,7 @@ void CheckOther::checkIncompleteArrayFill()
32163216
if (!var || !var->isArray() || var->dimensions().empty() || !var->dimension(0))
32173217
continue;
32183218

3219-
if (MathLib::toBigNumber(tok->linkAt(1)->strAt(-1)) == var->dimension(0)) {
3219+
if (MathLib::toBigNumber(tok->linkAt(1)->tokAt(-1)) == var->dimension(0)) {
32203220
int size = mTokenizer->sizeOfType(var->typeStartToken());
32213221
if (size == 0 && var->valueType()->pointer)
32223222
size = mSettings->platform.sizeof_pointer;

lib/checkstring.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ void CheckString::checkIncorrectStringCompare()
294294
tok = tok->linkAt(1);
295295

296296
if (Token::simpleMatch(tok, ". substr (") && Token::Match(tok->tokAt(3)->nextArgument(), "%num% )")) {
297-
const MathLib::biguint clen = MathLib::toBigUNumber(tok->linkAt(2)->strAt(-1));
297+
const MathLib::biguint clen = MathLib::toBigUNumber(tok->linkAt(2)->tokAt(-1));
298298
const Token* begin = tok->previous();
299299
for (;;) { // Find start of statement
300300
while (begin->link() && Token::Match(begin, "]|)|>"))

lib/checkuninitvar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,9 @@ static void conditionAlwaysTrueOrFalse(const Token *tok, const std::map<nonneg i
332332
return;
333333

334334
if (tok->str() == "==")
335-
*alwaysTrue = (it->second == MathLib::toBigNumber(numtok->str()));
335+
*alwaysTrue = (it->second == MathLib::toBigNumber(numtok));
336336
else if (tok->str() == "!=")
337-
*alwaysTrue = (it->second != MathLib::toBigNumber(numtok->str()));
337+
*alwaysTrue = (it->second != MathLib::toBigNumber(numtok));
338338
else
339339
return;
340340
*alwaysFalse = !(*alwaysTrue);

lib/library.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,13 +1059,13 @@ bool Library::isIntArgValid(const Token *ftok, int argnr, const MathLib::bigint
10591059
TokenList tokenList(nullptr);
10601060
gettokenlistfromvalid(ac->valid, ftok->isCpp(), tokenList);
10611061
for (const Token *tok = tokenList.front(); tok; tok = tok->next()) {
1062-
if (tok->isNumber() && argvalue == MathLib::toBigNumber(tok->str()))
1062+
if (tok->isNumber() && argvalue == MathLib::toBigNumber(tok))
10631063
return true;
1064-
if (Token::Match(tok, "%num% : %num%") && argvalue >= MathLib::toBigNumber(tok->str()) && argvalue <= MathLib::toBigNumber(tok->strAt(2)))
1064+
if (Token::Match(tok, "%num% : %num%") && argvalue >= MathLib::toBigNumber(tok) && argvalue <= MathLib::toBigNumber(tok->tokAt(2)))
10651065
return true;
1066-
if (Token::Match(tok, "%num% : ,") && argvalue >= MathLib::toBigNumber(tok->str()))
1066+
if (Token::Match(tok, "%num% : ,") && argvalue >= MathLib::toBigNumber(tok))
10671067
return true;
1068-
if ((!tok->previous() || tok->strAt(-1) == ",") && Token::Match(tok,": %num%") && argvalue <= MathLib::toBigNumber(tok->strAt(1)))
1068+
if ((!tok->previous() || tok->strAt(-1) == ",") && Token::Match(tok,": %num%") && argvalue <= MathLib::toBigNumber(tok->tokAt(1)))
10691069
return true;
10701070
}
10711071
return false;
@@ -1079,11 +1079,11 @@ bool Library::isFloatArgValid(const Token *ftok, int argnr, double argvalue) con
10791079
TokenList tokenList(nullptr);
10801080
gettokenlistfromvalid(ac->valid, ftok->isCpp(), tokenList);
10811081
for (const Token *tok = tokenList.front(); tok; tok = tok->next()) {
1082-
if (Token::Match(tok, "%num% : %num%") && argvalue >= MathLib::toDoubleNumber(tok->str()) && argvalue <= MathLib::toDoubleNumber(tok->strAt(2)))
1082+
if (Token::Match(tok, "%num% : %num%") && argvalue >= MathLib::toDoubleNumber(tok) && argvalue <= MathLib::toDoubleNumber(tok->tokAt(2)))
10831083
return true;
1084-
if (Token::Match(tok, "%num% : ,") && argvalue >= MathLib::toDoubleNumber(tok->str()))
1084+
if (Token::Match(tok, "%num% : ,") && argvalue >= MathLib::toDoubleNumber(tok))
10851085
return true;
1086-
if ((!tok->previous() || tok->strAt(-1) == ",") && Token::Match(tok,": %num%") && argvalue <= MathLib::toDoubleNumber(tok->strAt(1)))
1086+
if ((!tok->previous() || tok->strAt(-1) == ",") && Token::Match(tok,": %num%") && argvalue <= MathLib::toDoubleNumber(tok->tokAt(1)))
10871087
return true;
10881088
if (Token::Match(tok, "%num%") && MathLib::isFloat(tok->str()) && MathLib::isEqual(tok->str(), MathLib::toString(argvalue)))
10891089
return true;

0 commit comments

Comments
 (0)