mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Alex Brachet-Mialot <alexbrachetmialot@gmail.com>
To: musl@lists.openwall.com
Subject: Re: [PATCH] Use open_memstream(3) for more efficient asprintf
Date: Mon, 14 Oct 2019 02:56:36 -0400	[thread overview]
Message-ID: <CAOt2X9spaSkH_U3TS-pXtnFQsXdq3AVVjLwLbJu0Sq3E4D=Rog@mail.gmail.com> (raw)
In-Reply-To: <CAOt2X9uNdorAtmWc6jDT+dVLf1neu5at+9LMrrfBAB20e9dzGQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1222 bytes --]

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@gmail.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!

[-- Attachment #2: asprintf.txt --]
[-- Type: text/plain, Size: 575 bytes --]

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;
 }

  reply	other threads:[~2019-10-14  6:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-14  6:48 Alex Brachet-Mialot
2019-10-14  6:56 ` Alex Brachet-Mialot [this message]
2019-10-14 12:12   ` Rich Felker
2019-10-14 12:07 ` Rich Felker
2019-10-14 13:57   ` Micha Nelissen
2019-10-14 14:23     ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOt2X9spaSkH_U3TS-pXtnFQsXdq3AVVjLwLbJu0Sq3E4D=Rog@mail.gmail.com' \
    --to=alexbrachetmialot@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).