From patchwork Thu Jul 13 16:04:10 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: 72661 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 4BB9F3858CDB for ; Thu, 13 Jul 2023 16:04:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4BB9F3858CDB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689264278; bh=d7cWpjRVk97iDSR94v96fGolTKhQVVevgggUHqFIZ3A=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=U35gptiWcaMg2uBV3xodANlzyzCGHseRgYIN/Q4loiFpYKZkx+KisujRNx+cBdKJH gilnWVB4Qz9cMh4LtdpPzBdIWlb+f28zqKFlA2Xgn8Dh66mswwlNlfi/s5Axsu1UcR SRYmj/UeduCi9DcPovR1HNuZfKrH2OnlHluYr50s= 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 731143858CDB for ; Thu, 13 Jul 2023 16:04:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 731143858CDB Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-336-IKdx9xaXOfe9scyl2487BQ-1; Thu, 13 Jul 2023 12:04:14 -0400 X-MC-Unique: IKdx9xaXOfe9scyl2487BQ-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-4039e119f40so11292481cf.0 for ; Thu, 13 Jul 2023 09:04:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689264254; x=1691856254; 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=d7cWpjRVk97iDSR94v96fGolTKhQVVevgggUHqFIZ3A=; b=kSYCc3u7DEZUfaock8ELeyZ1X5erhebvbvTXzHgh7+8CNY6whX8zxc5JYIbuRbv4Yi t2L7DmmzW0lz1H8R/qO09bwOC6PblF5Vvilre0wY4Tg9h9XDBlrt7hZC+6ejAA8qNYvG jzq0UGt4dkZYX2M90Y3DSCfB2B6czR8SvGXYnq4xeqGuTIRz1I4BXGt/3kEUsRrcu/ji NkGl1YNym7fubLMjRazQkz1futskpFJvdifaj/u+3y3NHvsO+94X2fUMSzDupPxf4T34 06v3wi8MG7Y4DNa2QERJkzqQHi+eQuwEeqfmEqp5i2MaPBVXUklzh3MDfe+NFot0R4kY pHnQ== X-Gm-Message-State: ABy/qLY3ZklJFgyoLnX129aFh/ZPMKPg6CerWkdg3aKNPvAjjjA0/Flz J1mJM5i5NleZM+3l3rhtWFSvOjpQEx3iSO1HFGV+8zPVVQFwOrUFUOMwDULf49xn3Ig/RSYneYR XH48vNvfiIrkjb4/oR4QqPCjicQdmM43dnqZ1p/RXKvjnDhIONSZlPbavImDbnu8/i3F7epIS22 l+CRD0 X-Received: by 2002:a05:622a:34d:b0:403:ac9c:abff with SMTP id r13-20020a05622a034d00b00403ac9cabffmr2500351qtw.31.1689264254049; Thu, 13 Jul 2023 09:04:14 -0700 (PDT) X-Google-Smtp-Source: APBJJlFmpcXrQsPhcyex1mg+LOwAlFcY4X/7VpykhbWEhm4WF3JfTuRPLtl6Oa00kLkik04xYR0Qlg== X-Received: by 2002:a05:622a:34d:b0:403:ac9c:abff with SMTP id r13-20020a05622a034d00b00403ac9cabffmr2500312qtw.31.1689264253531; Thu, 13 Jul 2023 09:04:13 -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 g18-20020ac84b72000000b003f364778b2bsm3220440qts.4.2023.07.13.09.04.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 09:04:13 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH] gencat: Get rid of alloca. Date: Thu, 13 Jul 2023 12:04:10 -0400 Message-Id: <20230713160410.2996030-1-josimmon@redhat.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.29 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" Convert to scratch_buffers to avoid potential stack overflow. Checked on x86_64-linux-gnu and aarch64-linux-gnu. Reviewed-by: Adhemerval Zanella --- catgets/gencat.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/catgets/gencat.c b/catgets/gencat.c index 9cacc801b5..63bdbf86a6 100644 --- a/catgets/gencat.c +++ b/catgets/gencat.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -854,6 +855,10 @@ write_out (struct catalog *catalog, const char *output_name, uint32_t *array1, *array2; size_t cnt; int fd; + struct scratch_buffer buf1; + scratch_buffer_init (&buf1); + struct scratch_buffer buf2; + scratch_buffer_init (&buf2); /* If not otherwise told try to read file with existing translations. */ @@ -929,9 +934,19 @@ write_out (struct catalog *catalog, const char *output_name, uint32_t array_size = best_size * best_depth * sizeof (uint32_t) * 3; /* Allocate room for all needed arrays. */ - array1 = (uint32_t *) alloca (array_size); + if (!scratch_buffer_set_array_size (&buf1, best_size * best_depth * 3, + sizeof (uint32_t))) + error (EXIT_FAILURE, ENOMEM, gettext ("cannot allocate memory")); + array1 = buf1.data; memset (array1, '\0', array_size); - array2 = (uint32_t *) alloca (array_size); + + if (!scratch_buffer_set_array_size (&buf2, best_size * best_depth * 3, + sizeof (uint32_t))) + { + scratch_buffer_free (&buf1); + error (EXIT_FAILURE, ENOMEM, gettext ("cannot allocate memory")); + } + array2 = buf2.data; obstack_init (&string_pool); set_run = catalog->all_sets; @@ -979,8 +994,12 @@ write_out (struct catalog *catalog, const char *output_name, { fd = creat (output_name, 0666); if (fd < 0) - error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"), - output_name); + { + scratch_buffer_free (&buf1); + scratch_buffer_free (&buf2); + error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"), + output_name); + } } /* Write out header. */ @@ -1019,8 +1038,12 @@ write_out (struct catalog *catalog, const char *output_name, { fp = fopen (header_name, "w"); if (fp == NULL) - error (EXIT_FAILURE, errno, - gettext ("cannot open output file `%s'"), header_name); + { + scratch_buffer_free (&buf1); + scratch_buffer_free (&buf2); + error (EXIT_FAILURE, errno, + gettext ("cannot open output file `%s'"), header_name); + } } /* Iterate over all sets and all messages. */ @@ -1066,6 +1089,8 @@ write_out (struct catalog *catalog, const char *output_name, if (fp != stdout) fclose (fp); } + scratch_buffer_free (&buf1); + scratch_buffer_free (&buf2); }