From patchwork Thu Jun 8 15:58:43 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: 70794 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 372833858C2F for ; Thu, 8 Jun 2023 15:59:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 372833858C2F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686239951; bh=sKkOrdJL2Zrpz8yMF78HbW/e1LuzyalZXObnFqtHihc=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=k6ut2Cqqm4ZLHXqJpn7y8qK0fjDvHlSFgv1s01dDam2Tfh9hi1UHkOa8FnHSaDa45 gxJT6NdLWOsPdlqFT0Z2mdS/J3ANTN9HbKviVuUOo7x70HyBWJkZU8FNRfqhuAcjag Jd7wkn0NQRFM3mQ698Q4k6LY558ftEZLKoIlFyoA= 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 7CB963858C62 for ; Thu, 8 Jun 2023 15:58:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7CB963858C62 Received: from mail-vk1-f200.google.com (mail-vk1-f200.google.com [209.85.221.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-637-zQBL9c9zPtGpxSXvAiAvDw-1; Thu, 08 Jun 2023 11:58:48 -0400 X-MC-Unique: zQBL9c9zPtGpxSXvAiAvDw-1 Received: by mail-vk1-f200.google.com with SMTP id 71dfb90a1353d-464b34d1c04so97225e0c.3 for ; Thu, 08 Jun 2023 08:58:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686239927; x=1688831927; 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=sKkOrdJL2Zrpz8yMF78HbW/e1LuzyalZXObnFqtHihc=; b=J3MdLU3OEuz+8V77XI4E7zmbKtNXn/TNW/FnzD4BoWmYsrukydaWV63na4aijmg+xp IWnw92beHMqmvW8FZQka23nYrPzUOIRHxlxHswBiFqyvCvuU4r46wbECxRbDP/JahYDA 2bpVCulzbETzOdki1J86Fx2tyLk2trVaJJHsHIvSZoepRqqh/Z8IGukykEoHO8But0mW loNrBj37gPwgrzZsvYEVGepMXbEIEbEIAOSjSfw+zrLqRuypk0c8DOjE6gVnUtimmCA6 Jy0fWd/tc0CW4/vihGr+pHk2FySyIa/4RL3Wvn69dt6dqbrqBjR8YiZHo/BbkdisTBFe ngUw== X-Gm-Message-State: AC+VfDx5hrFJT1hBSAZJ6Hjl1FVts0BGRtiZxZZijeGY7khzeGWHFjwU 6IRr4yem5e3R+yMCB+nhuHD1a7/HyPjwurXjEDHgKotovcVBZiyqfxtMjGFVeYJzdNbaOwLCiMX VYrgtwUnD/+bBUzsuffH9ekQbt/nczw/Zkikt97OfgWQdyhZ+/tOXhtnbjpdiGE+xmi1JEAlpUc JgKgvo X-Received: by 2002:a67:ebd5:0:b0:43d:c1a4:c166 with SMTP id y21-20020a67ebd5000000b0043dc1a4c166mr1582419vso.13.1686239926998; Thu, 08 Jun 2023 08:58:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4TvDB/epKgPebnwjb4YrsUf5L+eYs6smzNqxNdfR2HKFvGWt5W80Hg8Bbv4pmokM30Ptlx5g== X-Received: by 2002:a67:ebd5:0:b0:43d:c1a4:c166 with SMTP id y21-20020a67ebd5000000b0043dc1a4c166mr1582409vso.13.1686239926696; Thu, 08 Jun 2023 08:58:46 -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 y18-20020a0cd992000000b0061b2a2f949bsm486071qvj.61.2023.06.08.08.58.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 08:58:46 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH] hurd: writev: Get rid of alloca Date: Thu, 8 Jun 2023 11:58:43 -0400 Message-Id: <20230608155844.976554-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. Checked on i686-gnu and x86_64-linux-gnu --- 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)