Openwall GNU/*/Linux - a small security-enhanced Linux distro for servers
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 14 Oct 2019 02:56:36 -0400
From: Alex Brachet-Mialot <alexbrachetmialot@...il.com>
To: musl@...ts.openwall.com
Subject: Re: [PATCH] Use open_memstream(3) for more efficient asprintf

That's strange, I looked on the archives and it looks like my
attachment was in a weird format, something my email client did
perhaps. Here it is copied I'll also try again with .txt extension so
my mail client is less confused hopefully. Sorry about that.

diff --git a/src/stdio/vasprintf.c b/src/stdio/vasprintf.c
index 08251bc2..d55fe32f 100644
--- a/src/stdio/vasprintf.c
+++ b/src/stdio/vasprintf.c
@@ -5,11 +5,16 @@

 int vasprintf(char **s, const char *fmt, va_list ap)
 {
-    va_list ap2;
-    va_copy(ap2, ap);
-    int l = vsnprintf(0, 0, fmt, ap2);
-    va_end(ap2);
+    size_t l;
+    *s = 0;
+    FILE *f = open_memstream(s, &l);
+    if (!f)
+        return -1;

-    if (l<0 || !(*s=malloc(l+1U))) return -1;
-    return vsnprintf(*s, l+1U, fmt, ap);
+    if ((l = vfprintf(f, fmt, ap)) == -1) {
+        free(*s);
+        *s = 0;
+    }
+    fclose(f);
+    return l;
 }


On Mon, Oct 14, 2019 at 2:48 AM Alex Brachet-Mialot
<alexbrachetmialot@...il.com> wrote:
>
> Hi I wasn't able to search the lists from the online archive, so I'm not sure if it has been talked about yet, but the current vasprintf implementation could be made better if it didn't call vsnprintf twice. Let me know what you think!

View attachment "asprintf.txt" of type "text/plain" (575 bytes)

Powered by blists - more mailing lists

Your e-mail address:

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.