From a0f51d1e21358245c256c7b9a28843e03214d84d Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 26 Jan 2023 22:18:55 +0100 Subject: [PATCH] Improve error message (#4735) --- lib/symboldatabase.cpp | 5 ++++- test/testbufferoverrun.cpp | 13 +++++++++++++ test/testunusedvar.cpp | 5 +++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 713f979b9..4b59bf97f 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2280,8 +2280,11 @@ std::string Variable::getTypeName() const { std::string ret; // TODO: For known types, generate the full type name - for (const Token *typeTok = mTypeStartToken; Token::Match(typeTok, "%name%|::") && typeTok->varId() == 0; typeTok = typeTok->next()) + for (const Token *typeTok = mTypeStartToken; Token::Match(typeTok, "%name%|::") && typeTok->varId() == 0; typeTok = typeTok->next()) { ret += typeTok->str(); + if (Token::simpleMatch(typeTok->next(), "<") && typeTok->next()->link()) // skip template arguments + typeTok = typeTok->next()->link(); + } return ret; } diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 074182749..80ef1fa32 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -207,6 +207,7 @@ private: TEST_CASE(array_index_negative4); TEST_CASE(array_index_negative5); // #10526 TEST_CASE(array_index_negative6); // #11349 + TEST_CASE(array_index_negative7); // #5685 TEST_CASE(array_index_for_decr); TEST_CASE(array_index_varnames); // FP: struct member #1576, FN: #1586 TEST_CASE(array_index_for_continue); // for,continue @@ -2271,6 +2272,18 @@ private: ASSERT_EQUALS("", errout.str()); } + // #5685 + void array_index_negative7() + { + check("void f() {\n" + " int i = -9;\n" + " int a[5];\n" + " for (; i < 5; i++)\n" + " a[i] = 1;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (error) Array 'a[5]' accessed at index -9, which is out of bounds.\n", errout.str()); + } + void array_index_for_decr() { check("void f()\n" "{\n" diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index af675bcfa..5f3cc2743 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -6270,6 +6270,11 @@ private: " myManager.theDummyTable.addRow(UnsignedIndexValue{ myNewValue }, DummyRowData{ false });\n" "}"); ASSERT_EQUALS("", errout.str()); + + functionVariableUsage("void f() {\n" + " std::list>::value_type a{ 1, 2, 3, 4 };\n" + "}\n"); + TODO_ASSERT_EQUALS("", "[test.cpp:2]: (information) --check-library: Provide configuration for std::list::value_type\n", errout.str()); } void localvarRangeBasedFor() {