From f092779a4dce1f4eedd5978a04288cd93ad0f1ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 6 Nov 2011 11:26:28 +0100 Subject: [PATCH] Fixed #3282 (Invalid report that an array index is of type char.) --- lib/checkother.cpp | 7 +++++-- test/testcharvar.cpp | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 28317dd3e..ef4a14fe2 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1762,6 +1762,10 @@ void CheckOther::checkCharVariable() if (tok->str() == "*") tok = tok->next(); + const unsigned int varid = tok->varId(); + if (!varid) + continue; + // Check usage of char variable.. unsigned int indentlevel = 0; for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { @@ -1775,8 +1779,7 @@ void CheckOther::checkCharVariable() } if (!isPointer) { - std::string temp = "%var% [ " + tok->str() + " ]"; - if ((tok2->str() != ".") && Token::Match(tok2->next(), temp.c_str())) { + if ((tok2->str() != ".") && Token::Match(tok2->next(), "%var% [ %varid% ]", varid)) { charArrayIndexError(tok2->next()); break; } diff --git a/test/testcharvar.cpp b/test/testcharvar.cpp index e1d2571fc..908fe4779 100644 --- a/test/testcharvar.cpp +++ b/test/testcharvar.cpp @@ -33,7 +33,8 @@ private: void run() { - TEST_CASE(array_index); + TEST_CASE(array_index_1); + TEST_CASE(array_index_2); TEST_CASE(bitop1); TEST_CASE(bitop2); TEST_CASE(return1); @@ -60,7 +61,7 @@ private: checkOther.checkCharVariable(); } - void array_index() { + void array_index_1() { check("void foo()\n" "{\n" " unsigned char ch = 0x80;\n" @@ -95,6 +96,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void array_index_2() { + // #3282 - False positive + check("void foo(char i);\n" + "void bar(int i) {\n" + " const char *s = \"abcde\";\n" + " foo(s[i]);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } void bitop1() { check("void foo()\n"