Fixed #4565 (false positive: dangerous usage (not null-terminated))

This commit is contained in:
Mathias De Maré 2013-02-10 13:36:40 +01:00 committed by Daniel Marjamäki
parent e18888cd4d
commit dfd557888d
2 changed files with 15 additions and 1 deletions

View File

@ -277,8 +277,12 @@ private:
CheckUninitVar *checkUninitVar = dynamic_cast<CheckUninitVar *>(c->owner);
if (checkUninitVar) {
if (c->strncpy_ || c->memset_nonzero)
if (c->strncpy_ || c->memset_nonzero) {
if(!Token::Match(c->var->typeStartToken(), "char|wchar_t")) {
continue;
}
checkUninitVar->uninitstringError(tok, c->var->name(), c->strncpy_);
}
else if (c->var->isPointer() && !c->var->isArray() && c->alloc)
checkUninitVar->uninitdataError(tok, c->var->name());
else

View File

@ -45,6 +45,7 @@ private:
TEST_CASE(uninitvar_return); // return
TEST_CASE(uninitvar_strncpy); // strncpy doesn't always null-terminate
TEST_CASE(uninitvar_memset); // not null-terminated
TEST_CASE(uninitvar_memset_nonchar);
TEST_CASE(uninitvar_func); // analyse functions
TEST_CASE(func_uninit_var); // analyse function calls for: 'int a(int x) { return x+x; }'
TEST_CASE(func_uninit_pointer); // analyse function calls for: 'void a(int *p) { *p = 0; }'
@ -1638,6 +1639,15 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (error) Dangerous usage of 'a' (not null-terminated).\n", errout.str());
}
void uninitvar_memset_nonchar() {
checkUninitVar("void f() {\n"
" int a[20];\n"
" memset(a, 1, 20);\n"
" a[0] |= 2;\n"
"}\n");
ASSERT_EQUALS(errout.str(), "");
}
std::string analyseFunctions(const char code[]) {
// Clear the error buffer..
errout.str("");