mirror of
https://sourceware.org/git/glibc.git
synced 2025-08-08 17:42:12 +03:00
hurd: writev: Get rid of alloca
Use a scratch_buffer rather than alloca to avoid potential stack overflows. Checked on i686-gnu and x86_64-linux-gnu Message-Id: <20230608155844.976554-1-josimmon@redhat.com>
This commit is contained in:
committed by
Samuel Thibault
parent
01dd2875f8
commit
cf30aa43a5
@@ -19,19 +19,13 @@
|
|||||||
#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>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
ifree (char **ptrp)
|
|
||||||
{
|
|
||||||
free (*ptrp);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Write data pointed by the buffers described by VECTOR, which
|
/* Write data pointed by the buffers described by VECTOR, which
|
||||||
is a vector of COUNT 'struct iovec's, to file descriptor FD.
|
is a vector of COUNT 'struct iovec's, to file descriptor FD.
|
||||||
The data is written in the order specified.
|
The data is written in the order specified.
|
||||||
@@ -53,22 +47,17 @@ __writev (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 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);
|
/* XXX I don't know whether it is acceptable to try writing
|
||||||
else
|
the data in chunks. Probably not so we just fail here. */
|
||||||
{
|
return -1;
|
||||||
malloced_buffer = buffer = (char *) malloc (bytes);
|
char *buffer = buf.data;
|
||||||
if (buffer == NULL)
|
|
||||||
/* XXX I don't know whether it is acceptable to try writing
|
|
||||||
the data in chunks. Probably not so we just fail here. */
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Copy the data into BUFFER. */
|
/* Copy the data into BUFFER. */
|
||||||
size_t to_copy = bytes;
|
size_t to_copy = bytes;
|
||||||
@@ -86,6 +75,8 @@ __writev (int fd, const struct iovec *vector, int count)
|
|||||||
|
|
||||||
ssize_t bytes_written = __write (fd, buffer, bytes);
|
ssize_t bytes_written = __write (fd, buffer, bytes);
|
||||||
|
|
||||||
|
scratch_buffer_free (&buf);
|
||||||
|
|
||||||
return bytes_written;
|
return bytes_written;
|
||||||
}
|
}
|
||||||
libc_hidden_def (__writev)
|
libc_hidden_def (__writev)
|
||||||
|
Reference in New Issue
Block a user