From patchwork Mon Aug 28 18:27:01 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: 74843 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 4872F3858C20 for ; Mon, 28 Aug 2023 18:27:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4872F3858C20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1693247262; bh=wVDHbpkFfl/ZvJDzAX1BfgBUOuTPvKF+eYy3as9N1K0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=dayItDj95/vQTfop/2VGeFx8qtUfjj2RgYL+yvZ8wCKYqJ2TiX0LcdAlUvyiwUimp sCEBArMlH4tYpht0OgFAGqnv82Di5zj3rvIVXqOKigWngPUNS42SIr61f1eDMmDWra kBVlHKlMX6Zg4rUqAJEK8AcUzarGHv7s5ormPha4= 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 CE3493858D28 for ; Mon, 28 Aug 2023 18:27:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CE3493858D28 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) To: libc-alpha@sourceware.org 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 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 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: , X-Patchwork-Original-From: Joe Simmons-Talbott via Libc-alpha From: Joe Simmons-Talbott Reply-To: Joe Simmons-Talbott Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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)