From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 2571B3858D20 for ; Wed, 13 Sep 2023 19:45:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2571B3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6befdb1f545so114218a34.3 for ; Wed, 13 Sep 2023 12:45:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694634325; x=1695239125; darn=sourceware.org; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=0CSWIgf+eYsLKhM4nFKY23zezuUB+EJzepbglJ1VY2k=; b=YlwF1w6NrLpDFXmH1jmwcpH4LpgZ+O1oOaFo4t549Z/fFyd9PShbrC21TLob5MyKoR Pz1p/qMAbEGpW0A6gGDF6D03QIH27UY05P6gpMAITvzhC95y31J/kkiz5WfpxcPpxgq2 +09kLIMRCXsypgRLCPZ2EgObk7Bluw2kPZZD+xlZljJOBpCZWfDzQ2L1v7WsVZ/957I7 094fDIWgINTgRIcJRmW+VepyE4S7yDl2774Gj90xKguV2TdpgEJNqaZFO3R6U4vu0/ZL L+3AtrDSLb217ayJEaxN8MBWSsZZJb+lhjEykdlQ0mwkHIDy5y0Fp8CunBxOlm3ifBYZ Zlkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694634325; x=1695239125; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0CSWIgf+eYsLKhM4nFKY23zezuUB+EJzepbglJ1VY2k=; b=lnsgxOPp5xJaBog+nWbv7OXq4k6DEa5QNVGCKftrlupmbFUcE8CuB5VIYGI+ahX6JX DAdWHsCDVHlUe0RWZGgURmpFTnVlXdI/Yj3a6QSLtO0ZTWBS/ZOqkj+yoIosWNXsA1dD GdRXS/9lOlccTBdkoNYxqj+ArDFKWLHbkWjk3jTzKsmWucIvsdbNQbQKLt5g4Gg+SVrw h+tlriEqB3ikvEo4/qzJ1GsOABn+cTuiljS0iFVRkY5/zb7W1NzsZin3vxcKrTVX2iJ4 3D+KcLT1vgCzd0f4+VRQTu95pUT+03lnthP2SmdVCKn/IIfs5iUQCrv+wyKGlOksCK8q WQfQ== X-Gm-Message-State: AOJu0YwM+U0wB85mSJhr4n8Zz09UHOq1SZX42lNHEuVXogWWEJzNECIZ MaaMRhYm8nKByX+Mp7S6SonSyQ== X-Google-Smtp-Source: AGHT+IFXpyDvdmAjaN9xiY0TW4IhuZt+CddH8qdogMtrfrwBz1iKFgmJ+9LfFD1aUNV1v4I9b3Fl0g== X-Received: by 2002:a05:6830:6710:b0:6c0:edaa:58a1 with SMTP id cr16-20020a056830671000b006c0edaa58a1mr3741077otb.36.1694634324928; Wed, 13 Sep 2023 12:45:24 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c0:91cb:cdab:14e8:3780:382b? ([2804:1b3:a7c0:91cb:cdab:14e8:3780:382b]) by smtp.gmail.com with ESMTPSA id k20-20020a056830151400b006b9cc007e87sm6877otp.20.2023.09.13.12.45.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 13 Sep 2023 12:45:24 -0700 (PDT) Message-ID: <1510ff5f-ab9c-a839-4334-1a6ff33b99b1@linaro.org> Date: Wed, 13 Sep 2023 16:45:21 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Subject: Re: [PATCH v3] argp-help: Get rid of alloca. Content-Language: en-US To: Joe Simmons-Talbott , libc-alpha@sourceware.org References: <20230913160042.326350-1-josimmon@redhat.com> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <20230913160042.326350-1-josimmon@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 List-Id: On 13/09/23 13:00, Joe Simmons-Talbott wrote: > 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); > Use malloc here as well. > @@ -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)