diff options
author | Erik de Castro Lopo <erikd@mega-nerd.com> | 2015-08-22 19:22:50 +1000 |
---|---|---|
committer | Erik de Castro Lopo <erikd@mega-nerd.com> | 2015-08-22 19:39:37 +1000 |
commit | d9ae5e9128f13fb43d33adf732c313516c70f9c6 (patch) | |
tree | 26a45432c984de73f845b4a23848c65d3e81b18f /include | |
parent | 684fb3d544b6fec54464e2f09296eb8d7705382a (diff) | |
download | flac-d9ae5e9128f13fb43d33adf732c313516c70f9c6.tar.gz |
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.
Diffstat (limited to 'include')
-rw-r--r-- | include/share/alloc.h | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/include/share/alloc.h b/include/share/alloc.h index 5e1f1e26..3b707491 100644 --- a/include/share/alloc.h +++ b/include/share/alloc.h @@ -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) */ |