Skip to content

Commit 1ed5af1

Browse files
authored
Merge pull request #17035 from geoffw0/allocerr
C++: Fix issue with cpp/incorrect-allocation-error-handling
2 parents db53be3 + b1608d8 commit 1ed5af1

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

cpp/ql/src/Security/CWE/CWE-570/IncorrectAllocationErrorHandling.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ predicate nullCheckInThrowingNew(NewOrNewArrayExpr newExpr, GuardCondition guard
232232
from NewOrNewArrayExpr newExpr, Element element, string msg, string elementString
233233
where
234234
not newExpr.isFromUninstantiatedTemplate(_) and
235+
not newExpr.isFromTemplateInstantiation(_) and
235236
(
236237
noThrowInTryBlock(newExpr, element) and
237238
msg = "This allocation cannot throw. $@ is unnecessary." and
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `cpp/incorrect-allocation-error-handling` ("Incorrect allocation-error handling") query no longer produces occasional false positive results inside template instantiations.

cpp/ql/test/query-tests/Security/CWE/CWE-570/IncorrectAllocationErrorHandling.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
| test.cpp:92:5:92:31 | new[] | This allocation cannot throw. $@ is unnecessary. | test.cpp:97:36:98:3 | { ... } | This catch block |
1414
| test.cpp:93:15:93:41 | new[] | This allocation cannot throw. $@ is unnecessary. | test.cpp:97:36:98:3 | { ... } | This catch block |
1515
| test.cpp:96:10:96:36 | new[] | This allocation cannot throw. $@ is unnecessary. | test.cpp:97:36:98:3 | { ... } | This catch block |
16-
| test.cpp:151:9:151:24 | new | This allocation cannot throw. $@ is unnecessary. | test.cpp:152:15:152:18 | { ... } | This catch block |
17-
| test.cpp:199:15:199:35 | new | This allocation cannot throw. $@ is unnecessary. | test.cpp:201:16:201:19 | { ... } | This catch block |
18-
| test.cpp:212:14:212:34 | new | This allocation cannot throw. $@ is unnecessary. | test.cpp:213:34:213:36 | { ... } | This catch block |
19-
| test.cpp:246:17:246:31 | new[] | This allocation cannot return null. $@ is unnecessary. | test.cpp:247:8:247:12 | ! ... | This check |
16+
| test.cpp:160:9:160:24 | new | This allocation cannot throw. $@ is unnecessary. | test.cpp:161:15:161:18 | { ... } | This catch block |
17+
| test.cpp:229:15:229:35 | new | This allocation cannot throw. $@ is unnecessary. | test.cpp:231:16:231:19 | { ... } | This catch block |
18+
| test.cpp:242:14:242:34 | new | This allocation cannot throw. $@ is unnecessary. | test.cpp:243:34:243:36 | { ... } | This catch block |
19+
| test.cpp:276:17:276:31 | new[] | This allocation cannot return null. $@ is unnecessary. | test.cpp:277:8:277:12 | ! ... | This check |

cpp/ql/test/query-tests/Security/CWE/CWE-570/test.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ void good_new_handles_nullptr() {
136136
return; // GOOD
137137
}
138138

139+
// ---
140+
139141
void* operator new(std::size_t count, void*) noexcept;
140142
void* operator new[](std::size_t count, void*) noexcept;
141143

@@ -146,18 +148,46 @@ struct Foo {
146148
operator bool();
147149
};
148150

151+
struct Bar {
152+
Bar();
153+
154+
operator bool();
155+
};
156+
149157
void bad_placement_new_with_exception_handling() {
150158
char buffer[1024];
151-
try { new (buffer) Foo; } // BAD
159+
160+
try { new (buffer) Foo; } // BAD (placement new should not fail)
152161
catch (...) { }
153162
}
154163

155164
void good_placement_new_with_exception_handling() {
156165
char buffer[1024];
166+
157167
try { new (buffer) Foo(42); } // GOOD: Foo constructor might throw
158168
catch (...) { }
169+
170+
try { new (buffer) Bar; } // GOOD: Bar constructor might throw
171+
catch (...) { }
159172
}
160173

174+
template<typename F> F *test_template_platement_new() {
175+
char buffer[1024];
176+
177+
try {
178+
return new (buffer) F; // GOOD: `F` constructor might throw (when `F` is `Bar`)
179+
} catch (...) {
180+
return 0;
181+
}
182+
}
183+
184+
void test_template_platement_new_caller() {
185+
test_template_platement_new<Foo>();
186+
test_template_platement_new<Bar>();
187+
}
188+
189+
// ---
190+
161191
int unknown_value_without_exceptions() noexcept;
162192

163193
void may_throw() {

0 commit comments

Comments
 (0)