* [PATCH v2] hurd: readv: Get rid of alloca
@ 2023-06-19 14:13 Joe Simmons-Talbott
2023-06-19 14:24 ` Joe Simmons-Talbott
0 siblings, 1 reply; 2+ messages in thread
From: Joe Simmons-Talbott @ 2023-06-19 14:13 UTC (permalink / raw)
To: libc-alpha; +Cc: Joe Simmons-Talbott
Replace alloca with a scratch_buffer to avoid potential stack overflows.
Checked on i686-gnu and x86_64-linux-gnu
---
Changes to v1:
* Add back the cleanup handler since this is a "shall occur"
cancellation point.
sysdeps/posix/readv.c | 29 ++++++++++++-----------------
1 file changed, 12 insertions(+), 17 deletions(-)
diff --git a/sysdeps/posix/readv.c b/sysdeps/posix/readv.c
index 91208e9894..fab35754b0 100644
--- a/sysdeps/posix/readv.c
+++ b/sysdeps/posix/readv.c
@@ -19,16 +19,16 @@
#include <unistd.h>
#include <string.h>
#include <limits.h>
+#include <scratch_buffer.h>
#include <stdbool.h>
#include <sys/param.h>
#include <sys/uio.h>
#include <errno.h>
-
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
@@ -52,20 +52,15 @@ __readv (int fd, const struct iovec *vector, int count)
bytes += vector[i].iov_len;
}
- /* Allocate a temporary buffer to hold the data. We should normally
- use alloca since it's faster and does not require synchronization
- with other threads. But we cannot if the amount of memory
- required is too large. */
- char *buffer;
- char *malloced_buffer __attribute__ ((__cleanup__ (ifree))) = NULL;
- if (__libc_use_alloca (bytes))
- buffer = (char *) __alloca (bytes);
- else
- {
- malloced_buffer = buffer = (char *) malloc (bytes);
- if (buffer == NULL)
- return -1;
- }
+ /* Allocate a temporary buffer to hold the data. Use a scratch_buffer
+ since it's faster for small buffer sizes but can handle larger
+ allocations as well. */
+
+ struct scratch_buffer __attribute__ ((__cleanup__ (ifree))) buf;
+ scratch_buffer_init (&buf);
+ if (!scratch_buffer_set_array_size (&buf, 1, bytes))
+ return -1;
+ char *buffer = buf.data;
/* Read the data. */
ssize_t bytes_read = __read (fd, buffer, bytes);
--
2.39.2
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] hurd: readv: Get rid of alloca
2023-06-19 14:13 [PATCH v2] hurd: readv: Get rid of alloca Joe Simmons-Talbott
@ 2023-06-19 14:24 ` Joe Simmons-Talbott
0 siblings, 0 replies; 2+ messages in thread
From: Joe Simmons-Talbott @ 2023-06-19 14:24 UTC (permalink / raw)
To: libc-alpha
On Mon, Jun 19, 2023 at 10:13:25AM -0400, Joe Simmons-Talbott wrote:
> Replace alloca with a scratch_buffer to avoid potential stack overflows.
>
> Checked on i686-gnu and x86_64-linux-gnu
> ---
> Changes to v1:
> * Add back the cleanup handler since this is a "shall occur"
> cancellation point.
>
> sysdeps/posix/readv.c | 29 ++++++++++++-----------------
> 1 file changed, 12 insertions(+), 17 deletions(-)
>
> diff --git a/sysdeps/posix/readv.c b/sysdeps/posix/readv.c
> index 91208e9894..fab35754b0 100644
> --- a/sysdeps/posix/readv.c
> +++ b/sysdeps/posix/readv.c
> @@ -19,16 +19,16 @@
> #include <unistd.h>
> #include <string.h>
> #include <limits.h>
> +#include <scratch_buffer.h>
> #include <stdbool.h>
> #include <sys/param.h>
> #include <sys/uio.h>
> #include <errno.h>
>
> -
> static void
> -ifree (char **ptrp)
> +ifree (struct scratch_buffer *sbuf)
> {
> - free (*ptrp);
> + scratch_buffer_free(sbuf);
> }
There are a couple of spacing issues here. I will send a v3 shortly.
Thanks,
Joe
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-06-19 14:24 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-19 14:13 [PATCH v2] hurd: readv: Get rid of alloca Joe Simmons-Talbott
2023-06-19 14:24 ` Joe Simmons-Talbott
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).