From patchwork Fri Jun 23 15:25:17 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: 1799067 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=B8o49GaC; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qnh0v1PDFz20Wk for ; Sat, 24 Jun 2023 01:26:39 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CA01E3857346 for ; Fri, 23 Jun 2023 15:26:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA01E3857346 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687533996; bh=IFcjxQnG4lUlA63K9yFFfOUNcVu/SoKSYeLDq1Izw0w=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=B8o49GaCNC1kzyGiSHETRHiRj2F5cVznnvk7gHcCINFRvR+v3daibnQcU0+LcNZhq BUPXOPwb3rqYJS+9himp3/Bu45L0xoLPubYwMC/OAJ3bETZ9ef6egLhOdH8dchTiRA xaSf2UzobTlfGVtRiKc8dk38Z64k7kfy7/VSny94= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 9737C3856968 for ; Fri, 23 Jun 2023 15:25:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9737C3856968 Received: from mail-vk1-f199.google.com (mail-vk1-f199.google.com [209.85.221.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-248-CdaeTKQjP5-BcsImHXe5JA-1; Fri, 23 Jun 2023 11:25:32 -0400 X-MC-Unique: CdaeTKQjP5-BcsImHXe5JA-1 Received: by mail-vk1-f199.google.com with SMTP id 71dfb90a1353d-4717494b129so200063e0c.0 for ; Fri, 23 Jun 2023 08:25:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687533920; x=1690125920; 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=IFcjxQnG4lUlA63K9yFFfOUNcVu/SoKSYeLDq1Izw0w=; b=fIM1nVEWOPI+I8ijoZQvRpESqWDn9YlqZnV8c1zXKixvEKCes5AgcJWPOWRj+kjCI/ 3CNlXbORpZQJpETJexF9GIpevSpCio51b9eiT/pue6P23maDl1UodMzIQS2cDO0ep0tC LVdwdXtosYwGNdsSJAEedc6mEhAaN08rYUak3adpI0JFER6nbhi4ihbaBitO0Xh5SUjv 3VCKxORRJXQAfch+nFB+Gg6FHObUoiJgcDX81aU55KTJZSOWM3vzGRiCjI+cEZXfaMyj cPvIkvchojGKeKs4gPDYLuZ9ilxYe99JBSXgzppgtD9DbY44kEVG8aK4q+CjQ+F78w2p avYg== X-Gm-Message-State: AC+VfDwC4CZ+FdHVacHY/+BrAP1asW2TrGpPz5wmgK3HriP3sdc684zz tTx6X9HjXJFoGxM90JgaoAHpqweowSso3z6Eqd3TspojgSBY1wlLs+yFsCm3h6wTSdf2EMKG8An DbS6z7jJb6iFe3iMNJL+yLqlBtq0mFT0ErSNGfYe+2U96cVDWj2/Lfs9Movq7zMXX8RUjUByNmV nv0CFM X-Received: by 2002:a1f:da87:0:b0:476:3544:773 with SMTP id r129-20020a1fda87000000b0047635440773mr1059453vkg.11.1687533919959; Fri, 23 Jun 2023 08:25:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7jWJkH1bMKxpb7aJFzHB2n35dvwsGOqLUqPuWTT8Qh1XFQe27DnQUoEtETAkDCWvns+5/t4g== X-Received: by 2002:a1f:da87:0:b0:476:3544:773 with SMTP id r129-20020a1fda87000000b0047635440773mr1059436vkg.11.1687533919597; Fri, 23 Jun 2023 08:25:19 -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 e65-20020a0dc244000000b0056cffe97a11sm2525532ywd.13.2023.06.23.08.25.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 08:25:19 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH] setenv.c: Get rid of alloca. Date: Fri, 23 Jun 2023 11:25:17 -0400 Message-Id: <20230623152517.3268336-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 X-Spam-Status: No, score=-12.5 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 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+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Use a scratch_buffer rather than alloca to avoid potential stack overflow. --- stdlib/setenv.c | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/stdlib/setenv.c b/stdlib/setenv.c index ba5257d3bf..90bc30b219 100644 --- a/stdlib/setenv.c +++ b/stdlib/setenv.c @@ -44,6 +44,8 @@ extern int errno; # include #endif +#include + #if !_LIBC # define __environ environ # ifndef HAVE_ENVIRON_DECL @@ -182,19 +184,14 @@ __add_to_environ (const char *name, const char *value, const char *combined, { const size_t varlen = namelen + 1 + vallen; #ifdef USE_TSEARCH - char *new_value; - int use_alloca = __libc_use_alloca (varlen); - if (__builtin_expect (use_alloca, 1)) - new_value = (char *) alloca (varlen); - else + struct scratch_buffer buf; + scratch_buffer_init (&buf); + if (!scratch_buffer_set_array_size (&buf, 1, varlen)) { - new_value = malloc (varlen); - if (new_value == NULL) - { - UNLOCK; - return -1; - } + UNLOCK; + return -1; } + char *new_value = buf.data; # ifdef _LIBC __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1), value, vallen); @@ -209,18 +206,9 @@ __add_to_environ (const char *name, const char *value, const char *combined, #endif { #ifdef USE_TSEARCH - if (__glibc_unlikely (! use_alloca)) - np = new_value; - else + np = new_value; #endif { - np = malloc (varlen); - if (__glibc_unlikely (np == NULL)) - { - UNLOCK; - return -1; - } - #ifdef USE_TSEARCH memcpy (np, new_value, varlen); #else @@ -234,10 +222,7 @@ __add_to_environ (const char *name, const char *value, const char *combined, } #ifdef USE_TSEARCH else - { - if (__glibc_unlikely (! use_alloca)) - free (new_value); - } + scratch_buffer_free (&buf); #endif }