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 E31ED3858D20 for ; Wed, 13 Sep 2023 20:45:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E31ED3858D20 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=1694637957; 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: in-reply-to:in-reply-to:references:references; bh=P4vQagy1GbTHZj8VRRsUNCOdaDGY39zMv+u86OSVL+o=; b=MyA/90JVc6HISSM4M9xUvyQ7lpF733FJCpO8O7aYO/yqN8i8UXmgd0JJ9pY6duKYKRIhr7 iUUwfXxquL+TpMieUa7jAdjSnuA7sjn8fIfr2vOa774/UK/koqilh4W+qcHUarq0ORkuEE W3MEMm79JTobmJGgBtYxYj7/8KHnVXQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-308-PLwfNy-7MxaYWBOVlLfA6Q-1; Wed, 13 Sep 2023 16:45:53 -0400 X-MC-Unique: PLwfNy-7MxaYWBOVlLfA6Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 94B4B185A78E; Wed, 13 Sep 2023 20:45:53 +0000 (UTC) Received: from oak (unknown [10.22.8.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7891F40C6EA8; Wed, 13 Sep 2023 20:45:53 +0000 (UTC) Date: Wed, 13 Sep 2023 16:45:52 -0400 From: Joe Simmons-Talbott To: Adhemerval Zanella Netto Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v3] argp-help: Get rid of alloca. Message-ID: <20230913204552.GP3849957@oak> References: <20230913160042.326350-1-josimmon@redhat.com> <1510ff5f-ab9c-a839-4334-1a6ff33b99b1@linaro.org> MIME-Version: 1.0 In-Reply-To: <1510ff5f-ab9c-a839-4334-1a6ff33b99b1@linaro.org> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-12.0 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 List-Id: On Wed, Sep 13, 2023 at 04:45:21PM -0300, Adhemerval Zanella Netto wrote: > > > 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. Done in v4.