Skip to content

Commit cb9d67b

Browse files
committed
Fixed danmar#5901 (False positive: (error) Using 'memcpy' with vector of uint8_t items)
1 parent 7b06167 commit cb9d67b

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

lib/checkclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Sco
10731073
const Scope *typeScope = var->typeScope();
10741074

10751075
// check for std:: type
1076-
if (var->isStlType() && tok1->strAt(2) != "array")
1076+
if (var->isStlType() && tok1->strAt(2) != "array" && !_settings->library.podtype(tok1->strAt(2)))
10771077
if (allocation)
10781078
mallocOnClassError(tok, tok->str(), type->classDef, "'std::" + tok1->strAt(2) + "'");
10791079
else

test/testclass.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class TestClass : public TestFixture {
8484
TEST_CASE(memsetVector);
8585
TEST_CASE(memsetOnClass);
8686
TEST_CASE(memsetOnInvalid); // Ticket #5425: Crash upon invalid
87+
TEST_CASE(memsetOnStdPodType); // #5901 - std::uint8_t
8788
TEST_CASE(mallocOnClass);
8889

8990
TEST_CASE(this_subtraction); // warn about "this-x"
@@ -2123,12 +2124,15 @@ class TestClass : public TestFixture {
21232124
ASSERT_EQUALS("", errout.str());
21242125
}
21252126

2126-
void checkNoMemset(const char code[]) {
2127+
void checkNoMemset(const char code[], bool load_std_cfg = false) {
21272128
// Clear the error log
21282129
errout.str("");
21292130

21302131
Settings settings;
21312132
settings.addEnabled("warning");
2133+
if (load_std_cfg) {
2134+
LOAD_LIB_2(settings.library, "std.cfg");
2135+
}
21322136

21332137
// Tokenize..
21342138
Tokenizer tokenizer(&settings, this);
@@ -2565,10 +2569,24 @@ class TestClass : public TestFixture {
25652569
"void f() {\n"
25662570
" A a;\n"
25672571
" memset(&a, 0, sizeof(A));\n"
2568-
"}");
2572+
"}", true);
25692573
ASSERT_EQUALS("", errout.str()); // std::array is POD (#5481)
25702574
}
25712575

2576+
void memsetOnStdPodType() { // Ticket #5901
2577+
checkNoMemset("struct st {\n"
2578+
" std::uint8_t a;\n"
2579+
" std::uint8_t b;\n"
2580+
" std::uint8_t c;\n"
2581+
"};\n"
2582+
"\n"
2583+
"void f() {\n"
2584+
" st s;\n"
2585+
" std::memset(&s, 0, sizeof(st));\n"
2586+
"}", "std.cfg");
2587+
ASSERT_EQUALS("", errout.str());
2588+
}
2589+
25722590
void mallocOnClass() {
25732591
checkNoMemset("class C { C() {} };\n"
25742592
"void foo(C*& p) {\n"

0 commit comments

Comments
 (0)