From 6ea88c27580a2cd04cca1f58f4a8b9d2412b629c Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Fri, 9 Jun 2023 21:46:45 +0200 Subject: [PATCH] Added test case from #8302 (#5138) * Added test case from #8302 and a TODO * Check with in C++ mode to get rid of TODO. * Updated expected error message text * Updated expected error message text --- test/testleakautovar.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index a873e469c..42f2416f1 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -112,6 +112,7 @@ private: TEST_CASE(deallocuse8); // #1765 TEST_CASE(deallocuse9); // #9781 TEST_CASE(deallocuse10); + TEST_CASE(deallocuse11); // #8302 TEST_CASE(doublefree1); TEST_CASE(doublefree2); @@ -840,6 +841,22 @@ private: ASSERT_EQUALS("[test.c:2] -> [test.c:3]: (error) Returning/dereferencing 'p' after it is deallocated / released\n", errout.str()); } + void deallocuse11() { // #8302 + check("int f() {\n" + " int *array = new int[42];\n" + " delete [] array;\n" + " return array[1];" // << + "}", true); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Returning/dereferencing 'array' after it is deallocated / released\n", errout.str()); + + check("int f() {\n" + " int *array = (int*)malloc(40);\n" + " free(array);\n" + " return array[1];" // << + "}"); + ASSERT_EQUALS("[test.c:3] -> [test.c:4]: (error) Returning/dereferencing 'array' after it is deallocated / released\n", errout.str()); + } + void doublefree1() { // #3895 check("void f(char *p) {\n" " if (x)\n"