Skip to content

Commit 49f6231

Browse files
committed
Fixed two issues in CheckSizeof::checkSizeofForPointerSize()
1 parent 598809a commit 49f6231

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/checksizeof.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ void CheckSizeof::checkSizeofForPointerSize()
134134
} else if (Token::simpleMatch(tok, "return calloc (")) {
135135
tokSize = tok->tokAt(3)->nextArgument();
136136
tokFunc = tok->next();
137-
} else if (Token::simpleMatch(tok, "memset (")) {
137+
} else if (Token::simpleMatch(tok, "memset (") && tok->strAt(-1) != ".") {
138138
variable = tok->tokAt(2);
139139
tokSize = variable->nextArgument();
140140
if (tokSize)
141141
tokSize = tokSize->nextArgument();
142142
tokFunc = tok;
143-
} else if (Token::Match(tok, "memcpy|memcmp|memmove|strncpy|strncmp|strncat (")) {
143+
} else if (Token::Match(tok, "memcpy|memcmp|memmove|strncpy|strncmp|strncat (") && tok->strAt(-1) != ".") {
144144
variable = tok->tokAt(2);
145145
variable2 = variable->nextArgument();
146146
if (!variable2)
@@ -158,7 +158,7 @@ void CheckSizeof::checkSizeofForPointerSize()
158158
}
159159
}
160160

161-
if (!variable)
161+
if (!variable || !tokSize)
162162
continue;
163163

164164
while (Token::Match(variable, "%var% ::|."))
@@ -199,6 +199,9 @@ void CheckSizeof::checkSizeofForPointerSize()
199199
while (Token::Match(tokSize, "%var% ::|."))
200200
tokSize = tokSize->tokAt(2);
201201

202+
if (Token::Match(tokSize, "%var% [|("))
203+
continue;
204+
202205
// Now check for the sizeof usage. Once here, everything using sizeof(varid) or sizeof(&varid)
203206
// looks suspicious
204207
// Do it for first variable

test/testsizeof.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,19 @@ class TestSizeof : public TestFixture {
524524
" memset(pIntArray, 0, sizeof(pIntArray));\n"
525525
"}");
526526
ASSERT_EQUALS("", errout.str());
527+
528+
check("void FreeFileName(const char *s) {\n"
529+
" CxString tbuf;\n"
530+
" const char *p;\n"
531+
" memcpy(s, siezof(s));\n" // non-standard memcpy
532+
"}");
533+
ASSERT_EQUALS("", errout.str());
534+
535+
check("int f() {\n"
536+
" module_config_t *tab = module;\n"
537+
" memset(tab + confsize, 0, sizeof(tab[confsize]));\n"
538+
"}");
539+
ASSERT_EQUALS("", errout.str());
527540
}
528541

529542
void checkPointerSizeofStruct() {

0 commit comments

Comments
 (0)