From patchwork Wed Sep 13 16:00: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: 75884 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 AC5B03857715 for ; Wed, 13 Sep 2023 16:01: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 CFEF43858D20 for ; Wed, 13 Sep 2023 16:00:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CFEF43858D20 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=1694620852; 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=9FHjj2VnD6GmzdZ8sURmtCf1ZiOCeTkazucUk6CUpME=; b=TyLchnwzGucAd6mw3kHgAGsU6rSmp7k7BTTL2gaEl8ym+uYSfUeTYYi7glxRiA9+nrVxqd Wcd1FoVbGhooK5Gfu/+kzGOQjpDOyKWGn74LicL8IPdl+dfqzexP7wV0weQyMLrLn8UjPe kgBzldNO0ToypgS4g7X9nTP0GktSo3s= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-562-elc1CAncPoOaYru-AZpQ7g-1; Wed, 13 Sep 2023 12:00:50 -0400 X-MC-Unique: elc1CAncPoOaYru-AZpQ7g-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-64ab5892bbcso121876d6.0 for ; Wed, 13 Sep 2023 09:00:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694620850; x=1695225650; 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=u8SXBhye1v8FroJrtbeE1Eo9bzI1bBTmMgMW7ZYETCw=; b=BAOAl0lLt9N4NkdmSjTUANKvv0vKA7qtBgC2IZRoTY9QYVdak76op8kHqVOPun7eiM E4LZ2Dfq3fzxeloFb/Tn9ZgnxnF7JMm/6/TtXc15vp9+l+FyLkVEKj/kjnB9NDNjETeq PUOZNlPt9UTVhH0upFbJ3yMkmZcUb6K6l795B7hLc2uSCRA1JiKBsHZWdvn5SBs8PDXV IBc3DP4OsVcOzb6mWh8km8NK2Sn9vbPViSKEXnmpXzrSuGqw1I3Au16anF1LYh+hhuA7 H9rk6mrZe9qhaaDSJ0RJOtSPUkC1BkYnv+9MsKD/npuOge7mk9YAMEPB3ADZv+dWu1iV BITg== X-Gm-Message-State: AOJu0YxqCyAb+Ab625VnvlLRXQuRHjlp6aBSLbJnWI1ruVkSwoMeNtlS hD8ypCbsy5U2ooekuEsg3y7NMXzirSFqzrjZonO9JeQN4JTa6ukw0VzPeLnjDS6F4dtrJK1QCnb tGfF7OJ0Rrpy6yM16MmkBfmRLfZs74ZyACQqi3AI019367bn0oR78qyjka0a6rHmPHCDsRuS4s7 J0QNvZ X-Received: by 2002:a0c:f2cd:0:b0:655:8ac4:2658 with SMTP id c13-20020a0cf2cd000000b006558ac42658mr3510137qvm.21.1694620849993; Wed, 13 Sep 2023 09:00:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEkWlnj9dZ0DOLi//alTwNl8p0/ryPwsIXc39Smrh47vSQsDif4wzSLndTHOMQT01h5pVcrYg== X-Received: by 2002:a0c:f2cd:0:b0:655:8ac4:2658 with SMTP id c13-20020a0cf2cd000000b006558ac42658mr3510110qvm.21.1694620849639; Wed, 13 Sep 2023 09:00:49 -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 f15-20020a05620a280f00b007713659adeasm1969979qkp.63.2023.09.13.09.00.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 09:00:49 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v3] argp-help: Get rid of alloca. Date: Wed, 13 Sep 2023 12:00:14 -0400 Message-ID: <20230913160042.326350-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=-11.7 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, 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 with malloc and a scratch_buffer to avoid potential stack overflow. Checked on x86_64-linux-gnu --- argp/argp-help.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/argp/argp-help.c b/argp/argp-help.c index d019ed58d2..54a15a8233 100644 --- a/argp/argp-help.c +++ b/argp/argp-help.c @@ -25,21 +25,7 @@ #include #endif -/* AIX requires this to be the first thing in the file. */ -#ifndef __GNUC__ -# if HAVE_ALLOCA_H || defined _LIBC -# include -# else -# ifdef _AIX -#pragma alloca -# else -# ifndef alloca /* predefined by HP cc +Olibcalls */ -char *alloca (); -# endif -# endif -# endif -#endif - +#include #include #include #include @@ -1450,8 +1436,14 @@ hol_usage (struct hol *hol, argp_fmtstream_t stream) { unsigned nentries; struct hol_entry *entry; - char *short_no_arg_opts = alloca (strlen (hol->short_options) + 1); - char *snao_end = short_no_arg_opts; + char *short_no_arg_opts; + char *snao_end; + + short_no_arg_opts = malloc (strlen (hol->short_options) + 1); + assert (short_no_arg_opts != NULL); + + snao_end = short_no_arg_opts; + /* First we put a list of short options without arguments. */ for (entry = hol->entries, nentries = hol->num_entries @@ -1478,6 +1470,8 @@ hol_usage (struct hol *hol, argp_fmtstream_t stream) ; entry++, nentries--) hol_entry_long_iterate (entry, usage_long_opt, entry->argp->argp_domain, stream); + + free (short_no_arg_opts); } } @@ -1698,7 +1692,15 @@ _help (const struct argp *argp, const struct argp_state *state, FILE *stream, { int first_pattern = 1, more_patterns; size_t num_pattern_levels = argp_args_levels (argp); - char *pattern_levels = alloca (num_pattern_levels); + struct scratch_buffer buf; + scratch_buffer_init (&buf); + char *pattern_levels; + bool result; + + result = scratch_buffer_set_array_size (&buf, 1, num_pattern_levels); + assert (result); + + pattern_levels = buf.data; memset (pattern_levels, 0, num_pattern_levels); @@ -1746,6 +1748,8 @@ _help (const struct argp *argp, const struct argp_state *state, FILE *stream, first_pattern = 0; } while (more_patterns); + + scratch_buffer_free (&buf); } if (flags & ARGP_HELP_PRE_DOC)