020-upstream_open_memstream.patch 2.2 KB

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