Skip to content

Commit fec2bf8

Browse files
committed
CheckIO: make sure ArgumentInfo::tempToken is sufficiently initialized
1 parent faaabb1 commit fec2bf8

3 files changed

Lines changed: 24 additions & 20 deletions

File tree

lib/checkio.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings,
13581358
top = top->astParent();
13591359
const ValueType *valuetype = top->argumentType();
13601360
if (valuetype && valuetype->type >= ValueType::Type::BOOL) {
1361-
typeToken = tempToken = new Token();
1361+
typeToken = tempToken = new Token(top);
13621362
if (valuetype->pointer && valuetype->constness & 1) {
13631363
tempToken->str("const");
13641364
tempToken->insertToken("a");
@@ -1440,9 +1440,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings,
14401440
if (function->retType->classScope->enumType)
14411441
typeToken = function->retType->classScope->enumType;
14421442
else {
1443-
tempToken = new Token();
1444-
tempToken->fileIndex(tok1->fileIndex());
1445-
tempToken->linenr(tok1->linenr());
1443+
tempToken = new Token(tok1);
14461444
tempToken->str("int");
14471445
typeToken = tempToken;
14481446
}
@@ -1461,9 +1459,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings,
14611459
if (function->retType->classScope->enumType)
14621460
typeToken = function->retType->classScope->enumType;
14631461
else {
1464-
tempToken = new Token();
1465-
tempToken->fileIndex(tok1->fileIndex());
1466-
tempToken->linenr(tok1->linenr());
1462+
tempToken = new Token(tok1);
14671463
tempToken->str("int");
14681464
typeToken = tempToken;
14691465
}
@@ -1487,9 +1483,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings,
14871483
// check for some common well known functions
14881484
else if (isCPP && ((Token::Match(tok1->previous(), "%var% . size|empty|c_str ( ) [,)]") && isStdContainer(tok1->previous())) ||
14891485
(Token::Match(tok1->previous(), "] . size|empty|c_str ( ) [,)]") && isStdContainer(tok1->previous()->link()->previous())))) {
1490-
tempToken = new Token();
1491-
tempToken->fileIndex(tok1->fileIndex());
1492-
tempToken->linenr(tok1->linenr());
1486+
tempToken = new Token(tok1);
14931487
if (tok1->next()->str() == "size") {
14941488
// size_t is platform dependent
14951489
if (settings->platform.sizeof_size_t == 8) {
@@ -1548,9 +1542,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings,
15481542
if (variableInfo->type() && variableInfo->type()->classScope && variableInfo->type()->classScope->enumType)
15491543
typeToken = variableInfo->type()->classScope->enumType;
15501544
else {
1551-
tempToken = new Token();
1552-
tempToken->fileIndex(tok1->fileIndex());
1553-
tempToken->linenr(tok1->linenr());
1545+
tempToken = new Token(tok1);
15541546
tempToken->str("int");
15551547
typeToken = tempToken;
15561548
}
@@ -1588,9 +1580,7 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString()
15881580
return true;
15891581
}
15901582
if (variableInfo->isStlType(stl_string)) {
1591-
tempToken = new Token();
1592-
tempToken->fileIndex(variableInfo->typeStartToken()->fileIndex());
1593-
tempToken->linenr(variableInfo->typeStartToken()->linenr());
1583+
tempToken = new Token(variableInfo->typeStartToken());
15941584
if (variableInfo->typeStartToken()->strAt(2) == "string")
15951585
tempToken->str("char");
15961586
else
@@ -1608,9 +1598,7 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString()
16081598
return true;
16091599
}
16101600
if (Token::Match(nameTok, "std :: string|wstring")) {
1611-
tempToken = new Token();
1612-
tempToken->fileIndex(variableInfo->typeStartToken()->fileIndex());
1613-
tempToken->linenr(variableInfo->typeStartToken()->linenr());
1601+
tempToken = new Token(variableInfo->typeStartToken());
16141602
if (nameTok->strAt(2) == "string")
16151603
tempToken->str("char");
16161604
else

lib/token.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,23 @@ namespace {
5757

5858
const std::list<ValueFlow::Value> TokenImpl::mEmptyValueList;
5959

60+
Token::Token()
61+
: Token(static_cast<TokensFrontBack*>(nullptr))
62+
{}
63+
6064
Token::Token(TokensFrontBack *tokensFrontBack) :
6165
mTokensFrontBack(tokensFrontBack)
6266
{
6367
mImpl = new TokenImpl();
6468
}
6569

70+
Token::Token(const Token* tok)
71+
: Token(const_cast<Token*>(tok)->mTokensFrontBack)
72+
{
73+
fileIndex(tok->fileIndex());
74+
linenr(tok->linenr());
75+
}
76+
6677
Token::~Token()
6778
{
6879
delete mImpl;

lib/token.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ class CPPCHECKLIB Token {
151151
friend class TestToken;
152152

153153
private:
154+
// for usage in TestToken only
155+
Token();
156+
154157
TokensFrontBack* mTokensFrontBack{};
155158

156159
public:
@@ -168,7 +171,9 @@ class CPPCHECKLIB Token {
168171
eNone
169172
};
170173

171-
explicit Token(TokensFrontBack *tokensFrontBack = nullptr);
174+
explicit Token(TokensFrontBack *tokensFrontBack);
175+
// for usage in CheckIO::ArgumentInfo only
176+
explicit Token(const Token *tok);
172177
~Token();
173178

174179
ConstTokenRange until(const Token * t) const;

0 commit comments

Comments
 (0)