diff options
author | hexcoder- <heiko@hexco.de> | 2020-11-17 21:06:47 +0100 |
---|---|---|
committer | hexcoder- <heiko@hexco.de> | 2020-11-17 21:06:47 +0100 |
commit | add108ec2386736f683e34172311af2c5a9d6237 (patch) | |
tree | 0da922defab96778a66a280d61d332a604324f63 /include/alloc-inl.h | |
parent | d042a63ab43545a5038fe46d11fef1bde8327028 (diff) | |
download | afl++-add108ec2386736f683e34172311af2c5a9d6237.tar.gz |
fix two exotic mem leaks detected by cppcheck
Diffstat (limited to 'include/alloc-inl.h')
-rw-r--r-- | include/alloc-inl.h | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/include/alloc-inl.h b/include/alloc-inl.h index d7aa51a7..a6194f86 100644 --- a/include/alloc-inl.h +++ b/include/alloc-inl.h @@ -636,7 +636,7 @@ struct afl_alloc_buf { #define AFL_ALLOC_SIZE_OFFSET (offsetof(struct afl_alloc_buf, buf)) -/* Returs the container element to this ptr */ +/* Returns the container element to this ptr */ static inline struct afl_alloc_buf *afl_alloc_bufptr(void *buf) { return (struct afl_alloc_buf *)((u8 *)buf - AFL_ALLOC_SIZE_OFFSET); @@ -694,14 +694,20 @@ static inline void *afl_realloc(void **buf, size_t size_needed) { } /* alloc */ - new_buf = (struct afl_alloc_buf *)realloc(new_buf, next_size); - if (unlikely(!new_buf)) { + struct afl_alloc_buf *newer_buf = (struct afl_alloc_buf *)realloc(new_buf, next_size); + if (unlikely(!newer_buf)) { + free(new_buf); // avoid a leak *buf = NULL; return NULL; + } else { + + new_buf = newer_buf; + } + new_buf->complete_size = next_size; *buf = (void *)(new_buf->buf); return *buf; @@ -730,12 +736,17 @@ static inline void *afl_realloc_exact(void **buf, size_t size_needed) { if (unlikely(current_size == size_needed)) { return *buf; } /* alloc */ - new_buf = (struct afl_alloc_buf *)realloc(new_buf, size_needed); - if (unlikely(!new_buf)) { + struct afl_alloc_buf *newer_buf = (struct afl_alloc_buf *)realloc(new_buf, size_needed); + if (unlikely(!newer_buf)) { + free(new_buf); // avoid a leak *buf = NULL; return NULL; + } else { + + new_buf = newer_buf; + } new_buf->complete_size = size_needed; |