From cb9d67b9ec380533bae937c2725bbaa4859480e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 9 Jul 2014 15:00:06 +0200 Subject: [PATCH] Fixed #5901 (False positive: (error) Using 'memcpy' with vector of uint8_t items) --- lib/checkclass.cpp | 2 +- test/testclass.cpp | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index f02ba6c59..12d74dd74 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -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 diff --git a/test/testclass.cpp b/test/testclass.cpp index 37e3f5fc4..2a05eb455 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -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"