gcc: backport a libgcc fix for a dependency on libc write() on ARM
[openwrt.git] / toolchain / musl / patches / 020-upstream_open_memstream.patch
1 From 7b9f57f207b51132f188f750161953b7baf32154 Mon Sep 17 00:00:00 2001
2 From: Rich Felker <dalias@aerifal.cx>
3 Date: Thu, 8 Oct 2015 22:03:53 +0000
4 Subject: fix open_[w]memstream behavior when no writes take place
5
6 the specification for these functions requires that the buffer/size
7 exposed to the caller be valid after any successful call to fflush or
8 fclose on the stream. the implementation's approach is to update them
9 only at flush time, but that misses the case where fflush or fclose is
10 called without any writes having taken place, in which case the write
11 flushing callback will not be called.
12
13 to fix both the observable bug and the desired invariant, setup empty
14 buffers at open time and fail the open operation if no memory is
15 available.
16 ---
17  src/stdio/open_memstream.c  | 11 +++++++++--
18  src/stdio/open_wmemstream.c | 11 +++++++++--
19  2 files changed, 18 insertions(+), 4 deletions(-)
20
21 diff --git a/src/stdio/open_memstream.c b/src/stdio/open_memstream.c
22 index 58504c9..eab024d 100644
23 --- a/src/stdio/open_memstream.c
24 +++ b/src/stdio/open_memstream.c
25 @@ -59,14 +59,21 @@ FILE *open_memstream(char **bufp, size_t *sizep)
26  {
27         FILE *f;
28         struct cookie *c;
29 +       char *buf;
30 +
31         if (!(f=malloc(sizeof *f + sizeof *c + BUFSIZ))) return 0;
32 +       if (!(buf=malloc(sizeof *buf))) {
33 +               free(f);
34 +               return 0;
35 +       }
36         memset(f, 0, sizeof *f + sizeof *c);
37         f->cookie = c = (void *)(f+1);
38  
39         c->bufp = bufp;
40         c->sizep = sizep;
41 -       c->pos = c->len = c->space = 0;
42 -       c->buf = 0;
43 +       c->pos = c->len = c->space = *sizep = 0;
44 +       c->buf = *bufp = buf;
45 +       *buf = 0;
46  
47         f->flags = F_NORD;
48         f->fd = -1;
49 diff --git a/src/stdio/open_wmemstream.c b/src/stdio/open_wmemstream.c
50 index 7ab2c64..4d90cd9 100644
51 --- a/src/stdio/open_wmemstream.c
52 +++ b/src/stdio/open_wmemstream.c
53 @@ -61,14 +61,21 @@ FILE *open_wmemstream(wchar_t **bufp, size_t *sizep)
54  {
55         FILE *f;
56         struct cookie *c;
57 +       wchar_t *buf;
58 +
59         if (!(f=malloc(sizeof *f + sizeof *c))) return 0;
60 +       if (!(buf=malloc(sizeof *buf))) {
61 +               free(f);
62 +               return 0;
63 +       }
64         memset(f, 0, sizeof *f + sizeof *c);
65         f->cookie = c = (void *)(f+1);
66  
67         c->bufp = bufp;
68         c->sizep = sizep;
69 -       c->pos = c->len = c->space = 0;
70 -       c->buf = 0;
71 +       c->pos = c->len = c->space = *sizep = 0;
72 +       c->buf = *bufp = buf;
73 +       *buf = 0;
74  
75         f->flags = F_NORD;
76         f->fd = -1;
77 -- 
78 cgit v0.11.2
79