Skip to content

Commit

Permalink
CheckIO: make sure ArgumentInfo::tempToken is sufficiently initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Jan 31, 2024
1 parent cc27df3 commit f973053
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 65 deletions.
26 changes: 7 additions & 19 deletions lib/checkio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings,
top = top->astParent();
const ValueType *valuetype = top->argumentType();
if (valuetype && valuetype->type >= ValueType::Type::BOOL) {
typeToken = tempToken = new Token();
typeToken = tempToken = new Token(const_cast<Token*>(top));
if (valuetype->pointer && valuetype->constness & 1) {
tempToken->str("const");
tempToken->insertToken("a");
Expand Down Expand Up @@ -1440,9 +1440,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings,
if (function->retType->classScope->enumType)
typeToken = function->retType->classScope->enumType;
else {
tempToken = new Token();
tempToken->fileIndex(tok1->fileIndex());
tempToken->linenr(tok1->linenr());
tempToken = new Token(const_cast<Token*>(tok1));
tempToken->str("int");
typeToken = tempToken;
}
Expand All @@ -1461,9 +1459,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings,
if (function->retType->classScope->enumType)
typeToken = function->retType->classScope->enumType;
else {
tempToken = new Token();
tempToken->fileIndex(tok1->fileIndex());
tempToken->linenr(tok1->linenr());
tempToken = new Token(const_cast<Token*>(tok1));
tempToken->str("int");
typeToken = tempToken;
}
Expand All @@ -1487,9 +1483,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings,
// check for some common well known functions
else if (isCPP && ((Token::Match(tok1->previous(), "%var% . size|empty|c_str ( ) [,)]") && isStdContainer(tok1->previous())) ||
(Token::Match(tok1->previous(), "] . size|empty|c_str ( ) [,)]") && isStdContainer(tok1->previous()->link()->previous())))) {
tempToken = new Token();
tempToken->fileIndex(tok1->fileIndex());
tempToken->linenr(tok1->linenr());
tempToken = new Token(const_cast<Token*>(tok1));
if (tok1->next()->str() == "size") {
// size_t is platform dependent
if (settings->platform.sizeof_size_t == 8) {
Expand Down Expand Up @@ -1548,9 +1542,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings *settings,
if (variableInfo->type() && variableInfo->type()->classScope && variableInfo->type()->classScope->enumType)
typeToken = variableInfo->type()->classScope->enumType;
else {
tempToken = new Token();
tempToken->fileIndex(tok1->fileIndex());
tempToken->linenr(tok1->linenr());
tempToken = new Token(const_cast<Token*>(tok1));
tempToken->str("int");
typeToken = tempToken;
}
Expand Down Expand Up @@ -1588,9 +1580,7 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString()
return true;
}
if (variableInfo->isStlType(stl_string)) {
tempToken = new Token();
tempToken->fileIndex(variableInfo->typeStartToken()->fileIndex());
tempToken->linenr(variableInfo->typeStartToken()->linenr());
tempToken = new Token(const_cast<Token*>(variableInfo->typeStartToken()));
if (variableInfo->typeStartToken()->strAt(2) == "string")
tempToken->str("char");
else
Expand All @@ -1608,9 +1598,7 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString()
return true;
}
if (Token::Match(nameTok, "std :: string|wstring")) {
tempToken = new Token();
tempToken->fileIndex(variableInfo->typeStartToken()->fileIndex());
tempToken->linenr(variableInfo->typeStartToken()->linenr());
tempToken = new Token(const_cast<Token*>(variableInfo->typeStartToken()));
if (nameTok->strAt(2) == "string")
tempToken->str("char");
else
Expand Down
7 changes: 7 additions & 0 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ Token::Token(TokensFrontBack *tokensFrontBack) :
mImpl = new TokenImpl();
}

Token::Token(Token* tok)
: Token(tok->mTokensFrontBack)
{
fileIndex(tok->fileIndex());
linenr(tok->linenr());
}

Token::~Token()
{
delete mImpl;
Expand Down
3 changes: 2 additions & 1 deletion lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ class CPPCHECKLIB Token {
eNone
};

explicit Token(TokensFrontBack *tokensFrontBack = nullptr);
explicit Token(TokensFrontBack *tokensFrontBack);
explicit Token(Token *tok);
~Token();

ConstTokenRange until(const Token * t) const;
Expand Down
Loading

0 comments on commit f973053

Please sign in to comment.