Re: [PATCH] Use open_memstream(3) for more efficient asprintf

Submitted by Alex Brachet-Mialot on Oct. 14, 2019, 6:56 a.m.

Details

Message ID CAOt2X9spaSkH_U3TS-pXtnFQsXdq3AVVjLwLbJu0Sq3E4D=Rog@mail.gmail.com
State New
Series "Re: [PATCH] Use open_memstream(3) for more efficient asprintf"
Headers show

Commit Message

Alex Brachet-Mialot Oct. 14, 2019, 6:56 a.m.
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.



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

Patch hide | download patch | download mbox

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

Comments

Rich Felker Oct. 14, 2019, 12:12 p.m.
On Mon, Oct 14, 2019 at 02:56:36AM -0400, Alex Brachet-Mialot wrote:
> 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.

It came through ok -- the attachment was just marked as a binary file
rather than text. If your mailer can send text attachments without
mangling the content, it's preferable to send as text so that
archive/recipients' mail software will show them, but either is okay
and binary (application/octet-streaam) can be better if you're at all
worried that your mailer will mess things up, e.g. replacing tabs with
spaces.

Rich