Fixed #5901 (False positive: (error) Using 'memcpy' with vector of uint8_t items)

This commit is contained in:
Daniel Marjamäki 2014-07-09 15:00:06 +02:00
parent 7b0616786c
commit cb9d67b9ec
2 changed files with 21 additions and 3 deletions

View File

@ -1073,7 +1073,7 @@ void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Sco
const Scope *typeScope = var->typeScope();
// check for std:: type
if (var->isStlType() && tok1->strAt(2) != "array")
if (var->isStlType() && tok1->strAt(2) != "array" && !_settings->library.podtype(tok1->strAt(2)))
if (allocation)
mallocOnClassError(tok, tok->str(), type->classDef, "'std::" + tok1->strAt(2) + "'");
else

View File

@ -84,6 +84,7 @@ private:
TEST_CASE(memsetVector);
TEST_CASE(memsetOnClass);
TEST_CASE(memsetOnInvalid); // Ticket #5425: Crash upon invalid
TEST_CASE(memsetOnStdPodType); // #5901 - std::uint8_t
TEST_CASE(mallocOnClass);
TEST_CASE(this_subtraction); // warn about "this-x"
@ -2123,12 +2124,15 @@ private:
ASSERT_EQUALS("", errout.str());
}
void checkNoMemset(const char code[]) {
void checkNoMemset(const char code[], bool load_std_cfg = false) {
// Clear the error log
errout.str("");
Settings settings;
settings.addEnabled("warning");
if (load_std_cfg) {
LOAD_LIB_2(settings.library, "std.cfg");
}
// Tokenize..
Tokenizer tokenizer(&settings, this);
@ -2565,10 +2569,24 @@ private:
"void f() {\n"
" A a;\n"
" memset(&a, 0, sizeof(A));\n"
"}");
"}", true);
ASSERT_EQUALS("", errout.str()); // std::array is POD (#5481)
}
void memsetOnStdPodType() { // Ticket #5901
checkNoMemset("struct st {\n"
" std::uint8_t a;\n"
" std::uint8_t b;\n"
" std::uint8_t c;\n"
"};\n"
"\n"
"void f() {\n"
" st s;\n"
" std::memset(&s, 0, sizeof(st));\n"
"}", "std.cfg");
ASSERT_EQUALS("", errout.str());
}
void mallocOnClass() {
checkNoMemset("class C { C() {} };\n"
"void foo(C*& p) {\n"