Fixed #7582 (false positive: Division by result of sizeof(). strncpy() expects a size in bytes)

This commit is contained in:
Daniel Marjamäki 2016-07-17 15:22:14 +02:00
parent 7a183779e2
commit 91e38f3eb9
2 changed files with 8 additions and 0 deletions

View File

@ -157,6 +157,8 @@ void CheckSizeof::checkSizeofForPointerSize()
if (tokFunc && tokSize) { if (tokFunc && tokSize) {
for (const Token* tok2 = tokSize; tok2 != tokFunc->linkAt(1); tok2 = tok2->next()) { for (const Token* tok2 = tokSize; tok2 != tokFunc->linkAt(1); tok2 = tok2->next()) {
if (tok2->str() == "sizeof")
break;
if (Token::simpleMatch(tok2, "/ sizeof")) if (Token::simpleMatch(tok2, "/ sizeof"))
divideBySizeofError(tok2, tokFunc->str()); divideBySizeofError(tok2, tokFunc->str());
} }

View File

@ -623,6 +623,12 @@ private:
" return malloc(num / sizeof(Foo));\n" " return malloc(num / sizeof(Foo));\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Division by result of sizeof(). malloc() expects a size in bytes, did you intend to multiply instead?\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (warning) Division by result of sizeof(). malloc() expects a size in bytes, did you intend to multiply instead?\n", errout.str());
check("void f() {\n"
" char str[100];\n"
" strncpy(str, xyz, sizeof(str)/sizeof(str[0]));\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void sizeofVoid() { void sizeofVoid() {