libopkg: fix pkg_set_ptr() to properly set NULL pointers
[oweals/opkg-lede.git] / libopkg / sprintf_alloc.c
index e2513ecee5fda05c59861f6d08d0205d71270faa..82a847160edd9a6f52097d11c8705df73b2e82d9 100644 (file)
 #include "sprintf_alloc.h"
 #include "libbb/libbb.h"
 
-void
-sprintf_alloc(char **str, const char *fmt, ...)
+void sprintf_alloc(char **str, const char *fmt, ...)
 {
        va_list ap;
        int n;
-       unsigned int size = 1;
+       unsigned int size = 0;
 
-       *str = xcalloc(1, size);
+       *str = NULL;
 
        for (;;) {
                va_start(ap, fmt);
-               n = vsnprintf (*str, size, fmt, ap);
+               n = vsnprintf(*str, size, fmt, ap);
                va_end(ap);
 
                if (n < 0) {
                        fprintf(stderr, "%s: encountered an output or encoding"
-                                       " error during vsnprintf.\n",
-                                       __FUNCTION__);
+                               " error during vsnprintf.\n", __FUNCTION__);
                        exit(EXIT_FAILURE);
                }
 
@@ -43,7 +41,7 @@ sprintf_alloc(char **str, const char *fmt, ...)
                        break;
 
                /* Truncated, try again with more space. */
-               size = n+1;
+               size = n + 1;
                *str = xrealloc(*str, size);
        }
 }