From patchwork Wed Sep 13 20:44:09 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: 75890 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 B3C443858C3A for ; Wed, 13 Sep 2023 20:44:51 +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 E5CA23858D3C for ; Wed, 13 Sep 2023 20:44:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E5CA23858D3C 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=1694637871; 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=xcqtNIUoWacQ1fxIzYDOLCJH6YYSlM370gTajeTYNzQ=; b=HICiI1GXvnwPWAkr0bmBMtAKnw9mikpnCcfDCyiqqH5TJuIBGfIJ4BnlDpzd8iUbbqhHfE ubPPG6M370IGbP0hJJkuGYrNiZjgmj/DJ2bNmGJs9WWdGLyUvHXtetIqJS3/KNZg9pDu9y 4aVK0GqC3NBXJVPb3TSvuK1DXB8rRYE= Received: from mail-oi1-f200.google.com (mail-oi1-f200.google.com [209.85.167.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-614-epHGv5YLOHmlutwlcOFpBQ-1; Wed, 13 Sep 2023 16:44:30 -0400 X-MC-Unique: epHGv5YLOHmlutwlcOFpBQ-1 Received: by mail-oi1-f200.google.com with SMTP id 5614622812f47-3ac97a6abf6so318177b6e.1 for ; Wed, 13 Sep 2023 13:44:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694637869; x=1695242669; 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=Ci3D6FqD8lt902hTU3wTvd49bGf27Ro9w4f9x/c0oxg=; b=H2PVTPidgbm/lFOzhi9CDnJTSkMsiI5h8KqGfyGeI7HAxebfPm/OEha682QaaX+1Yq MZk+gCQePNtYztup7+1twYWCQuvEqRVqBr+uuocqmEiYCmZgx2pDWMGYjfDM2ghadHpS Ar35QN5Q8YqJ66Nq9aS2SupiyV3M/9bLKFJoqBNiB7J4wM/miG1PJrukmGfF/GUkG31y WiYb9HovhjpQLB6YewhgK++arHtAycZYde54jkrOdkLgT1qrL3xP7ntuZkCq7OnwM+2e Bzl2QpKOSsN0iJDkSfZ8x+NQlcYYW9p0ei3mE/vV6knuvDk1vYxJKAzk/0Dwjf6ygMF/ VnMQ== X-Gm-Message-State: AOJu0YxttHQrbcvQQ5nEFOY8f9YS5+QkYnLdqXlfDNhogK0MJ7+FJCQx FfZqI83MI6tRUF9OKyxsc+ChSAL/hHIlY5bIwu8SYTj0OHDW3jk9EUM7A9qne1vgNrO0I5vJEwQ KnKdOIaQyVZiix1mWmhZcFNP9PCqruga+kK+ETz4OS9Cp+qEeQNvaGi8nO/8v188ai+Y0jcDMun Tn2l7e X-Received: by 2002:a05:6808:1520:b0:3a3:eab8:8c40 with SMTP id u32-20020a056808152000b003a3eab88c40mr4521814oiw.54.1694637869466; Wed, 13 Sep 2023 13:44:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFFDCLJUUnc39i1cKdGMGFlGNeHL0iejF9VKVGO0Gx10ShG1012fFoJL0uVvYY7K5lguMVhWg== X-Received: by 2002:a05:6808:1520:b0:3a3:eab8:8c40 with SMTP id u32-20020a056808152000b003a3eab88c40mr4521798oiw.54.1694637869226; Wed, 13 Sep 2023 13:44:29 -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 kf10-20020a05622a2a8a00b00411fcc18cc1sm29788qtb.64.2023.09.13.13.44.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 13:44:28 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v4] argp-help: Get rid of alloca. Date: Wed, 13 Sep 2023 16:44:09 -0400 Message-ID: <20230913204426.1460643-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 to avoid potential stack overflow. Checked on x86_64-linux-gnu --- Changes to v3: * convert scratch_buffer to malloc. Changes to v2: * Convert first scratch_buffer to malloc. * Remove alloca boilerplate. argp/argp-help.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/argp/argp-help.c b/argp/argp-help.c index d019ed58d2..262729e976 100644 --- a/argp/argp-help.c +++ b/argp/argp-help.c @@ -25,21 +25,6 @@ #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 @@ -1450,8 +1435,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 +1469,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 +1691,10 @@ _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); + char *pattern_levels; + + pattern_levels = malloc (num_pattern_levels); + assert (pattern_levels != NULL); memset (pattern_levels, 0, num_pattern_levels); @@ -1746,6 +1742,8 @@ _help (const struct argp *argp, const struct argp_state *state, FILE *stream, first_pattern = 0; } while (more_patterns); + + free (pattern_levels); } if (flags & ARGP_HELP_PRE_DOC)