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 CE3493858D28 for ; Mon, 28 Aug 2023 18:27:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CE3493858D28 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=1693247237; 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=I3PNaFgY93uEFbLL/TyCXcfy4GaUaBHyI4gf7YmZ+wQ=; b=SwcUp2nt7mVizJuKboqkdmou/TBa7YGV/HL4LikUZpghqeE//VcnX5/MLwLFYHfzNIRXdI QKlGhbMHfln809j7H+Hc6Q+A61IfVHGaYmZj/C6OAYTd/qBn08SXUbypklHXHMKDNuQaLo xefNr8/lHAEwtf/qNvhFckTCtYTUSi8= Received: from mail-vk1-f197.google.com (mail-vk1-f197.google.com [209.85.221.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-122-oo0H9RUlN7KFBAaeTybTMw-1; Mon, 28 Aug 2023 14:27:16 -0400 X-MC-Unique: oo0H9RUlN7KFBAaeTybTMw-1 Received: by mail-vk1-f197.google.com with SMTP id 71dfb90a1353d-48f986eee7dso1146554e0c.2 for ; Mon, 28 Aug 2023 11:27:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693247235; x=1693852035; 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=wVDHbpkFfl/ZvJDzAX1BfgBUOuTPvKF+eYy3as9N1K0=; b=HfyUGII0C5TIk0x4BVbo237OxLKIICGG42XM06Z8Zo2Uk4EOPBGmzgmQyXJht+l/yQ ypbQR9p8qE/ztv2k8W3o1MktE8b6rn24K60BV/k3gRJHRN2e6mfvALanm8rcJqer+k+M UtuSjCKnRtt7Zn8YMtcjPR+f77V3ISCATgotbcTl6RmF4sNCFzrYqxHiUgsPezYM/Drh i+UmiMLPA2nz2N17tHIe1Z/rYvfTckXy8ASk4d3nM/mBfVEL+uCAp6oJq+jqfZVpsiuS e2/bMVmac282uZL20QxkBuhUpUHTa/xK5J/fuZYt5j7MZmGrQ7VKq502WtQEtZdN+nm8 yhSQ== X-Gm-Message-State: AOJu0YyvNVBD6pcGwGJF4zrvVur5fRcn5Vik7+ExAz9HVGa3g04lsk3l fRYp2ZJlfzUfeOnQ+nZ4nUd5jgOBEpALDAfts+Wr69L1UuWZtNYw9Eit6kaDOt7R6ZAICV4raDq piajaaNpLMMvnSaxUV1adEWkoy3L5whHxnC6ybej2HTo/h97HHb2hFw+B9xJV8cxfwzFl4qOGVf Df+Ki3 X-Received: by 2002:a1f:c381:0:b0:48c:fdfe:aa40 with SMTP id t123-20020a1fc381000000b0048cfdfeaa40mr22193382vkf.3.1693247235269; Mon, 28 Aug 2023 11:27:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG5R4tGQ2K//pSplGK80MkLQLY6cQjODftNjH1jdjuyKv9cPxirkm5A1ZRrOKlEfLz25g/ImQ== X-Received: by 2002:a1f:c381:0:b0:48c:fdfe:aa40 with SMTP id t123-20020a1fc381000000b0048cfdfeaa40mr22193366vkf.3.1693247234936; Mon, 28 Aug 2023 11:27:14 -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 i6-20020a0cf486000000b00631ecb1052esm2793597qvm.74.2023.08.28.11.27.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Aug 2023 11:27:14 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v2] argp-help: Get rid of alloca. Date: Mon, 28 Aug 2023 14:27:01 -0400 Message-ID: <20230828182711.1376956-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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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_H4,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: Replace alloca with a scratch_buffer to avoid potential stack overflow. Checked on x86_64-linux-gnu --- Changes to v1: * Call assert on allocation failure. argp/argp-help.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/argp/argp-help.c b/argp/argp-help.c index d019ed58d2..af99649f20 100644 --- a/argp/argp-help.c +++ b/argp/argp-help.c @@ -40,6 +40,7 @@ char *alloca (); # endif #endif +#include #include #include #include @@ -1450,8 +1451,19 @@ 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; + struct scratch_buffer buf; + scratch_buffer_init (&buf); + char *short_no_arg_opts; + char *snao_end; + bool result; + + result = scratch_buffer_set_array_size (&buf, 1, + strlen (hol->short_options) + 1); + assert (result); + + short_no_arg_opts = buf.data; + 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 +1490,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); + + scratch_buffer_free (&buf); } } @@ -1698,7 +1712,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 +1768,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) -- 2.39.2