Don't use non-portable asprintf()
authorPetri Lehtinen <petri@digip.org>
Tue, 13 Oct 2009 12:40:26 +0000 (15:40 +0300)
committerPetri Lehtinen <petri@digip.org>
Tue, 13 Oct 2009 12:40:26 +0000 (15:40 +0300)
Thanks to Adam Strzelecki for reporting.

src/dump.c

index 93717ab..dad64f8 100644 (file)
@@ -13,6 +13,9 @@
 #include <jansson.h>
 #include "strbuffer.h"
 
+#define MAX_INTEGER_STR_LENGTH  100
+#define MAX_REAL_STR_LENGTH     100
+
 typedef int (*dump_func)(const char *buffer, int size, void *data);
 
 struct string
@@ -126,30 +129,26 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
 
         case JSON_INTEGER:
         {
-            char *buffer;
-            int size, ret;
+            char buffer[MAX_INTEGER_STR_LENGTH];
+            int size;
 
-            size = asprintf(&buffer, "%d", json_integer_value(json));
-            if(size == -1)
+            size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json));
+            if(size >= MAX_INTEGER_STR_LENGTH)
                 return -1;
 
-            ret = dump(buffer, size, data);
-            free(buffer);
-            return ret;
+            return dump(buffer, size, data);
         }
 
         case JSON_REAL:
         {
-            char *buffer;
-            int size, ret;
+            char buffer[MAX_REAL_STR_LENGTH];
+            int size;
 
-            size = asprintf(&buffer, "%.17f", json_real_value(json));
-            if(size == -1)
+            size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%0.17f", json_real_value(json));
+            if(size >= MAX_REAL_STR_LENGTH)
                 return -1;
 
-            ret = dump(buffer, size, data);
-            free(buffer);
-            return ret;
+            return dump(buffer, size, data);
         }
 
         case JSON_STRING: