diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 4c5297f54..433f36f6b 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -72,6 +72,8 @@ void CheckStl::outOfBounds() outOfBoundsError(tok, &value, nullptr); continue; } + if (!container->arrayLike_indexOp && !container->stdStringLike) + continue; if (value.intvalue == 0 && Token::Match(tok, "%name% [")) { outOfBoundsError(tok, &value, nullptr); continue; diff --git a/test/teststl.cpp b/test/teststl.cpp index 798512953..097c686f6 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -216,6 +216,20 @@ private: " v[i] = 0;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + checkNormal("void f(std::map x) {\n" + " if (x.empty()) { x[1] = 2; }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + checkNormal("void f(std::string s) {\n" + " if (s.size() == 1) {\n" + " s[2] = 0;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("test.cpp:3:warning:Possible access out of bounds of container 's'; size=1, index=2\n" + "test.cpp:2:note:condition 's.size()==1'\n" + "test.cpp:3:note:Access out of bounds\n", errout.str()); } void iterator1() {