Skip to content

Commit ae74161

Browse files
authored
Merge pull request #22730 from geoffw0/qualitytests3
C++: Fill gaps in query tests (part 3)
2 parents 6c0497c + 681a35e commit ae74161

18 files changed

Lines changed: 270 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:2:3:2:7 | re-throw exception | As there is no current exception, this rethrow expression will terminate the program. |
2+
| test.cpp:6:3:6:7 | re-throw exception | As there is no current exception, this rethrow expression will terminate the program. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Best Practices/Exceptions/AccidentalRethrow.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
void rethrowOutsideCatch() {
2+
throw; // $ Alert
3+
}
4+
5+
void helperRethrow() {
6+
throw; // $ Alert
7+
}
8+
9+
void safeRethrowInCatch() {
10+
try {
11+
} catch (...) {
12+
throw;
13+
}
14+
}
15+
16+
// The function name matches "%exception%", so a rethrow here is assumed to
17+
// be intentional even though it is lexically and dynamically outside any
18+
// catch block.
19+
void rethrowException() {
20+
throw;
21+
}
22+
23+
// Not lexically inside a catch block, but every call to this function is
24+
// made from within a catch block, so the rethrow is assumed to be safe.
25+
void calledFromCatch() {
26+
throw;
27+
}
28+
29+
void triggersFromCatch() {
30+
try {
31+
} catch (...) {
32+
calledFromCatch();
33+
}
34+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:12:32:12:34 | { ... } | This should catch a DerivedException by (const) reference rather than by value. |
2+
| test.cpp:16:29:16:31 | { ... } | This should catch a BaseException by (const) reference rather than by value. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Best Practices/Exceptions/CatchingByValue.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class BaseException {
2+
public:
3+
virtual ~BaseException() {}
4+
};
5+
6+
class DerivedException : public BaseException {
7+
};
8+
9+
void catchByValueDerived() {
10+
try {
11+
throw DerivedException();
12+
} catch (DerivedException e) { } // $ Alert
13+
14+
try {
15+
throw BaseException();
16+
} catch (BaseException e) { } // $ Alert
17+
18+
try {
19+
throw DerivedException();
20+
} catch (DerivedException &e) { }
21+
22+
try {
23+
throw DerivedException();
24+
} catch (BaseException &e) { }
25+
26+
try {
27+
throw new BaseException();
28+
} catch (BaseException *e) { }
29+
30+
try {
31+
throw DerivedException();
32+
} catch (...) { }
33+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:21:28:21:30 | { ... } | This catch block does not free the caught exception, thereby leaking memory. |
2+
| test.cpp:27:31:27: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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// --- definitions ---
2+
3+
class MyException {
4+
public:
5+
void ReportError() {}
6+
void Delete() {}
7+
};
8+
9+
class OtherException {
10+
public:
11+
void ReportError() {}
12+
};
13+
14+
void handle(OtherException *e) {}
15+
16+
// --- test cases ---
17+
18+
void leakyCatchEmpty() {
19+
try {
20+
// ...
21+
} catch (MyException *e) { } // $ Alert
22+
}
23+
24+
void leakyCatchNoDelete() {
25+
try {
26+
// ...
27+
} catch (OtherException *e) { e->ReportError(); } // $ Alert
28+
}
29+
30+
void catchWithDeleteMethodCall() {
31+
try {
32+
// ...
33+
} catch (MyException *e) {
34+
e->ReportError();
35+
e->Delete();
36+
}
37+
}
38+
39+
void catchWithOperatorDelete() {
40+
try {
41+
// ...
42+
} catch (MyException *e) {
43+
e->ReportError();
44+
delete e;
45+
}
46+
}
47+
48+
void catchWithPassToFunction() {
49+
try {
50+
// ...
51+
} catch (OtherException *e) {
52+
handle(e);
53+
}
54+
}
55+
56+
void catchByValueNotPointer() {
57+
try {
58+
// ...
59+
} catch (MyException e) { }
60+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.cpp:5:3:5:25 | throw ... | This should throw a MyException rather than a pointer to one. |

0 commit comments

Comments
 (0)