Skip to content

Commit 9e39b08

Browse files
committed
C++: Improve the qhelp for cpp/detect-and-handle-memory-allocation-errors.
1 parent 44de127 commit 9e39b08

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed
Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
1-
// BAD: on memory allocation error, the program terminates.
2-
void badFunction(const int *source, std::size_t length) noexcept {
3-
int * dest = new int[length];
1+
// BAD: the allocation will throw an unhandled exception
2+
// instead of returning a null pointer.
3+
void bad1(std::size_t length) noexcept {
4+
int* dest = new int[length];
5+
if(!dest) {
6+
return;
7+
}
48
std::memset(dest, 0, length);
5-
// ..
9+
// ...
610
}
7-
// GOOD: memory allocation error will be handled.
8-
void goodFunction(const int *source, std::size_t length) noexcept {
11+
12+
// BAD: the allocation won't throw an exception, but
13+
// instead return a null pointer.
14+
void bad2(std::size_t length) noexcept {
915
try {
10-
int * dest = new int[length];
11-
} catch(std::bad_alloc) {
16+
int* dest = new(std::nothrow) int[length];
17+
std::memset(dest, 0, length);
18+
// ...
19+
} catch(std::bad_alloc&) {
1220
// ...
1321
}
14-
std::memset(dest, 0, length);
15-
// ..
1622
}
17-
// BAD: memory allocation error will not be handled.
18-
void badFunction(const int *source, std::size_t length) noexcept {
23+
24+
// GOOD: the allocation failure is handled appropiately.
25+
void good1(std::size_t length) noexcept {
1926
try {
20-
int * dest = new (std::nothrow) int[length];
21-
} catch(std::bad_alloc) {
27+
int* dest = new int[length];
28+
std::memset(dest, 0, length);
29+
// ...
30+
} catch(std::bad_alloc&) {
2231
// ...
2332
}
24-
std::memset(dest, 0, length);
25-
// ..
2633
}
27-
// GOOD: memory allocation error will be handled.
28-
void goodFunction(const int *source, std::size_t length) noexcept {
29-
int * dest = new (std::nothrow) int[length];
30-
if (!dest) {
31-
return;
34+
35+
// GOOD: the allocation failure is handled appropiately.
36+
void good2(std::size_t length) noexcept {
37+
int* dest = new int[length];
38+
if(!dest) {
39+
return;
3240
}
3341
std::memset(dest, 0, length);
34-
// ..
42+
// ...
3543
}

cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
"qhelp.dtd">
44
<qhelp>
55
<overview>
6-
<p>When using the <code>new</code> operator to allocate memory, you need to pay attention to the different ways of detecting errors. <code>::operator new(std::size_t)</code> throws an exception on error, whereas <code>::operator new(std::size_t, const std::nothrow_t &amp;)</code> returns zero on error. The programmer can get confused and check the error that occurs when allocating memory incorrectly. That can lead to an unhandled program termination or to a violation of the program logic.</p>
6+
<p>Different overloads of the <code>new</code> operator handle allocation failures in different ways:
7+
If <code>new T</code> fails for some type <code>T</code>, it throws a <code>std::bad_alloc</code> exception,
8+
but <code>new(std::nothrow) T</code> returns a null pointer. If the programmer handles the wrong kind of
9+
failure, it could cause the program to behave in unexpected ways.</p>
710

811
</overview>
912
<recommendation>
1013

11-
<p>Use the correct error detection method corresponding with the memory allocation.</p>
14+
<p>Make sure that exceptions are handled appropriately if <code>new T</code> is used. On the other hand,
15+
make sure to handle the possibility of null pointers if <code>new(std::nothrow) T</code> is used.</p>
1216

1317
</recommendation>
1418
<example>
15-
<p>The following example demonstrates various approaches to detecting memory allocation errors using the <code>new</code> operator.</p>
1619
<sample src="WrongInDetectingAndHandlingMemoryAllocationErrors.cpp" />
1720

1821
</example>

0 commit comments

Comments
 (0)