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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ test/testsizeof.o: test/testsizeof.cpp lib/check.h lib/checkers.h lib/checkimpl.
test/teststandards.o: test/teststandards.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/utils.h test/fixture.h
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/teststandards.cpp

test/teststl.o: test/teststl.cpp lib/check.h lib/checkers.h lib/checkimpl.h lib/checkstl.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
test/teststl.o: test/teststl.cpp lib/check.h lib/checkers.h lib/checkimpl.h lib/checkstl.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/teststl.cpp

test/teststring.o: test/teststring.cpp lib/check.h lib/checkers.h lib/checkimpl.h lib/checkstring.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
Expand Down
8 changes: 5 additions & 3 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,16 @@ void CheckStlImpl::mismatchingContainerIteratorError(const Token* containerTok,
}

// Error message for bad iterator usage..
void CheckStlImpl::mismatchingContainersError(const Token* tok1, const Token* tok2)
void CheckStlImpl::mismatchingContainersError(const Token* tok1, const Token* tok2, ValueFlow::Value::LifetimeKind ltk)
{
const std::string expr1(tok1 ? tok1->expressionString() : std::string("v1"));
const std::string expr2(tok2 ? tok2->expressionString() : std::string("v2"));
const std::string dataStr = (ltk == ValueFlow::Value::LifetimeKind::Iterator) ? "Iterators" : (ltk == ValueFlow::Value::LifetimeKind::Address ? "Addresses" : "Pointers");
const std::string objectStr = (ltk == ValueFlow::Value::LifetimeKind::Iterator || ltk == ValueFlow::Value::LifetimeKind::Object) ? "containers '" : "objects '";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This treats LifetimeKind::Object as if it always meant a container, but Object is also the kind for a pointer to a member of any object. For example:

struct S { int x; int y; };
void f3(S s1, S s2) { std::sort(&s1.x, &s2.y); }

This now reports Pointers of different containers 's1' and 's2' are used together., and S is not a container. Choosing between "containers" and "objects" based on whether the token is actually a container (e.g. astIsContainer(tok1)) would fix this.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think this makes sense? If you can fix it please do.. otherwise this PR lgtm..

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there might be a ValueFlow issue/inconsistency, still need to check that.

reportError(tok1,
Severity::error,
"mismatchingContainers",
"Iterators of different containers '" + expr1 + "' and '" + expr2 + "' are used together.",
dataStr + " of different " + objectStr + expr1 + "' and '" + expr2 + "' are used together.",
CWE664,
Certainty::normal);
}
Expand Down Expand Up @@ -791,7 +793,7 @@ bool CheckStlImpl::checkIteratorPair(const Token* tok1, const Token* tok2)
if (val1.tokvalue->expressionString() == val2.tokvalue->expressionString())
iteratorsError(tok1, val1.tokvalue, val1.tokvalue->expressionString());
else
mismatchingContainersError(val1.tokvalue, val2.tokvalue);
mismatchingContainersError(val1.tokvalue, val2.tokvalue, val1.lifetimeKind);
return true;
}

Expand Down
7 changes: 2 additions & 5 deletions lib/checkstl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "config.h"
#include "errortypes.h"
#include "mathlib.h"
#include "vfvalue.h"

#include <cstdint>
#include <string>
Expand All @@ -37,10 +38,6 @@ class Token;
class Variable;
class ErrorLogger;
class Tokenizer;
namespace ValueFlow
{
class Value;
}

/// @addtogroup Checks
/// @{
Expand Down Expand Up @@ -214,7 +211,7 @@ class CPPCHECKLIB CheckStlImpl : public CheckImpl {
void iteratorsError(const Token* tok, const std::string& containerName1, const std::string& containerName2);
void iteratorsError(const Token* tok, const Token* containerTok, const std::string& containerName);
void mismatchingContainerIteratorError(const Token* containerTok, const Token* iterTok, const Token* containerTok2);
void mismatchingContainersError(const Token* tok1, const Token* tok2);
void mismatchingContainersError(const Token* tok1, const Token* tok2, ValueFlow::Value::LifetimeKind ltk = ValueFlow::Value::LifetimeKind::Iterator);
void mismatchingContainerExpressionError(const Token *tok1, const Token *tok2);
void sameIteratorExpressionError(const Token *tok);
void stlBoundariesError(const Token* tok);
Expand Down
22 changes: 18 additions & 4 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class TestStl : public TestFixture {
TEST_CASE(iterator29);
TEST_CASE(iterator30);
TEST_CASE(iterator31);
TEST_CASE(iterator32);
TEST_CASE(iteratorExpression);
TEST_CASE(iteratorSameExpression);
TEST_CASE(mismatchingContainerIterator);
Expand Down Expand Up @@ -1914,13 +1915,13 @@ class TestStl : public TestFixture {
check("void f(int a, int b) {\n"
" if (std::for_each(&a, &b + 1, [](auto) {})) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:22]: (error) Iterators of different containers 'a' and 'b' are used together. [mismatchingContainers]\n",
ASSERT_EQUALS("[test.cpp:2:22]: (error) Addresses of different objects 'a' and 'b' are used together. [mismatchingContainers]\n",
errout_str());

check("void f(int a, int b) {\n"
" if (std::for_each(&a, &b, [](auto) {})) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:22]: (error) Iterators of different containers 'a' and 'b' are used together. [mismatchingContainers]\n",
ASSERT_EQUALS("[test.cpp:2:22]: (error) Addresses of different objects 'a' and 'b' are used together. [mismatchingContainers]\n",
errout_str());

check("void f(int a) {\n"
Expand Down Expand Up @@ -2061,8 +2062,21 @@ class TestStl : public TestFixture {
" std::string b;\n"
" return t.s.a.c_str() == b.c_str();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9:13]: (error) Iterators of different containers 's.a' and 'b' are used together. [mismatchingContainers]\n"
"[test.cpp:13:15]: (error) Iterators of different containers 't.s.a' and 'b' are used together. [mismatchingContainers]\n",
ASSERT_EQUALS("[test.cpp:9:13]: (error) Pointers of different containers 's.a' and 'b' are used together. [mismatchingContainers]\n"
"[test.cpp:13:15]: (error) Pointers of different containers 't.s.a' and 'b' are used together. [mismatchingContainers]\n",
errout_str());
}

void iterator32()
{
check("bool f(const std::string& a, const std::string& b) {\n" // #15158
" return a.data() != b.data();\n"
"}\n"
"bool g(const std::unique_ptr<int>& a, const std::unique_ptr<int>& b) {\n"
" return a.get() != b.get();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:12]: (error) Pointers of different containers 'a' and 'b' are used together. [mismatchingContainers]\n"
"[test.cpp:5:12]: (error) Pointers of different objects 'a' and 'b' are used together. [mismatchingContainers]\n",
errout_str());
}

Expand Down
Loading