]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: new function my_realloc2 = realloc + free upon failure
authorHubert Verstraete <hubs@users.sourceforge.net>
Tue, 28 Jun 2016 20:41:00 +0000 (22:41 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 29 Jun 2016 08:45:15 +0000 (10:45 +0200)
When realloc fails to allocate memory, the original pointer is not
freed. Sometime people override the original pointer with the pointer
returned by realloc which is NULL in case of failure. This results
in a memory leak because the memory pointed by the original pointer
cannot be freed.

include/common/standard.h

index 8711ede28b7a8023e856395df2ccffbc42461dcb..bf14369e865f2b16312ae93d795a10b9a6eb2fe2 100644 (file)
@@ -1093,4 +1093,15 @@ static inline unsigned long long rdtsc()
 struct list;
 int list_append_word(struct list *li, const char *str, char **err);
 
+/* same as realloc() except that ptr is also freed upon failure */
+static inline void *my_realloc2(void *ptr, size_t size)
+{
+       void *ret;
+
+       ret = realloc(ptr, size);
+       if (!ret && size)
+               free(ptr);
+       return ret;
+}
+
 #endif /* _COMMON_STANDARD_H */