aboutsummaryrefslogtreecommitdiff
path: root/lib/strdup.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/strdup.c')
-rw-r--r--lib/strdup.c23
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;
+}