Skip to content

Commit 8beb95e

Browse files
committed
Fix CheckClass::checkMemset for arrays of pointers.
1 parent 1b29a99 commit 8beb95e

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

lib/checkclass.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,28 +1010,30 @@ void CheckClass::checkMemset()
10101010
else if (Token::simpleMatch(arg3, "sizeof ( * this ) )") || Token::simpleMatch(arg1, "this ,")) {
10111011
type = findFunctionOf(arg3->scope());
10121012
} else if (Token::Match(arg1, "&|*|%var%")) {
1013-
int derefs = 1;
1013+
int numIndirToVariableType = 0; // Offset to the actual type in terms of dereference/addressof
10141014
for (;; arg1 = arg1->next()) {
10151015
if (arg1->str() == "&")
1016-
derefs--;
1016+
++numIndirToVariableType;
10171017
else if (arg1->str() == "*")
1018-
derefs++;
1018+
--numIndirToVariableType;
10191019
else
10201020
break;
10211021
}
10221022

10231023
const Variable *var = arg1->variable();
10241024
if (var && arg1->strAt(1) == ",") {
1025-
if (var->isPointer()) {
1026-
derefs--;
1027-
if (var->typeEndToken() && Token::simpleMatch(var->typeEndToken()->previous(), "* *")) // Check if it's a pointer to pointer
1028-
derefs--;
1025+
if (var->isArrayOrPointer()) {
1026+
const Token *endTok = var->typeEndToken();
1027+
while (endTok && Token::simpleMatch(endTok, "*")) {
1028+
--numIndirToVariableType;
1029+
endTok = endTok->previous();
1030+
}
10291031
}
10301032

10311033
if (var->isArray())
1032-
derefs -= (int)var->dimensions().size();
1034+
numIndirToVariableType += (int)var->dimensions().size();
10331035

1034-
if (derefs == 0)
1036+
if (numIndirToVariableType == 1)
10351037
type = var->typeScope();
10361038
}
10371039
}

test/testclass.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,10 +2581,7 @@ class TestClass : public TestFixture {
25812581
" memset(c2, 0, 10);\n"
25822582
" memset(c3, 0, 10);\n"
25832583
"}");
2584-
ASSERT_EQUALS("[test.cpp:9]: (error) Using 'memset' on struct that contains a 'std::string'.\n"
2585-
"[test.cpp:11]: (error) Using 'memset' on struct that contains a 'std::string'.\n"
2586-
"[test.cpp:12]: (error) Using 'memset' on struct that contains a 'std::string'.\n"
2587-
"[test.cpp:13]: (error) Using 'memset' on struct that contains a 'std::string'.\n", errout.str());
2584+
ASSERT_EQUALS("[test.cpp:12]: (error) Using 'memset' on struct that contains a 'std::string'.\n", errout.str());
25882585
}
25892586

25902587
void memsetOnStdPodType() { // Ticket #5901

0 commit comments

Comments
 (0)