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