diff options
author | Daniel Stenberg <daniel@haxx.se> | 2016-11-07 10:55:25 +0100 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2016-11-11 10:03:48 +0100 |
commit | 0649433da53c7165f839e24e889e131e2894dd32 (patch) | |
tree | 7e516c1702fe87c09f190e5dc47ecd3a9bede1b8 /lib/strdup.c | |
parent | cdfda3ee827da069f1871722278fd82e7cbb4194 (diff) | |
download | curl-0649433da53c7165f839e24e889e131e2894dd32.tar.gz |
realloc: use Curl_saferealloc to avoid common mistakes
Discussed: https://curl.haxx.se/mail/lib-2016-11/0087.html
Diffstat (limited to 'lib/strdup.c')
-rw-r--r-- | lib/strdup.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/strdup.c b/lib/strdup.c index 5a15c2b16..136b69377 100644 --- a/lib/strdup.c +++ b/lib/strdup.c @@ -75,3 +75,26 @@ void *Curl_memdup(const void *src, size_t length) return buffer; } + +/*************************************************************************** + * + * Curl_saferealloc(ptr, size) + * + * Does a normal realloc(), but will free the data pointer if the realloc + * fails. If 'size' is zero, it will free the data and return a failure. + * + * This convenience function is provided and used to help us avoid a common + * mistake pattern when we could pass in a zero, catch the NULL return and end + * up free'ing the memory twice. + * + * Returns the new pointer or NULL on failure. + * + ***************************************************************************/ +void *Curl_saferealloc(void *ptr, size_t size) +{ + void *datap = realloc(ptr, size); + if(size && !datap) + /* only free 'ptr' if size was non-zero */ + free(ptr); + return datap; +} |