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

Method ifelse24

test/testleakautovar.cpp:2281–2325  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2279 }
2280
2281 void ifelse24() { // #1733
2282 const Settings s = settingsBuilder().library("std.cfg").library("posix.cfg").checkLibrary().build();
2283
2284 check("void f() {\n"
2285 " char* temp = strdup(\"temp.txt\");\n"
2286 " FILE* fp;\n"
2287 " if (NULL == x || NULL == (fp = fopen(temp, \"rt\")))\n"
2288 " return;\n"
2289 "}\n", s);
2290 ASSERT_EQUALS("[test.cpp:5:9]: (error) Memory leak: temp [memleak]\n"
2291 "[test.cpp:6:1]: (error) Memory leak: temp [memleak]\n"
2292 "[test.cpp:6:1]: (error) Resource leak: fp [resourceLeak]\n",
2293 errout_str());
2294
2295 check("FILE* f() {\n"
2296 " char* temp = strdup(\"temp.txt\");\n"
2297 " FILE* fp = fopen(temp, \"rt\");\n"
2298 " return fp;\n"
2299 "}\n", s);
2300 ASSERT_EQUALS("[test.cpp:4:5]: (error) Memory leak: temp [memleak]\n", errout_str());
2301
2302 check("FILE* f() {\n"
2303 " char* temp = strdup(\"temp.txt\");\n"
2304 " FILE* fp = NULL;\n"
2305 " fopen_s(&fp, temp, \"rt\");\n"
2306 " return fp;\n"
2307 "}\n", s);
2308 ASSERT_EQUALS("[test.cpp:5:5]: (error) Memory leak: temp [memleak]\n", errout_str());
2309
2310 check("void f() {\n"
2311 " char* temp = strdup(\"temp.txt\");\n"
2312 " FILE* fp = fopen(\"a.txt\", \"rb\");\n"
2313 " if (fp)\n"
2314 " freopen(temp, \"rt\", fp);\n"
2315 "}\n", s);
2316 ASSERT_EQUALS("[test.cpp:6:1]: (error) Memory leak: temp [memleak]\n"
2317 "[test.cpp:6:1]: (error) Resource leak: fp [resourceLeak]\n",
2318 errout_str());
2319
2320 check("FILE* f() {\n"
2321 " char* temp = strdup(\"temp.txt\");\n"
2322 " return fopen(temp, \"rt\");\n"
2323 "}\n", s);
2324 TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: temp\n", "", errout_str());
2325 }
2326
2327 void ifelse25() { // #9966
2328 check("void f() {\n"

Callers

nothing calls this directly

Calls 3

buildMethod · 0.80
checkLibraryMethod · 0.80
libraryMethod · 0.80

Tested by

no test coverage detected