From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1792) id 970F83858D1E; Tue, 20 Jun 2023 17:15:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 970F83858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687281320; bh=6BMvjFpdhbTeYuPEpnK0Wyt3CCAm1NMhjKJp8Pwrgdo=; h=From:To:Subject:Date:From; b=LX2mK+jbJGD2g6nAtp+bsH4XPli3rUdxbw5G0Jdqx+1oH80Up3gbwd4HyPTzLHDSF P1gRwFTvpAC3J2303fkBiYSJg5JjB/ZGvhK9Lz2jj9wFRmtNDNdIiwJEN1YQjqGrzY +Bl/xn4IviolsL4mJ7sISyf5Arn46U0QrAJ7hp0Y= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Samuel Thibault To: glibc-cvs@sourceware.org Subject: [glibc] hurd: readv: Get rid of alloca X-Act-Checkin: glibc X-Git-Author: Joe Simmons-Talbott X-Git-Refname: refs/heads/master X-Git-Oldrev: c6957bddb939a1a602824b9fa731fc45fb4a6d8c X-Git-Newrev: 9e6863a537e66e01f5819dc356c5405a2bc67dc7 Message-Id: <20230620171520.970F83858D1E@sourceware.org> Date: Tue, 20 Jun 2023 17:15:20 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=9e6863a537e66e01f5819dc356c5405a2bc67dc7 commit 9e6863a537e66e01f5819dc356c5405a2bc67dc7 Author: Joe Simmons-Talbott Date: Mon Jun 19 10:43:34 2023 -0400 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> Diff: --- sysdeps/posix/readv.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/sysdeps/posix/readv.c b/sysdeps/posix/readv.c index 91208e9894..9cdf571787 100644 --- a/sysdeps/posix/readv.c +++ b/sysdeps/posix/readv.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -26,9 +27,9 @@ 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 +53,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);