Skip to content

Commit 58f3048

Browse files
committed
C++: Add more testcases.
1 parent 5bcf810 commit 58f3048

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-570/semmle/tests/WrongInDetectingAndHandlingMemoryAllocationErrors.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@
77
| test.cpp:92:5:92:31 | call to operator new[] | memory allocation error check is incorrect or missing |
88
| test.cpp:93:15:93:41 | call to operator new[] | memory allocation error check is incorrect or missing |
99
| test.cpp:96:10:96:36 | call to operator new[] | memory allocation error check is incorrect or missing |
10+
| test.cpp:151:9:151:24 | call to operator new | memory allocation error check is incorrect or missing |
11+
| test.cpp:157:9:157:28 | call to operator new | memory allocation error check is incorrect or missing |
12+
| test.cpp:182:15:182:35 | call to operator new | memory allocation error check is incorrect or missing |
13+
| test.cpp:187:15:187:35 | call to operator new | memory allocation error check is incorrect or missing |
14+
| test.cpp:192:15:192:35 | call to operator new | memory allocation error check is incorrect or missing |
15+
| test.cpp:199:15:199:35 | call to operator new | memory allocation error check is incorrect or missing |
16+
| test.cpp:204:15:204:35 | call to operator new | memory allocation error check is incorrect or missing |

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,74 @@ void good_new_handles_nullptr() {
135135
if (new (std::nothrow) int[100] == nullptr)
136136
return; // GOOD
137137
}
138+
139+
void* operator new(std::size_t count, void*) noexcept;
140+
void* operator new[](std::size_t count, void*) noexcept;
141+
142+
struct Foo {
143+
Foo() noexcept;
144+
Foo(int);
145+
146+
operator bool();
147+
};
148+
149+
void bad_placement_new_with_exception_handling() {
150+
char buffer[1024];
151+
try { new (buffer) Foo; } // BAD
152+
catch (...) { }
153+
}
154+
155+
void good_placement_new_with_exception_handling() {
156+
char buffer[1024];
157+
try { new (buffer) Foo(42); } // GOOD: Foo constructor might throw [FALSE POSITIVE]
158+
catch (...) { }
159+
}
160+
161+
int rand();
162+
163+
void may_throw() {
164+
if(rand()) {
165+
throw "bad luck exception!";
166+
}
167+
}
168+
169+
void unknown_code_that_may_throw(int*);
170+
void unknown_code_that_will_not_throw(int*) noexcept;
171+
172+
void calls_throwing_code(int* p) {
173+
if(rand()) unknown_code_that_may_throw(p);
174+
}
175+
176+
void calls_non_throwing(int* p) {
177+
if (rand()) unknown_code_that_will_not_throw(p);
178+
}
179+
180+
void good_new_with_throwing_call() {
181+
try {
182+
int* p1 = new(std::nothrow) int; // GOOD [FALSE POSITIVE]
183+
may_throw();
184+
} catch(...) { }
185+
186+
try {
187+
int* p2 = new(std::nothrow) int; // GOOD [FALSE POSITIVE]
188+
Foo f(10);
189+
} catch(...) { }
190+
191+
try {
192+
int* p3 = new(std::nothrow) int; // GOOD [FALSE POSITIVE]
193+
calls_throwing_code(p3);
194+
} catch(...) { }
195+
}
196+
197+
void bad_new_with_nonthrowing_call() {
198+
try {
199+
int* p1 = new(std::nothrow) int; // BAD
200+
calls_non_throwing(p1);
201+
} catch(...) { }
202+
203+
try {
204+
int* p2 = new(std::nothrow) int; // GOOD: boolean conversion constructor might throw [FALSE POSITIVE]
205+
Foo f(12);
206+
if(f) { }
207+
} catch(...) { }
208+
}

0 commit comments

Comments
 (0)