C++: Add a few more test cases that we don't recognize as OK.

This commit is contained in:
Geoffrey White
2021-01-12 18:33:58 +00:00
parent 24947f27b4
commit 8fa3ffe125
2 changed files with 50 additions and 0 deletions

View File

@@ -2,3 +2,6 @@
| test.c:63:29:63:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
| test.c:139:29:139:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
| test.c:186:29:186:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
| test.c:282:29:282:35 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
| test.c:299:26:299:32 | call to realloc | possible loss of original pointer on unsuccessful call realloc |
| test.c:316:33:316:39 | call to realloc | possible loss of original pointer on unsuccessful call realloc |

View File

@@ -272,3 +272,50 @@ unsigned char * noBadResize_2_7(unsigned char * buffer,size_t currentSize,size_t
myASSERT_2(buffer);
return buffer;
}
unsigned char *goodResize_3_1(unsigned char *buffer, size_t currentSize, size_t newSize)
{
// GOOD: this way we will exclude possible memory leak [FALSE POSITIVE]
unsigned char *tmp = buffer;
if (currentSize < newSize)
{
buffer = (unsigned char *)realloc(buffer, newSize);
if (buffer == NULL)
{
free(tmp);
return NULL;
}
}
return buffer;
}
unsigned char *goodResize_3_2(unsigned char *buffer, size_t currentSize, size_t newSize)
{
// GOOD: this way we will exclude possible memory leak [FALSE POSITIVE]
unsigned char *tmp = buffer;
if (currentSize < newSize)
{
tmp = (unsigned char *)realloc(tmp, newSize);
if (tmp != 0)
{
buffer = tmp;
}
}
return buffer;
}
void abort(void);
unsigned char *noBadResize_4_1(unsigned char *buffer, size_t currentSize, size_t newSize)
{
// GOOD: program to end [FALSE POSITIVE]
if (currentSize < newSize)
{
if (buffer = (unsigned char *)realloc(buffer, newSize))
abort();
}
return buffer;
}