mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] Use open_memstream(3) for more efficient asprintf
@ 2019-10-14  6:48 Alex Brachet-Mialot
  2019-10-14  6:56 ` Alex Brachet-Mialot
  2019-10-14 12:07 ` Rich Felker
  0 siblings, 2 replies; 6+ messages in thread
From: Alex Brachet-Mialot @ 2019-10-14  6:48 UTC (permalink / raw)
  To: musl


[-- Attachment #1.1: Type: text/plain, Size: 236 bytes --]

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 #1.2: Type: text/html, Size: 281 bytes --]

[-- Attachment #2: asprintf.patch --]
[-- Type: application/octet-stream, 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;
 }

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-10-14 14:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-14  6:48 [PATCH] Use open_memstream(3) for more efficient asprintf Alex Brachet-Mialot
2019-10-14  6:56 ` Alex Brachet-Mialot
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

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).