From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 07D153858D32 for ; Mon, 19 Jun 2023 14:13:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 07D153858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687184011; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=9PQy7NjoaffLvLjde5Vw0Xs6FDAiUGDWWdG329moEJ0=; b=EJuXYuzIZldyYIWkGNgY9+C+9TQhen2RIT82lNBJfHuwAAgkEHg2pv2nSausNjFlzvhRMO 28dNts4Rb+wkh7afkYMDT9DNT1yatS7tWuYjfBWmXKdv2H2RcU/jkiy/jGNC6nvXSx3+Ov 3Nwyg0jIArXmOP3gtjvkuJP0qJMaFGQ= Received: from mail-vs1-f69.google.com (mail-vs1-f69.google.com [209.85.217.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-66-DAXxZV7_MKO6NSu5S85IXQ-1; Mon, 19 Jun 2023 10:13:30 -0400 X-MC-Unique: DAXxZV7_MKO6NSu5S85IXQ-1 Received: by mail-vs1-f69.google.com with SMTP id ada2fe7eead31-440a69620c7so135926137.2 for ; Mon, 19 Jun 2023 07:13:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687184009; x=1689776009; 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=9PQy7NjoaffLvLjde5Vw0Xs6FDAiUGDWWdG329moEJ0=; b=lO8cQ9+5qUg+IhqVuvX7vQRJD26bF+Fn6fw1wmB/HhvIArKr1jlZ/jWzj0MrYZiObB AUMWu/qipNosbYB3ZKxxhoKeh6UXrKgwyME6ZS/Dn1Aubb8CbAmt1KZ5/QHmupeXSXMd TIp735c0Qo1mDUjunnzmU6P3URPx2PjZI/CXO+f53n7uauHtcgQqivwR4r2hSTSLZ7BF sev7+1jn/3hRLAgYc2r87aRZZz916bHyTpVDyGqOioW1q2ChfoS8S3Ut8kZunedZ8y7J Orxhsck80lxQp+ga+CZC+YqBHhxh2HGhX5rSAuMKCbPgCGopNAOgZs+gpaRpkVpI9wuR +XaQ== X-Gm-Message-State: AC+VfDy4WvR6IiIWJPfeDYIrXFr8Wq8/8shHmbF68ibqv4MkOaFflk4l l6EypYB2MwBq5XjDxh96wD4v5K8DWu57vbfkyd/KThte18yTL+lL2xDUgoYO4CkN884dxOoRTbH 8dnea8uxo6/NYXzVsl+lHpUrAjmHSzXRNqBCP3PuQLipfLVasTgkQcaXJlZ+ePyLBx2KhlGw3aR y7lozZ X-Received: by 2002:a67:f8cd:0:b0:440:b33b:e7ea with SMTP id c13-20020a67f8cd000000b00440b33be7eamr649326vsp.19.1687184009171; Mon, 19 Jun 2023 07:13:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ66V2pFqS6vhU0Z5JcmsHWGlq/FpLKol57KlAP0jZhxZqsV6YgIXJaeL9pkFlTOqj3g1ymuyQ== X-Received: by 2002:a67:f8cd:0:b0:440:b33b:e7ea with SMTP id c13-20020a67f8cd000000b00440b33be7eamr649303vsp.19.1687184008762; Mon, 19 Jun 2023 07:13:28 -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 ge10-20020a05621427ca00b0062629cbff10sm10440956qvb.114.2023.06.19.07.13.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jun 2023 07:13:28 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v2] hurd: readv: Get rid of alloca Date: Mon, 19 Jun 2023 10:13:25 -0400 Message-Id: <20230619141325.2324846-1-josimmon@redhat.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,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 List-Id: 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 #include #include +#include #include #include #include #include - 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