Skip to content

Commit d5d8b48

Browse files
committed
C++: More accurate solution using Guards library.
1 parent 439fe41 commit d5d8b48

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ unsigned char *noBadResize_4_1(unsigned char *buffer, size_t currentSize, size_t
322322

323323
unsigned char * badResize_5_2(unsigned char *buffer, size_t currentSize, size_t newSize, int cond)
324324
{
325-
// BAD: on unsuccessful call to realloc, we will lose a pointer to a valid memory block [NOT DETECTED]
325+
// BAD: on unsuccessful call to realloc, we will lose a pointer to a valid memory block
326326
if (currentSize < newSize)
327327
{
328328
buffer = (unsigned char *)realloc(buffer, newSize);
@@ -336,7 +336,7 @@ unsigned char * badResize_5_2(unsigned char *buffer, size_t currentSize, size_t
336336

337337
unsigned char * badResize_5_1(unsigned char *buffer, size_t currentSize, size_t newSize, int cond)
338338
{
339-
// BAD: on unsuccessful call to realloc, we will lose a pointer to a valid memory block [NOT DETECTED]
339+
// BAD: on unsuccessful call to realloc, we will lose a pointer to a valid memory block
340340
if (currentSize < newSize)
341341
{
342342
buffer = (unsigned char *)realloc(buffer, newSize);

0 commit comments

Comments
 (0)