Skip to content

Commit

Permalink
CheckIO: make sure ArgumentInfo::tempToken is sufficiently initiali…
Browse files Browse the repository at this point in the history
…zed (#5932)
  • Loading branch information
firewave authored Feb 8, 2024
1 parent ab4e462 commit 6ee2c86
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 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(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(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(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(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(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(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(variableInfo->typeStartToken());
if (nameTok->strAt(2) == "string")
tempToken->str("char");
else
Expand Down
11 changes: 11 additions & 0 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,23 @@ namespace {

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

Token::Token()
: Token(static_cast<TokensFrontBack*>(nullptr))
{}

Token::Token(TokensFrontBack *tokensFrontBack) :
mTokensFrontBack(tokensFrontBack)
{
mImpl = new TokenImpl();
}

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

Token::~Token()
{
delete mImpl;
Expand Down
7 changes: 6 additions & 1 deletion lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ class CPPCHECKLIB Token {
friend class TestToken;

private:
// for usage in TestToken only
Token();

TokensFrontBack* mTokensFrontBack{};

public:
Expand All @@ -168,7 +171,9 @@ class CPPCHECKLIB Token {
eNone
};

explicit Token(TokensFrontBack *tokensFrontBack = nullptr);
explicit Token(TokensFrontBack *tokensFrontBack);
// for usage in CheckIO::ArgumentInfo only
explicit Token(const Token *tok);
~Token();

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

0 comments on commit 6ee2c86

Please sign in to comment.