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
50 changes: 49 additions & 1 deletion 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 @@ -816,6 +816,49 @@ 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)'. 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();
if (!astIsContainer(contTok) || !contTok->valueType()->container)
return {};
const Library::Container* const container = contTok->valueType()->container;

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 +886,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
29 changes: 26 additions & 3 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
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
4 changes: 3 additions & 1 deletion man/reference-cfg-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,9 @@ The size of the type is specified in bytes. Possible values for the "sign" attri

# `<container>`

A lot of C++ libraries, among those the STL itself, provide containers with very similar functionality. Libraries can be used to tell cppcheck about their behaviour. Each container needs a unique ID. It can optionally have a startPattern, which must be a valid Token::Match pattern and an endPattern that is compared to the linked token of the first token with such a link. The optional attribute "inherits" takes an ID from a previously defined container.
A lot of C++ libraries, among those the STL itself, provide containers with very similar functionality. Libraries can be used to tell cppcheck about their behaviour. Each container needs a unique ID. It can optionally have a startPattern, which must be a valid Token::Match pattern and an endPattern that is compared to the linked token of the first token with such a link. The itEndPattern is compared in the same way to detect iterators of the container (e.g. `> :: iterator|const_iterator`). The optional attribute "inherits" takes an ID from a previously defined container.

If the container is not a template (e.g. `std::string`) or if its template arguments can be deduced (e.g. `std::span`), set endPattern to an empty string. The itEndPattern is then compared to the tokens following the container type and its template arguments, if there are any (e.g. `:: iterator|const_iterator`).

The `hasInitializerListConstructor` attribute can be set when the container has a constructor taking an initializer list.

Expand Down
5 changes: 4 additions & 1 deletion releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ Infrastructure & dependencies:
and rule files work unchanged. On Windows the Visual Studio project now expects pcre2-8.lib and pcre2.h in externals.

Other:
-
- Iterators of non-template library containers (e.g. std::string::iterator, std::string_view::const_iterator, QString::iterator, wxString::iterator) are now detected as iterators.
Previously such variables had no value type, so several iterator checks (e.g. containerOutOfBounds, mismatchingContainerIterator) missed bugs involving them.
- Iterators of std::span and gsl::span (e.g. std::span<int>::iterator) are now detected as iterators instead of containers, and nested types such as std::span<int>::size_type are no longer treated as containers.
- mismatchingContainers now detects iterator ranges from different containers passed to container member functions, e.g. s.insert(a.begin(), b.end()), v.insert(v.end(), a.begin(), b.end()), v.assign(a.begin(), b.end()) and str.append(a.begin(), b.end()).
47 changes: 42 additions & 5 deletions test/testlibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,10 +994,11 @@ class TestLibrary : public TestFixture {
SimpleTokenizer var(*this);
ASSERT(var.tokenize("std::F::iterator I;\n"));
ASSERT(!library.detectContainer(var.tokens()));
TODO_ASSERT(library.detectIterator(var.tokens()));
ASSERT_EQUALS(&F, library.detectIterator(var.tokens()));
bool isIterator = false;
TODO_ASSERT_EQUALS(reinterpret_cast<intptr_t>(&F), 0, reinterpret_cast<intptr_t>(library.detectContainerOrIterator(var.tokens(), &isIterator)));
TODO_ASSERT(isIterator);
ASSERT_EQUALS(&F, library.detectContainerOrIterator(var.tokens(), &isIterator));
ASSERT(isIterator);
ASSERT(!library.detectContainerOrIterator(var.tokens(), nullptr, true));
}

{
Expand All @@ -1007,8 +1008,44 @@ class TestLibrary : public TestFixture {
ASSERT(!library.detectIterator(var.tokens()));
ASSERT(!library.detectContainerOrIterator(var.tokens()));
bool isIterator = false;
TODO_ASSERT_EQUALS(reinterpret_cast<intptr_t>(&F), 0, reinterpret_cast<intptr_t>(library.detectContainerOrIterator(var.tokens(), &isIterator, true)));
TODO_ASSERT(isIterator);
ASSERT_EQUALS(&F, library.detectContainerOrIterator(var.tokens(), &isIterator, true));
ASSERT(isIterator);
}

{
SimpleTokenizer var(*this);
ASSERT(var.tokenize("std::F<int> f;\n"));
ASSERT_EQUALS(&F, library.detectContainer(var.tokens()));
ASSERT(!library.detectIterator(var.tokens()));
bool isIterator = true;
ASSERT_EQUALS(&F, library.detectContainerOrIterator(var.tokens(), &isIterator));
ASSERT(!isIterator);
}

{
SimpleTokenizer var(*this);
ASSERT(var.tokenize("std::F<int>::iterator I;\n"));
ASSERT(!library.detectContainer(var.tokens()));
ASSERT_EQUALS(&F, library.detectIterator(var.tokens()));
bool isIterator = false;
ASSERT_EQUALS(&F, library.detectContainerOrIterator(var.tokens(), &isIterator));
ASSERT(isIterator);
}

{
SimpleTokenizer var(*this);
ASSERT(var.tokenize("std::F<int>::size_type s;\n"));
ASSERT(!library.detectContainer(var.tokens()));
ASSERT(!library.detectIterator(var.tokens()));
ASSERT(!library.detectContainerOrIterator(var.tokens()));
}

{
SimpleTokenizer var(*this);
ASSERT(var.tokenize("std::F::size_type s;\n"));
ASSERT(!library.detectContainer(var.tokens()));
ASSERT(!library.detectIterator(var.tokens()));
ASSERT(!library.detectContainerOrIterator(var.tokens()));
}
}

Expand Down
93 changes: 92 additions & 1 deletion test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,21 @@ class TestStl : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:4:6]: (error) Out of bounds access in expression 'it' because 'vec' is empty. [containerOutOfBounds]\n",
errout_str());

check("int f(std::span<int> s) {\n"
" std::span<int>::iterator it = s.end();\n"
" return *it;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:13]: (error) Dereference of an invalid iterator: it [derefInvalidIterator]\n", errout_str());

// iterators of non-template containers
check("void f() {\n"
" std::string s;\n"
" std::string::iterator it = s.begin();\n"
" *it = 'a';\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:6]: (error) Out of bounds access in expression 'it' because 's' is empty. [containerOutOfBounds]\n",
errout_str());
}

void outOfBoundsErrorPath() {
Expand Down Expand Up @@ -1338,7 +1353,74 @@ class TestStl : public TestFixture {
" std::set<int>::iterator it2 = ints2.end();\n"
" ints2.insert(it1, it2);\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Iterators of different containers are used together.\n", "", errout_str());
ASSERT_EQUALS("[test.cpp:4:35]: (error) Iterators of different containers 'ints1' and 'ints2' are used together. [mismatchingContainers]\n", errout_str());

check("void foo(const std::set<int> &ints1, std::set<int> &ints2)\n"
"{\n"
" ints2.insert(ints1.begin(), ints2.end());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:18]: (error) Iterators of different containers 'ints1' and 'ints2' are used together. [mismatchingContainers]\n", errout_str());

check("void foo(const std::map<int, int> &m1, const std::map<int, int> &m2, std::map<int, int> &m3)\n"
"{\n"
" m3.insert(m1.begin(), m2.end());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:15]: (error) Iterators of different containers 'm1' and 'm2' are used together. [mismatchingContainers]\n", errout_str());

// insert(pos, first, last)
check("void foo(const std::vector<int> &v1, const std::vector<int> &v2, std::vector<int> &v3)\n"
"{\n"
" v3.insert(v3.end(), v1.begin(), v2.end());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:25]: (error) Iterators of different containers 'v1' and 'v2' are used together. [mismatchingContainers]\n", errout_str());

check("void foo(const std::vector<int> &v1, std::vector<int> &v3)\n"
"{\n"
" v3.insert(v3.end(), v1.begin(), v1.end());\n"
"}\n");
ASSERT_EQUALS("", errout_str());

// assign(first, last), append(first, last)
check("void foo(const std::vector<int> &v1, const std::vector<int> &v2, std::vector<int> &v3)\n"
"{\n"
" v3.assign(v1.begin(), v2.end());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:15]: (error) Iterators of different containers 'v1' and 'v2' are used together. [mismatchingContainers]\n", errout_str());

check("void foo(const std::vector<int> &v1, std::vector<int> &v3)\n"
"{\n"
" v3.assign(v1.begin(), v1.end());\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("void foo(const std::string &s1, const std::string &s2, std::string &s3)\n"
"{\n"
" s3.append(s1.begin(), s2.end());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:15]: (error) Iterators of different containers 's1' and 's2' are used together. [mismatchingContainers]\n", errout_str());

// replace(first1, last1, first2, last2)
check("void foo(const std::string &s1, const std::string &s2, std::string &s3)\n"
"{\n"
" s3.replace(s3.begin(), s3.end(), s1.begin(), s2.end());\n"
" s3.replace(s3.begin(), s3.end(), s1.begin(), s1.end());\n"
" s3.replace(s3.begin(), s3.end(), 3, 'c');\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:38]: (error) Iterators of different containers 's1' and 's2' are used together. [mismatchingContainers]\n", errout_str());

// insert(hint, value) where the value is an iterator
check("void foo(std::set<std::vector<int>::iterator> &s, std::vector<int> &v)\n"
"{\n"
" s.insert(s.end(), v.begin());\n"
"}\n");
ASSERT_EQUALS("", errout_str());

// insert(pos, value) where the value is an iterator
check("void foo(std::vector<std::vector<int>::iterator> &s, std::vector<int> &v)\n"
"{\n"
" s.insert(s.end(), v.begin());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void iterator7() {
Expand Down Expand Up @@ -2292,6 +2374,15 @@ class TestStl : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:2:5]: (error) Iterator 'b.begin()' referring to container 'b' is used with container 'a'. [mismatchingContainerIterator]\n", errout_str());

// iterators of non-template containers
check("void f(std::string& a, std::string& b) {\n"
" std::string::iterator it = b.begin();\n"
" a.erase(it);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:5]: (error) Same iterator is used with different containers 'b' and 'a'. [iterators1]\n"
"[test.cpp:3:5]: (error) Iterator 'it' referring to container 'b' is used with container 'a'. [mismatchingContainerIterator]\n",
errout_str());

// #9973
check("void f() {\n"
" std::list<int> l1;\n"
Expand Down
Loading
Loading