blob: 7853c1afa03cfa126f639fc1e0ce4031ce77021b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
Reduce the initial buffer size for open_memstream (used by vasprintf),
as most strings are usually smaller than that.
Realloc the buffer after finishing the string to further reduce size.
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
--- a/libc/stdio/vasprintf.c
+++ b/libc/stdio/vasprintf.c
@@ -39,6 +39,8 @@ int vasprintf(char **__restrict buf, con
if (rv < 0) {
free(*buf);
*buf = NULL;
+ } else {
+ *buf = realloc(*buf, rv + 1);
}
}
--- a/libc/stdio/open_memstream.c
+++ b/libc/stdio/open_memstream.c
@@ -17,6 +17,8 @@
#define COOKIE ((__oms_cookie *) cookie)
+#define MEMSTREAM_BUFSIZ 256
+
typedef struct {
char *buf;
size_t len;
@@ -134,7 +136,7 @@ FILE *open_memstream(char **__restrict b
register FILE *fp;
if ((cookie = malloc(sizeof(__oms_cookie))) != NULL) {
- if ((cookie->buf = malloc(cookie->len = BUFSIZ)) == NULL) {
+ if ((cookie->buf = malloc(cookie->len = MEMSTREAM_BUFSIZ)) == NULL) {
goto EXIT_cookie;
}
*cookie->buf = 0; /* Set nul terminator for buffer. */
|