From 6ee2c86dc331cdec0dcf4195ac45503a39fba57c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Thu, 8 Feb 2024 16:22:20 +0100 Subject: [PATCH] CheckIO: make sure `ArgumentInfo::tempToken` is sufficiently initialized (#5932) --- lib/checkio.cpp | 26 +++++++------------------- lib/token.cpp | 11 +++++++++++ lib/token.h | 7 ++++++- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index a3805dc7ac3..e5556a3c99a 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -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"); @@ -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; } @@ -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; } @@ -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) { @@ -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; } @@ -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 @@ -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 diff --git a/lib/token.cpp b/lib/token.cpp index 445ac26ac22..313fb56c661 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -57,12 +57,23 @@ namespace { const std::list TokenImpl::mEmptyValueList; +Token::Token() + : Token(static_cast(nullptr)) +{} + Token::Token(TokensFrontBack *tokensFrontBack) : mTokensFrontBack(tokensFrontBack) { mImpl = new TokenImpl(); } +Token::Token(const Token* tok) + : Token(const_cast(tok)->mTokensFrontBack) +{ + fileIndex(tok->fileIndex()); + linenr(tok->linenr()); +} + Token::~Token() { delete mImpl; diff --git a/lib/token.h b/lib/token.h index 0578a903a93..a889416d1de 100644 --- a/lib/token.h +++ b/lib/token.h @@ -151,6 +151,9 @@ class CPPCHECKLIB Token { friend class TestToken; private: + // for usage in TestToken only + Token(); + TokensFrontBack* mTokensFrontBack{}; public: @@ -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;