From patchwork Thu Jun 8 15:52:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Simmons-Talbott X-Patchwork-Id: 70793 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B508F3857019 for ; Thu, 8 Jun 2023 15:52:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B508F3857019 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686239563; bh=K+pwjkWmAG4m7N9Ys08vG0fjYPdABg9D9R5ZOR4SCmU=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=LOrrMJZzavVCVKuspnjo+nwM5vUbIsbhp9BUBeJj6Cf+yDkgBpAR0OV9l+R1WA0TP RMg8NPnDIlzUwknEdTb0JBPxzFhL650/G8QSsMJtBttGTHgX5hVyVPwOvCX1zp9mwr 8YoXckuoxQppdM5OARCFpH3ZmsvvlcPWRUje8QQs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id BA3FA3858C62 for ; Thu, 8 Jun 2023 15:52:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BA3FA3858C62 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-191-3IIKY17ON8uXRPdO7nMeiA-1; Thu, 08 Jun 2023 11:52:19 -0400 X-MC-Unique: 3IIKY17ON8uXRPdO7nMeiA-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-75d4d5eda29so161476685a.1 for ; Thu, 08 Jun 2023 08:52:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686239538; x=1688831538; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=K+pwjkWmAG4m7N9Ys08vG0fjYPdABg9D9R5ZOR4SCmU=; b=QSdzEgli+OgWlJF3LH/bTzRfmsNi7mKxbmP/C13V1SmLM1QByfwO5UUOWubd+Pmaym Qt0NlH3T+9VChdS/lZ45fYj2wLPg7z5ddjHpn3as1dnAiXAoH1fBe17x/X5Q4btCU5GL 2SniEuq95Q85CuNQ9ZQcjX03cOs+UTIw6CGDBvlaeuP8vKB5P6OIPx35E1bRfILhnj2s iXv/5ydlgU7ODfyXFwAH0hWlaXO3Rgj1b6kc5gXI1pVT7ArYmkxY/LYVv8wMphSbMFB3 nrdwoghbewBgbqsZW9b/Gx/rmmIru8/3ijHNX3AMra+rQwOTG5dldSWcl9eqJLEKcnId oKZQ== X-Gm-Message-State: AC+VfDxt4m2ws+zkCoFP1CJA9jXVln0mk7Zrh6gQe0wdTS/G7bqei+kf 9TSrhDALl5OS2ZyYDWkZXHrJtcqpK2MSteGbZB3L3tdt+75JwYfX2TpUww7ShrLSnysq9UKUpjY B18nS/03Jsxntkhy7WGa/n+i7fJdVjTrJnuwDSuMsw/zqFywtOg89B7ZsFNFTY+NOgZnSwIjAHV ARjgfV X-Received: by 2002:a05:620a:1d0e:b0:75b:23a1:d864 with SMTP id dl14-20020a05620a1d0e00b0075b23a1d864mr3001994qkb.38.1686239538200; Thu, 08 Jun 2023 08:52:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7q77KDrcZ1bHfYm6cIM6HsXf5B85F55dIkp//kKESEFBPFCyPCUu+I38Ty8Vs824l5DalrRw== X-Received: by 2002:a05:620a:1d0e:b0:75b:23a1:d864 with SMTP id dl14-20020a05620a1d0e00b0075b23a1d864mr3001972qkb.38.1686239537872; Thu, 08 Jun 2023 08:52:17 -0700 (PDT) Received: from oak.redhat.com (c-71-206-142-238.hsd1.va.comcast.net. [71.206.142.238]) by smtp.gmail.com with ESMTPSA id u6-20020a05620a120600b0074ca7c33b79sm421604qkj.23.2023.06.08.08.52.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 08:52:17 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH] wirtev: Get rid of alloca Date: Thu, 8 Jun 2023 11:52:14 -0400 Message-Id: <20230608155214.975683-1-josimmon@redhat.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Joe Simmons-Talbott via Libc-alpha From: Joe Simmons-Talbott Reply-To: Joe Simmons-Talbott Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Use a scratch_buffer rather than alloca to avoid potential stack overflows. --- sysdeps/posix/writev.c | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/sysdeps/posix/writev.c b/sysdeps/posix/writev.c index 53e090c087..0cee0aa692 100644 --- a/sysdeps/posix/writev.c +++ b/sysdeps/posix/writev.c @@ -19,19 +19,13 @@ #include #include #include +#include #include #include #include #include -static void -ifree (char **ptrp) -{ - free (*ptrp); -} - - /* 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. @@ -53,22 +47,15 @@ __writev (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) - /* 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; - } + /* 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 buf; + scratch_buffer_init (&buf); + if (!scratch_buffer_set_array_size (&buf, 1, bytes)) + return -1; + char *buffer = buf.data; /* Copy the data into BUFFER. */ size_t to_copy = bytes; @@ -86,6 +73,8 @@ __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)