From patchwork Mon Sep 25 18:48:38 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: 76656 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 BE02A385696A for ; Mon, 25 Sep 2023 18:49:06 +0000 (GMT) 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 C2ADA3858D3C for ; Mon, 25 Sep 2023 18:48:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C2ADA3858D3C 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=1695667732; 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=NwKgCpKHtoMVMx/Sl/ngOz2J9AyzpzxDENLG7TypRfk=; b=NCSlHwy+r7iG0T8tb0rKR0CQ6FwvzcHtnNOzjiYkUFdbcipSQEtEEFNGHLKsOnXq4UMamN RG8fWQMU7gxY26pcZP882KMUWbzdwckxBYrFmp5HNmUpWgadFWkNUeMrfw5J5egPSZIW5g g9F+NkuYW37DuYDULpMlE1iGXP0Kzfw= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-494-tZzGiN5lOauh9xgx2k5Odw-1; Mon, 25 Sep 2023 14:48:50 -0400 X-MC-Unique: tZzGiN5lOauh9xgx2k5Odw-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-41213943314so88095361cf.0 for ; Mon, 25 Sep 2023 11:48:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695667729; x=1696272529; 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=NwKgCpKHtoMVMx/Sl/ngOz2J9AyzpzxDENLG7TypRfk=; b=p+2rpFm/LVpEHYqNxcUVOyOOoTkVYgK2XJx8xfyUpHfKBYW3x6YW+pajUSpHGVp6tB ysRelfMxeYTKqU9qHVKT38LfeCFO3RO2axxDVMJyuUrjPwqyTA1/iWjcyY5SRRXEHTTm qYHbv4EdrjMI9wQSPoDwXMx+eH9wv6t926WNFazAlSpeopgJm6lAHmsBLVoIjj7X/B39 ZKg/TusDEmcmLyFlN18c6fHvHxDxLRvhS32hej/nZbeSubZTgu43flhyu3Aw6vLcZ0b2 QFu2V3v00JKJY/BsIyisqODpOcxF4kgmfIpph9kOH1y3ZMDExWAuQtWpywdM/R9msXC2 y/HA== X-Gm-Message-State: AOJu0YxzuuxksGpEFMXOG0t/T+WRR0JQoe9jjgAfoEf1Zs8c/gZOhbYP trEt5xz7bKGZQZhbXuX7LreOJKok0WEOC0RZ55iMmT3mjiENVhVPTLrq5/2RQsAOnFvTQxCp8Oq BxtJcKYIUji7t2fXaYOn8BYjz/yswV+HBM6SLsU/6z0AJXpx3p344Cf9dhkqW+FMgZcm5xToJcY C9i/Qc X-Received: by 2002:ac8:5acc:0:b0:417:c16a:c2c9 with SMTP id d12-20020ac85acc000000b00417c16ac2c9mr8625571qtd.62.1695667729289; Mon, 25 Sep 2023 11:48:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGT1YCqHK4T9UtdAS42WgvCIebisf+vgxp8yp9iN49pDQ/3x8feaz2YqLNsro90y5yFnMihVQ== X-Received: by 2002:ac8:5acc:0:b0:417:c16a:c2c9 with SMTP id d12-20020ac85acc000000b00417c16ac2c9mr8625551qtd.62.1695667728922; Mon, 25 Sep 2023 11:48:48 -0700 (PDT) Received: from oak.. (c-71-206-142-238.hsd1.va.comcast.net. [71.206.142.238]) by smtp.gmail.com with ESMTPSA id gb6-20020a05622a598600b004108bebd1fcsm3971214qtb.24.2023.09.25.11.48.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 11:48:48 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH] getopt: Get rid of alloca usage in process_long_option Date: Mon, 25 Sep 2023 14:48:38 -0400 Message-ID: <20230925184846.4096885-1-josimmon@redhat.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.7 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Replace alloca usage with a scratch_buffer. --- posix/getopt.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/posix/getopt.c b/posix/getopt.c index 1e2441c4af..a65e85b3f9 100644 --- a/posix/getopt.c +++ b/posix/getopt.c @@ -23,6 +23,7 @@ #include "getopt.h" +#include #include #include #include @@ -50,10 +51,6 @@ # define flockfile(fp) /* nop */ # define funlockfile(fp) /* nop */ # endif -/* When used standalone, do not attempt to use alloca. */ -# define __libc_use_alloca(size) 0 -# undef alloca -# define alloca(size) (abort (), (void *)0) #endif /* This implementation of 'getopt' has three modes for handling @@ -223,9 +220,10 @@ process_long_option (int argc, char **argv, const char *optstring, { /* Didn't find an exact match, so look for abbreviations. */ unsigned char *ambig_set = NULL; - int ambig_malloced = 0; int ambig_fallback = 0; int indfound = -1; + struct scratch_buffer sbuf; + scratch_buffer_init (&sbuf); for (p = longopts, option_index = 0; p->name; p++, option_index++) if (!strncmp (p->name, d->__nextchar, namelen)) @@ -250,13 +248,14 @@ process_long_option (int argc, char **argv, const char *optstring, ambig_fallback = 1; else if (!ambig_set) { - if (__libc_use_alloca (n_options)) - ambig_set = alloca (n_options); - else if ((ambig_set = malloc (n_options)) == NULL) - /* Fall back to simpler error message. */ - ambig_fallback = 1; + if (!scratch_buffer_set_array_size ( + &sbuf, 1, n_options)) + { + ambig_fallback = 1; + ambig_set = NULL; + } else - ambig_malloced = 1; + ambig_set = sbuf.data; if (ambig_set) { @@ -296,8 +295,7 @@ process_long_option (int argc, char **argv, const char *optstring, funlockfile (stderr); } } - if (ambig_malloced) - free (ambig_set); + scratch_buffer_free (&sbuf); d->__nextchar += strlen (d->__nextchar); d->optind++; d->optopt = 0;