Skip to content

Commit

Permalink
Avoid overflows in testcase malloc_wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriAimonen committed Feb 1, 2020
1 parent e382ea5 commit 903ae5b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/common/malloc_wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void* malloc_with_check(size_t size)
{
char *buf = NULL;

if (g_alloc_bytes + size <= g_max_alloc_bytes)
if (size <= g_max_alloc_bytes - g_alloc_bytes)
{
buf = malloc(size + GUARD_SIZE);
}
Expand Down Expand Up @@ -89,7 +89,7 @@ void* realloc_with_check(void *ptr, size_t size)
assert(g_alloc_count > 0);
assert(g_alloc_bytes >= oldsize);

if (g_alloc_bytes - oldsize + size <= g_max_alloc_bytes)
if (size <= g_max_alloc_bytes - (g_alloc_bytes - oldsize))
{
buf = realloc(buf, size + GUARD_SIZE);
}
Expand Down

0 comments on commit 903ae5b

Please sign in to comment.