From 20121b34d856efb59f60e7161417a2bffa63355c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Oct 2018 06:53:26 +0200 Subject: [PATCH] Fixed #7718 (False positive: out of bounds of already resized std::string) --- lib/checkbufferoverrun.cpp | 3 +++ test/testbufferoverrun.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 849e150ff..ccd2fb3cf 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -1561,6 +1561,9 @@ void CheckBufferOverrun::bufferOverrun() // char arr[10] = "123"; // arr[7] = 'x'; // warning: arr[7] is inside the array bounds, but past the string's end + if (tok->valueType() && tok->valueType()->type == ValueType::Type::CONTAINER) + continue; + const ArrayInfo arrayInfo(tok->varId(), varname, 1U, Token::getStrSize(strtoken)); valueFlowCheckArrayIndex(tok->next(), arrayInfo); } else { diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 6d9bda351..0a0ddaafd 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -74,6 +74,8 @@ private: } void run() override { + LOAD_LIB_2(settings0.library, "std.cfg"); + settings0.addEnabled("warning"); settings0.addEnabled("style"); settings0.addEnabled("portability"); @@ -3124,6 +3126,14 @@ private: " s[10] = 0;\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void foo()\n" // #7718 + "{\n" + " std::string s = \"123\";\n" + " s.resize(100);\n" + " s[10] = 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); } // data is allocated with alloca