You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: test/testclass.cpp
+59Lines changed: 59 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -76,9 +76,11 @@ class TestClass : public TestFixture {
76
76
TEST_CASE(operatorEqToSelf7);
77
77
TEST_CASE(operatorEqToSelf8); // ticket #2179
78
78
TEST_CASE(operatorEqToSelf9); // ticket #2592
79
+
79
80
TEST_CASE(memsetOnStruct);
80
81
TEST_CASE(memsetVector);
81
82
TEST_CASE(memsetOnClass);
83
+
TEST_CASE(mallocOnClass);
82
84
83
85
TEST_CASE(this_subtraction); // warn about "this-x"
84
86
@@ -1948,6 +1950,7 @@ class TestClass : public TestFixture {
1948
1950
errout.str("");
1949
1951
1950
1952
Settings settings;
1953
+
settings.addEnabled("warning");
1951
1954
1952
1955
// Tokenize..
1953
1956
Tokenizer tokenizer(&settings, this);
@@ -2304,6 +2307,62 @@ class TestClass : public TestFixture {
2304
2307
ASSERT_EQUALS("", errout.str()); // #4460
2305
2308
}
2306
2309
2310
+
voidmallocOnClass() {
2311
+
checkNoMemset("class C { C() {} };\n"
2312
+
"void foo(C*& p) {\n"
2313
+
" p = malloc(sizeof(C));\n"
2314
+
"}");
2315
+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (warning) Memory for class instance allocated with malloc(), but class provides constructors.\n", errout.str());
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (warning) Memory for class instance allocated with malloc(), but class provides constructors.\n", errout.str());
2322
+
2323
+
checkNoMemset("struct C { C() {} };\n"
2324
+
"void foo(C*& p) {\n"
2325
+
" p = realloc(p, sizeof(C));\n"
2326
+
"}");
2327
+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (warning) Memory for class instance allocated with realloc(), but class provides constructors.\n", errout.str());
2328
+
2329
+
checkNoMemset("struct C { C() {} };\n"
2330
+
"void foo(C*& p) {\n"
2331
+
" p = realloc(p, sizeof(C));\n"
2332
+
"}");
2333
+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (warning) Memory for class instance allocated with realloc(), but class provides constructors.\n", errout.str());
2334
+
2335
+
checkNoMemset("struct C { virtual void bar(); };\n"
2336
+
"void foo(C*& p) {\n"
2337
+
" p = malloc(sizeof(C));\n"
2338
+
"}");
2339
+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (error) Memory for class instance allocated with malloc(), but class contains a virtual method.\n", errout.str());
2340
+
2341
+
checkNoMemset("struct C { std::string s; };\n"
2342
+
"void foo(C*& p) {\n"
2343
+
" p = malloc(sizeof(C));\n"
2344
+
"}");
2345
+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (error) Memory for class instance allocated with malloc(), but class contains a 'std::string'.\n", errout.str());
2346
+
2347
+
checkNoMemset("class C { };\n"// C-Style class/struct
0 commit comments