MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / TestLeakAutoVarStrcpy

Class TestLeakAutoVarStrcpy

test/testleakautovar.cpp:3303–3403  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3301#endif
3302
3303class TestLeakAutoVarStrcpy : public TestFixture {
3304public:
3305 TestLeakAutoVarStrcpy() : TestFixture("TestLeakAutoVarStrcpy") {}
3306
3307private:
3308 const Settings settings = settingsBuilder().library("std.cfg").checkLibrary().build();
3309
3310 template<size_t size>
3311 void check_(const char* file, int line, const char (&code)[size]) {
3312 // Tokenize..
3313 SimpleTokenizer tokenizer(settings, *this);
3314 ASSERT_LOC(tokenizer.tokenize(code), file, line);
3315
3316 CheckLeakAutoVar check;
3317 runChecks(check, tokenizer, *this);
3318 }
3319
3320 void run() override {
3321 mNewTemplate = true;
3322 TEST_CASE(returnedValue); // #9298
3323 TEST_CASE(deallocuse2);
3324 TEST_CASE(fclose_false_positive); // #9575
3325 TEST_CASE(strcpy_false_negative);
3326 TEST_CASE(doubleFree);
3327 TEST_CASE(memleak_std_string);
3328 }
3329
3330 void returnedValue() { // #9298
3331 check("char *m;\n"
3332 "void strcpy_returnedvalue(const char* str)\n"
3333 "{\n"
3334 " char* ptr = new char[strlen(str)+1];\n"
3335 " m = strcpy(ptr, str);\n"
3336 "}");
3337 ASSERT_EQUALS("", errout_str());
3338 }
3339
3340 void deallocuse2() {
3341 check("void f(char *p) {\n"
3342 " free(p);\n"
3343 " strcpy(a, p);\n"
3344 "}");
3345 ASSERT_EQUALS("[test.cpp:3:15]: (error) Dereferencing 'p' after it is deallocated / released [deallocuse]\n", errout_str());
3346
3347 check("void f(char *p, const char *q) {\n" // #11665
3348 " free(p);\n"
3349 " strcpy(p, q);\n"
3350 "}\n");
3351 ASSERT_EQUALS("[test.cpp:3:12]: (error) Dereferencing 'p' after it is deallocated / released [deallocuse]\n",
3352 errout_str());
3353
3354 check("void f(char *p) {\n" // #3041 - assigning pointer when it's used
3355 " free(p);\n"
3356 " strcpy(a, p=b());\n"
3357 "}");
3358 ASSERT_EQUALS("", errout_str());
3359 }
3360

Callers

nothing calls this directly

Calls 3

buildMethod · 0.80
checkLibraryMethod · 0.80
libraryMethod · 0.80

Tested by

no test coverage detected