changeset 133:84182dab4fca

Fix reallocation.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 Jul 2014 18:22:52 +0300
parents 11fa2bf90251
children 4094fcfd4783
files th_string.c
diffstat 1 files changed, 8 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Fri Jul 04 02:18:26 2014 +0300
+++ b/th_string.c	Fri Jul 11 18:22:52 2014 +0300
@@ -83,10 +83,8 @@
  */
 char *th_strdup_vprintf(const char *fmt, va_list args)
 {
-    int size = strlen(fmt);
-    char *buf;
-    if (size < 64)
-        size = 64;
+    int size = 64;
+    char *buf, *tmp;
 
     if ((buf = th_malloc(size)) == NULL)
         return NULL;
@@ -106,8 +104,13 @@
         else
             size *= 2;
 
-        if ((buf = th_realloc(buf, size)) == NULL)
+        if ((tmp = th_realloc(buf, size)) == NULL)
+        {
+            th_free(buf);
             return NULL;
+        }
+        else
+            buf = tmp;
     }
 }