Skip to content

Commit 8fa3ffe

Browse files
committed
C++: Add a few more test cases that we don't recognize as OK.
1 parent 24947f2 commit 8fa3ffe

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
| test.c:63:29:63:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
33
| test.c:139:29:139:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
44
| test.c:186:29:186:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
5+
| test.c:282:29:282:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
6+
| test.c:299:26:299:32 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
7+
| test.c:316:33:316:39 | 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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,50 @@ unsigned char * noBadResize_2_7(unsigned char * buffer,size_t currentSize,size_t
272272
myASSERT_2(buffer);
273273
return buffer;
274274
}
275+
276+
unsigned char *goodResize_3_1(unsigned char *buffer, size_t currentSize, size_t newSize)
277+
{
278+
// GOOD: this way we will exclude possible memory leak [FALSE POSITIVE]
279+
unsigned char *tmp = buffer;
280+
if (currentSize < newSize)
281+
{
282+
buffer = (unsigned char *)realloc(buffer, newSize);
283+
if (buffer == NULL)
284+
{
285+
free(tmp);
286+
return NULL;
287+
}
288+
}
289+
290+
return buffer;
291+
}
292+
293+
unsigned char *goodResize_3_2(unsigned char *buffer, size_t currentSize, size_t newSize)
294+
{
295+
// GOOD: this way we will exclude possible memory leak [FALSE POSITIVE]
296+
unsigned char *tmp = buffer;
297+
if (currentSize < newSize)
298+
{
299+
tmp = (unsigned char *)realloc(tmp, newSize);
300+
if (tmp != 0)
301+
{
302+
buffer = tmp;
303+
}
304+
}
305+
306+
return buffer;
307+
}
308+
309+
void abort(void);
310+
311+
unsigned char *noBadResize_4_1(unsigned char *buffer, size_t currentSize, size_t newSize)
312+
{
313+
// GOOD: program to end [FALSE POSITIVE]
314+
if (currentSize < newSize)
315+
{
316+
if (buffer = (unsigned char *)realloc(buffer, newSize))
317+
abort();
318+
}
319+
320+
return buffer;
321+
}

0 commit comments

Comments
 (0)