hurd: writev: Add back cleanup handler

Message ID 20230619143842.2901522-1-josimmon@redhat.com
State Committed
Commit c6957bddb939a1a602824b9fa731fc45fb4a6d8c
Headers
Series hurd: writev: Add back cleanup handler |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Testing passed
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Testing passed

Commit Message

Joe Simmons-Talbott June 19, 2023, 2:38 p.m. UTC
  There is a potential memory leak for large writes due to writev being a
"shall occur" cancellation point.  Add back the cleanup handler removed
in cf30aa43a5917f441c9438aaee201c53c8e1d76b.

Checked on i686-gnu and x86_64-linux-gnu.
---
 sysdeps/posix/writev.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
  

Comments

Samuel Thibault June 20, 2023, 4:36 p.m. UTC | #1
Applied, thanks!

Joe Simmons-Talbott via Libc-alpha, le lun. 19 juin 2023 10:38:42 -0400, a ecrit:
> There is a potential memory leak for large writes due to writev being a
> "shall occur" cancellation point.  Add back the cleanup handler removed
> in cf30aa43a5917f441c9438aaee201c53c8e1d76b.
> 
> Checked on i686-gnu and x86_64-linux-gnu.
> ---
>  sysdeps/posix/writev.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/sysdeps/posix/writev.c b/sysdeps/posix/writev.c
> index d4c3cf6f03..0c86e7ea5e 100644
> --- a/sysdeps/posix/writev.c
> +++ b/sysdeps/posix/writev.c
> @@ -26,6 +26,12 @@
>  #include <errno.h>
>  
>  
> +static void
> +ifree (struct scratch_buffer *sbuf)
> +{
> +   scratch_buffer_free (sbuf);
> +}
> +
>  /* Write data pointed by the buffers described by VECTOR, which
>     is a vector of COUNT 'struct iovec's, to file descriptor FD.
>     The data is written in the order specified.
> @@ -51,7 +57,7 @@ __writev (int fd, const struct iovec *vector, int count)
>       since it's faster for small buffer sizes but can handle larger
>       allocations as well.  */
>  
> -  struct scratch_buffer buf;
> +  struct scratch_buffer __attribute__ ((__cleanup__ (ifree))) buf;
>    scratch_buffer_init (&buf);
>    if (!scratch_buffer_set_array_size (&buf, 1, bytes))
>      /* XXX I don't know whether it is acceptable to try writing
> @@ -75,8 +81,6 @@ __writev (int fd, const struct iovec *vector, int count)
>  
>    ssize_t bytes_written = __write (fd, buffer, bytes);
>  
> -  scratch_buffer_free (&buf);
> -
>    return bytes_written;
>  }
>  libc_hidden_def (__writev)
> -- 
> 2.39.2
  

Patch

diff --git a/sysdeps/posix/writev.c b/sysdeps/posix/writev.c
index d4c3cf6f03..0c86e7ea5e 100644
--- a/sysdeps/posix/writev.c
+++ b/sysdeps/posix/writev.c
@@ -26,6 +26,12 @@ 
 #include <errno.h>
 
 
+static void
+ifree (struct scratch_buffer *sbuf)
+{
+   scratch_buffer_free (sbuf);
+}
+
 /* Write data pointed by the buffers described by VECTOR, which
    is a vector of COUNT 'struct iovec's, to file descriptor FD.
    The data is written in the order specified.
@@ -51,7 +57,7 @@  __writev (int fd, const struct iovec *vector, int count)
      since it's faster for small buffer sizes but can handle larger
      allocations as well.  */
 
-  struct scratch_buffer buf;
+  struct scratch_buffer __attribute__ ((__cleanup__ (ifree))) buf;
   scratch_buffer_init (&buf);
   if (!scratch_buffer_set_array_size (&buf, 1, bytes))
     /* XXX I don't know whether it is acceptable to try writing
@@ -75,8 +81,6 @@  __writev (int fd, const struct iovec *vector, int count)
 
   ssize_t bytes_written = __write (fd, buffer, bytes);
 
-  scratch_buffer_free (&buf);
-
   return bytes_written;
 }
 libc_hidden_def (__writev)