libFLAC: Add function safe_realloc_()

The new function wraps, realloc() and if the realloc() fails, it
free()s the old pointer.

This is an improvement on the potential realloc() memory leak that
was fixed in 15a9062609.

Still needs fuzzing to validate it.
This commit is contained in:
Erik de Castro Lopo
2015-08-22 19:22:50 +10:00
parent 684fb3d544
commit d9ae5e9128
5 changed files with 28 additions and 48 deletions

View File

@@ -153,11 +153,21 @@ static inline void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size
return malloc(size1*size2);
}
static inline void *safe_realloc_(void *ptr, size_t size)
{
void *oldptr = ptr;
void *newptr = realloc(ptr, size);
if(size > 0 && newptr == 0)
free(oldptr);
return newptr;
}
static inline void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
{
size2 += size1;
if(size2 < size1)
if(size2 < size1) {
free(ptr);
return 0;
}
return realloc(ptr, size2);
}
@@ -192,7 +202,7 @@ static inline void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
if(size1 > SIZE_MAX / size2)
return 0;
return realloc(ptr, size1*size2);
return safe_realloc_(ptr, size1*size2);
}
/* size1 * (size2 + size3) */