public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Joe Simmons-Talbott <josepht@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] gencat: Get rid of alloca.
Date: Mon, 28 Aug 2023 16:43:50 +0000 (GMT)	[thread overview]
Message-ID: <20230828164350.3E0F93858D38@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=4d8b09393354f6ce079f399df5b84abf0db894b5

commit 4d8b09393354f6ce079f399df5b84abf0db894b5
Author: Joe Simmons-Talbott <josimmon@redhat.com>
Date:   Mon Aug 28 16:42:13 2023 +0000

    gencat: Get rid of alloca.
    
    Convert to scratch_buffers to avoid potential stack overflow.
    
    Checked on x86_64-linux-gnu and aarch64-linux-gnu.
    
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

Diff:
---
 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 <limits.h>
 #include <nl_types.h>
 #include <obstack.h>
+#include <scratch_buffer.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -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);
 }

                 reply	other threads:[~2023-08-28 16:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230828164350.3E0F93858D38@sourceware.org \
    --to=josepht@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).