Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cfg/std.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9106,7 +9106,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<container id="stdBasicString" startPattern="std :: basic_string &lt;" inherits="stdAllString">
<type templateParameter="0"/>
</container>
<container id="stdString" startPattern="std :: string|wstring|u16string|u32string" endPattern="" inherits="stdAllString"/>
<container id="stdString" startPattern="std :: string|wstring|u16string|u32string" endPattern="" itEndPattern=":: iterator|const_iterator|reverse_iterator|const_reverse_iterator" inherits="stdAllString"/>
<container id="stdAllStringView" inherits="stdAllString" view="true">
<size>
<function name="remove_prefix" action="change"/>
Expand All @@ -9126,10 +9126,10 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<function name="end" yields="end-iterator"/>
</access>
</container>
<container id="stdStringView" startPattern="std :: string_view|wstring_view|u16string_view|u32string_view" endPattern="" inherits="stdAllStringView"/>
<container id="stdExperimentalStringView" startPattern="std :: experimental :: string_view|wstring_view|u16string_view|u32string_view" endPattern="" inherits="stdAllStringView"/>
<container id="stdStringView" startPattern="std :: string_view|wstring_view|u16string_view|u32string_view" endPattern="" itEndPattern=":: iterator|const_iterator|reverse_iterator|const_reverse_iterator" inherits="stdAllStringView"/>
<container id="stdExperimentalStringView" startPattern="std :: experimental :: string_view|wstring_view|u16string_view|u32string_view" endPattern="" itEndPattern=":: iterator|const_iterator|reverse_iterator|const_reverse_iterator" inherits="stdAllStringView"/>
<container id="stdExperimentalBasicStringView" startPattern="std :: experimental :: basic_string_view &lt;" inherits="stdBasicStringView" />
<container id="stdSpan" startPattern="std :: span" endPattern="" inherits="stdContainer" view="true">
<container id="stdSpan" startPattern="std :: span" endPattern="" itEndPattern=":: iterator|const_iterator|reverse_iterator|const_reverse_iterator" inherits="stdContainer" view="true">
<access indexOperator="array-like">
<function name="front" yields="item"/>
<function name="back" yields="item"/>
Expand Down
26 changes: 25 additions & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,23 @@ bool astIsUniqueSmartPointer(const Token* tok)
return tok->valueType()->smartPointer->unique;
}

const Library::Container* getSmartPointerContainer(const Token* tok, const Settings& settings)
{
if (!astIsSmartPointer(tok) || tok->valueType()->pointer != 0)
return nullptr;
const ValueType pointee = ValueType::parseDecl(tok->valueType()->smartPointerTypeToken, settings);
if (pointee.type != ValueType::Type::CONTAINER || pointee.pointer != 0)
return nullptr;
return pointee.container;
}

bool astIsSmartPointerContainerAccess(const Token* tok, const Settings& settings)
{
if (!Token::simpleMatch(tok ? tok->astParent() : nullptr, ".") || tok->astParent()->originalName() != "->" || !astIsLHS(tok))
return false;
return getSmartPointerContainer(tok, settings) != nullptr;
}

bool astIsIterator(const Token *tok)
{
return tok && tok->valueType() && tok->valueType()->type == ValueType::Type::ITERATOR;
Expand Down Expand Up @@ -292,8 +309,15 @@ bool astIsContainerString(const Token* tok)
static std::pair<const Token*, const Library::Container*> getContainerFunction(const Token* tok, const Library& library)
{
const Library::Container* cont{};
if (!tok || !tok->valueType() || (!tok->valueType()->container && (!(cont = library.detectContainerOrIterator(tok->valueType()->smartPointerTypeToken)))))
if (!tok || !tok->valueType())
return {};
if (!tok->valueType()->container) {
const Token* typeTok = tok->valueType()->smartPointerTypeToken;
while (Token::Match(typeTok, "const|volatile"))
typeTok = typeTok->next();
if (!(cont = library.detectContainerOrIterator(typeTok)))
return {};
}
const Token* parent = tok->astParent();
if (Token::Match(parent, ". %name% [(<]") && astIsLHS(tok)) {
return { parent->next(), cont ? cont : tok->valueType()->container };
Expand Down
5 changes: 5 additions & 0 deletions lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ bool astIsPointer(const Token *tok);
bool astIsSmartPointer(const Token* tok);
bool astIsUniqueSmartPointer(const Token* tok);

/** If tok is a smart pointer to a library container, get that container (e.g. std::vector for std::shared_ptr<std::vector<int>>) */
const Library::Container* getSmartPointerContainer(const Token* tok, const Settings& settings);
/** Is tok a smart pointer to a library container whose member is accessed with '->' (e.g. 'p' in 'p->begin()') */
bool astIsSmartPointerContainerAccess(const Token* tok, const Settings& settings);

bool astIsIterator(const Token *tok);

bool astIsContainer(const Token *tok);
Expand Down
4 changes: 4 additions & 0 deletions lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ void CheckAutoVariablesImpl::checkVarLifetimeScope(const Token * start, const To
continue;
if (!printInconclusive && val.isInconclusive())
continue;
// Iterator to a container owned by a smart pointer. The container might be shared or moved,
// so its lifetime is not bound to the smart pointer variable.
if (val.lifetimeKind == ValueFlow::Value::LifetimeKind::Iterator && astIsSmartPointer(val.tokvalue))
continue;
const Token* parent = getParentLifetime(val.tokvalue, mSettings.library);
if (!exprs.insert(parent).second)
continue;
Expand Down
100 changes: 91 additions & 9 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ void CheckStlImpl::iterators()
break;
bool inconclusiveType2=false;
if (isIterator(par2->variable(), inconclusiveType2))
break; // TODO: check if iterator points at same container
break; // iterator range insert(first, last): mismatchingContainers() checks that both iterators refer to the same container
if (par2->str() == "(")
par2 = par2->link();
par2 = par2->next();
Expand Down Expand Up @@ -771,6 +771,21 @@ static bool sizeValueAppliesToIterator(const ValueFlow::Value& sizeValue, const
(iterValue.container->exprId() != 0 && iterValue.container->exprId() == sizeValue.container->exprId());
}

// Is the lifetime an iterator to a container that is owned by a smart pointer?
static bool isSmartPointerIteratorLifetime(const ValueFlow::Value& val)
{
return val.lifetimeKind == ValueFlow::Value::LifetimeKind::Iterator && astIsSmartPointer(val.tokvalue);
}

// Iterators to containers owned by smart pointers. Different unique pointers can't own the same
// container, but a shared pointer or a reference to the container (auto& c = *p) can refer to it.
static bool mayBeSameSmartPointerContainer(const ValueFlow::Value& val1, const ValueFlow::Value& val2)
{
if (!isSmartPointerIteratorLifetime(val1) && !isSmartPointerIteratorLifetime(val2))
return false;
return !astIsUniqueSmartPointer(val1.tokvalue) || !astIsUniqueSmartPointer(val2.tokvalue);
}

bool CheckStlImpl::checkIteratorPair(const Token* tok1, const Token* tok2)
{
if (!tok1)
Expand All @@ -779,7 +794,8 @@ bool CheckStlImpl::checkIteratorPair(const Token* tok1, const Token* tok2)
return false;
ValueFlow::Value val1 = getLifetimeIteratorValue(tok1);
ValueFlow::Value val2 = getLifetimeIteratorValue(tok2);
if (val1.tokvalue && val2.tokvalue && val1.lifetimeKind == val2.lifetimeKind) {
if (val1.tokvalue && val2.tokvalue && val1.lifetimeKind == val2.lifetimeKind &&
!mayBeSameSmartPointerContainer(val1, val2)) {
if (val1.lifetimeKind == ValueFlow::Value::LifetimeKind::Lambda)
return false;
if (tok1->astParent() == tok2->astParent() && Token::Match(tok1->astParent(), "%comp%|-")) {
Expand Down Expand Up @@ -816,6 +832,53 @@ bool CheckStlImpl::checkIteratorPair(const Token* tok1, const Token* tok2)
return false;
}

// Get the iterator range [first, last) that is passed to a member function of a container, e.g.
// 'c.insert(first, last)', 'c.insert(pos, first, last)', 'c.assign(first, last)', or 'p->insert(first, last)'
// when the container is owned by a smart pointer. Both iterators must refer to the same container.
static std::pair<const Token*, const Token*> getMemberIteratorRange(const Token* ftok, const std::vector<const Token*>& args, const Settings& settings)
{
if (!Token::simpleMatch(ftok->astParent(), ".") || ftok->astParent()->astOperand2() != ftok)
return {};
const Token* const contTok = ftok->astParent()->astOperand1();
const Library::Container* container = nullptr;
if (astIsContainer(contTok))
container = contTok->valueType()->container;
else if (astIsSmartPointerContainerAccess(contTok, settings)) // container owned by a smart pointer, e.g. 'p->insert(first, last)'
container = getSmartPointerContainer(contTok, settings);
if (!container)
return {};

std::size_t first;
if (ftok->str() == "insert") {
if (args.size() == 3) // insert(pos, first, last)
first = 1;
else if (args.size() == 2 && container->stdAssociativeLike) // insert(first, last) or insert(hint, value)
first = 0;
else
return {};
} else if (Token::Match(ftok, "assign|append") && args.size() == 2) { // assign(first, last)
first = 0;
} else if (ftok->str() == "replace" && args.size() == 4 && container->stdStringLike) { // replace(pos1, pos2, first, last)
first = 2;
} else {
return {};
}

const Token* const firstTok = args[first];
const Token* const lastTok = args[first + 1];
if (!astIsIterator(firstTok) || !astIsIterator(lastTok))
return {};

if (args.size() == 2 && ftok->str() == "insert") {
// The first argument is a hint if it is an iterator to the container itself.
// Then this is insert(hint, value) where value is an iterator stored in the container.
const ValueFlow::Value val = getLifetimeIteratorValue(firstTok);
if (!val.tokvalue || isSameIteratorContainerExpression(contTok, val.tokvalue, settings))
return {};
}
return {firstTok, lastTok};
}

namespace {
struct ArgIteratorInfo {
const Token* tok;
Expand Down Expand Up @@ -843,6 +906,11 @@ void CheckStlImpl::mismatchingContainers()
if (args.size() < 2)
continue;

// Iterator range passed to member function of container
const std::pair<const Token*, const Token*> range = getMemberIteratorRange(ftok, args, mSettings);
if (range.first && checkIteratorPair(range.first, range.second))
continue;

// Group args together by container
std::map<int, std::vector<ArgIteratorInfo>> containers;
for (size_t argnr = 1; argnr <= args.size(); ++argnr) {
Expand Down Expand Up @@ -925,6 +993,8 @@ void CheckStlImpl::mismatchingContainerIterator()
continue;
if (val.lifetimeKind != ValueFlow::Value::LifetimeKind::Iterator)
continue;
if (isSmartPointerIteratorLifetime(val)) // the smart pointer might point at the container
continue;
if (iterTok->str() == "*" && iterTok->astOperand1()->valueType() && iterTok->astOperand1()->valueType()->type == ValueType::ITERATOR)
continue;
if (isSameIteratorContainerExpression(tok, val.tokvalue, mSettings))
Expand All @@ -934,10 +1004,17 @@ void CheckStlImpl::mismatchingContainerIterator()
}
}

static const Token* getInvalidMethod(const Token* tok)
static const Token* getInvalidMethod(const Token* tok, const Settings& settings)
{
if (!astIsLHS(tok))
return nullptr;
// Container owned by a smart pointer, only member functions called with '->' change the container
const Library::Container* c = tok->valueType()->container;
if (!c) {
if (!astIsSmartPointerContainerAccess(tok, settings))
return nullptr;
c = getSmartPointerContainer(tok, settings);
}
if (Token::Match(tok->astParent(), ". assign|clear|swap"))
return tok->astParent()->next();
if (Token::Match(tok->astParent(), "%assign%"))
Expand All @@ -947,7 +1024,7 @@ static const Token* getInvalidMethod(const Token* tok)
ftok = tok->astParent()->next();
if (!ftok)
return nullptr;
if (const Library::Container * c = tok->valueType()->container) {
if (c) {
const Library::Container::Action action = c->getAction(ftok->str());
if (c->unstableErase) {
if (action == Library::Container::Action::ERASE)
Expand Down Expand Up @@ -1002,6 +1079,9 @@ namespace {
return result;
}
};
explicit InvalidContainerAnalyzer(const Settings& settings) : settings(settings) {}

const Settings& settings;
std::unordered_map<const Function*, Info> invalidMethods;

std::vector<Info::Reference> invalidatesContainer(const Token* tok) const {
Expand Down Expand Up @@ -1043,8 +1123,8 @@ namespace {
}
}
}
} else if (astIsContainer(tok)) {
const Token* ftok = getInvalidMethod(tok);
} else if (astIsContainer(tok) || astIsSmartPointerContainerAccess(tok, settings)) {
const Token* ftok = getInvalidMethod(tok, settings);
if (ftok) {
ErrorPath ep;
ep.emplace_front(ftok,
Expand Down Expand Up @@ -1139,7 +1219,7 @@ void CheckStlImpl::invalidContainer()
{
logChecker("CheckStl::invalidContainer");
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
InvalidContainerAnalyzer analyzer;
InvalidContainerAnalyzer analyzer(mSettings);
analyzer.analyze(symbolDatabase);
for (const Scope * scope : symbolDatabase->functionScopes) {
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
Expand Down Expand Up @@ -1171,7 +1251,7 @@ void CheckStlImpl::invalidContainer()
}
} else {
for (const InvalidContainerAnalyzer::Info::Reference& r : analyzer.invalidatesContainer(tok)) {
if (!astIsContainer(r.tok))
if (!astIsContainer(r.tok) && !getSmartPointerContainer(r.tok, mSettings))
continue;
std::set<nonneg int> skipVarIds;
// Skip if the variable is assigned to
Expand Down Expand Up @@ -1202,7 +1282,9 @@ void CheckStlImpl::invalidContainer()
// return false;
if (Token::Match(info.tok->astParent(), "%assign%") && astIsLHS(info.tok))
skipVarIds.insert(info.tok->varId());
if (info.tok->variable()->isReference() && !isVariableDecl(info.tok) &&
// References to a container owned by a smart pointer are not tracked, the
// reference refers to the container object that is not invalidated
if (info.tok->variable()->isReference() && !isVariableDecl(info.tok) && astIsContainer(r.tok) &&
reaches(info.tok->variable()->nameToken(), tok, nullptr)) {

if ((assignExpr && Token::Match(assignExpr->astOperand1()->previous(), "& %varid%", info.tok->varId()))) {
Expand Down
43 changes: 38 additions & 5 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
if (!endsWith(container.startPattern, '<'))
container.startPattern2 += " !!::";
container.startPatternHasStd = startsWith(container.startPattern2, "std :: ");
container.startPatternTokenCount = 1 + static_cast<int>(std::count(container.startPattern.cbegin(), container.startPattern.cend(), ' '));
}
const char* const endPattern = node->Attribute("endPattern");
if (endPattern)
Expand Down Expand Up @@ -1412,11 +1413,33 @@ const Library::Container* Library::detectContainerInternal(const Token* const ty

const int offset = (withoutStd && container.startPatternHasStd) ? 7 : 0;

// If endPattern is undefined, it will always match, but itEndPattern has to be defined.
if (detect != IteratorOnly && container.endPattern.empty()) {
if (!Token::Match(typeStart, container.startPattern2.c_str() + offset))
// If endPattern is undefined the container is a non-template container (e.g. std::string) or
// a container whose template arguments can be deduced (e.g. std::span).
// The iterator type is then matched with itEndPattern after the container type.
if (container.endPattern.empty()) {
if (!Token::Match(typeStart, container.startPattern.c_str() + offset))
continue;

// find the token after the container type (and its template arguments, if any)
const Token* endTok = typeStart->tokAt(container.startPatternTokenCount - (offset > 0 ? 3 : 1));
if (endTok && endTok->str() == "<" && endTok->link())
endTok = endTok->link();
endTok = endTok ? endTok->next() : nullptr;
if (endTok && endTok->str() == "<" && endTok->link())
endTok = endTok->link()->next();

if (Token::simpleMatch(endTok, "::")) {
// a type that is nested in the container, e.g. std::string::iterator or std::span<int>::size_type
if (detect != ContainerOnly && !container.itEndPattern.empty() && Token::Match(endTok, container.itEndPattern.c_str())) {
if (isIterator)
*isIterator = true;
return &container;
}
continue;
}

if (detect == IteratorOnly)
continue;
if (isIterator)
*isIterator = false;
return &container;
Expand Down Expand Up @@ -1674,8 +1697,18 @@ const std::string& Library::returnValueType(const Token *ftok) const
if (isNotLibraryFunction(ftok)) {
if (Token::simpleMatch(ftok->astParent(), ".") && ftok->astParent()->astOperand1()) {
const Token* contTok = ftok->astParent()->astOperand1();
if (contTok->valueType() && contTok->valueType()->container)
return contTok->valueType()->container->getReturnType(ftok->str());
if (const ValueType* vt = contTok->valueType()) {
if (vt->container)
return vt->container->getReturnType(ftok->str());
// member function of a container accessed through a smart pointer
if (ftok->astParent()->originalName() == "->" && vt->type == ValueType::Type::SMART_POINTER && vt->pointer == 0) {
const Token* typeTok = vt->smartPointerTypeToken;
while (Token::Match(typeTok, "const|volatile"))
typeTok = typeTok->next();
if (const Container* container = detectContainer(typeTok))
return container->getReturnType(ftok->str());
}
}
}
return mEmptyString;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ class CPPCHECKLIB Library {
int templateParameter; // TODO: use this
};
std::string startPattern, startPattern2, endPattern, itEndPattern;
/** Number of tokens matched by startPattern */
int startPatternTokenCount{};
std::map<std::string, Function> functions;
int type_templateArgNo = -1;
std::vector<RangeItemRecordTypeItem> rangeItemRecordType;
Expand Down
Loading
Loading