hurd: readv: Get rid of alloca

Replace alloca with a scratch_buffer to avoid potential stack overflows.

Checked on i686-gnu and x86_64-linux-gnu
Message-Id: <20230619144334.2902429-1-josimmon@redhat.com>
This commit is contained in:
Joe Simmons-Talbott 2023-06-19 10:43:34 -04:00 committed by Samuel Thibault
parent c6957bddb9
commit 9e6863a537

View file

@ -19,6 +19,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#include <scratch_buffer.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/uio.h> #include <sys/uio.h>
@ -26,9 +27,9 @@
static void static void
ifree (char **ptrp) ifree (struct scratch_buffer *sbuf)
{ {
free (*ptrp); scratch_buffer_free (sbuf);
} }
/* Read data from file descriptor FD, and put the result in the /* Read data from file descriptor FD, and put the result in the
@ -52,20 +53,15 @@ __readv (int fd, const struct iovec *vector, int count)
bytes += vector[i].iov_len; bytes += vector[i].iov_len;
} }
/* Allocate a temporary buffer to hold the data. We should normally /* Allocate a temporary buffer to hold the data. Use a scratch_buffer
use alloca since it's faster and does not require synchronization since it's faster for small buffer sizes but can handle larger
with other threads. But we cannot if the amount of memory allocations as well. */
required is too large. */
char *buffer; struct scratch_buffer __attribute__ ((__cleanup__ (ifree))) buf;
char *malloced_buffer __attribute__ ((__cleanup__ (ifree))) = NULL; scratch_buffer_init (&buf);
if (__libc_use_alloca (bytes)) if (!scratch_buffer_set_array_size (&buf, 1, bytes))
buffer = (char *) __alloca (bytes); return -1;
else char *buffer = buf.data;
{
malloced_buffer = buffer = (char *) malloc (bytes);
if (buffer == NULL)
return -1;
}
/* Read the data. */ /* Read the data. */
ssize_t bytes_read = __read (fd, buffer, bytes); ssize_t bytes_read = __read (fd, buffer, bytes);