Skip to content

Commit c5af13f

Browse files
geoffw0Copilot
andcommitted
C++: Test pointer catches missing a release
Cover leaked caught pointers and the method-delete, operator-delete, and escape exclusions for cpp/catch-missing-free. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ee7c52e commit c5af13f

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:16:28:16:30 | { ... } | This catch block does not free the caught exception, thereby leaking memory. |
2+
| test.cpp:21:31:21:51 | { ... } | This catch block does not free the caught exception, thereby leaking memory. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Best Practices/Exceptions/LeakyCatch.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class MyException {
2+
public:
3+
void ReportError() {}
4+
void Delete() {}
5+
};
6+
7+
class OtherException {
8+
public:
9+
void ReportError() {}
10+
};
11+
12+
void handle(OtherException *e) {}
13+
14+
void leakyCatchEmpty() {
15+
try {
16+
} catch (MyException *e) { } // $ Alert
17+
}
18+
19+
void leakyCatchNoDelete() {
20+
try {
21+
} catch (OtherException *e) { e->ReportError(); } // $ Alert
22+
}
23+
24+
void catchWithDeleteMethodCall() {
25+
try {
26+
} catch (MyException *e) {
27+
e->ReportError();
28+
e->Delete();
29+
}
30+
}
31+
32+
void catchWithOperatorDelete() {
33+
try {
34+
} catch (MyException *e) {
35+
e->ReportError();
36+
delete e;
37+
}
38+
}
39+
40+
void catchWithPassToFunction() {
41+
try {
42+
} catch (OtherException *e) {
43+
handle(e);
44+
}
45+
}
46+
47+
void catchByValueNotPointer() {
48+
try {
49+
} catch (MyException e) { }
50+
}

0 commit comments

Comments
 (0)