Skip to content

Commit 7bc461a

Browse files
authored
Merge pull request #4990 from geoffw0/cpp401b
C++: Further improvements to experimental query cpp/memory-leak-on-failed-call-to-realloc
2 parents 0e059ce + d5d8b48 commit 7bc461a

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-401/MemoryLeakOnFailedCallToRealloc.ql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
import cpp
15+
import semmle.code.cpp.controlflow.Guards
1516

1617
/**
1718
* A function call that potentially does not return (such as `exit`).
@@ -48,7 +49,11 @@ class ReallocCallLeak extends FunctionCall {
4849
* example a call to `exit()`.
4950
*/
5051
predicate mayHandleByTermination() {
51-
this.(ControlFlowNode).getASuccessor*() instanceof CallMayNotReturn
52+
exists(GuardCondition guard, CallMayNotReturn exit |
53+
this.(ControlFlowNode).getASuccessor*() = guard and
54+
guard.getAChild*() = v.getAnAccess() and
55+
guard.controls(exit.getBasicBlock(), _)
56+
)
5257
}
5358
}
5459

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-401/semmle/tests/MemoryLeakOnFailedCallToRealloc.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
| test.c:186:29:186:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
55
| test.c:282:29:282:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
66
| test.c:299:26:299:32 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
7+
| test.c:328:29:328:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
8+
| test.c:342:29:342:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,28 @@ unsigned char *noBadResize_4_1(unsigned char *buffer, size_t currentSize, size_t
319319

320320
return buffer;
321321
}
322+
323+
unsigned char * badResize_5_2(unsigned char *buffer, size_t currentSize, size_t newSize, int cond)
324+
{
325+
// BAD: on unsuccessful call to realloc, we will lose a pointer to a valid memory block
326+
if (currentSize < newSize)
327+
{
328+
buffer = (unsigned char *)realloc(buffer, newSize);
329+
}
330+
if (cond)
331+
{
332+
abort(); // irrelevant
333+
}
334+
return buffer;
335+
}
336+
337+
unsigned char * badResize_5_1(unsigned char *buffer, size_t currentSize, size_t newSize, int cond)
338+
{
339+
// BAD: on unsuccessful call to realloc, we will lose a pointer to a valid memory block
340+
if (currentSize < newSize)
341+
{
342+
buffer = (unsigned char *)realloc(buffer, newSize);
343+
assert(cond); // irrelevant
344+
}
345+
return buffer;
346+
}

0 commit comments

Comments
 (0)