gencat: Get rid of alloca.

Message ID 20230713160410.2996030-1-josimmon@redhat.com
State Committed
Commit 4d8b09393354f6ce079f399df5b84abf0db894b5
Headers
Series gencat: Get rid of alloca. |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed

Commit Message

Joe Simmons-Talbott July 13, 2023, 4:04 p.m. UTC
  Convert to scratch_buffers to avoid potential stack overflow.

Checked on x86_64-linux-gnu and aarch64-linux-gnu.
---
 catgets/gencat.c | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)
  

Comments

Joe Simmons-Talbott Aug. 3, 2023, 1:10 p.m. UTC | #1
On Thu, Jul 13, 2023 at 12:04:10PM -0400, Joe Simmons-Talbott wrote:
> Convert to scratch_buffers to avoid potential stack overflow.

Ping.

Thanks,
Joe
> 
> Checked on x86_64-linux-gnu and aarch64-linux-gnu.
> ---
>  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);
>  }
>  
>  
> -- 
> 2.39.2
>
  
Joe Simmons-Talbott Aug. 15, 2023, 2:48 p.m. UTC | #2
On Thu, Aug 03, 2023 at 09:10:26AM -0400, Joe Simmons-Talbott via Libc-alpha wrote:
> On Thu, Jul 13, 2023 at 12:04:10PM -0400, Joe Simmons-Talbott wrote:
> > Convert to scratch_buffers to avoid potential stack overflow.
> 
> Ping.
Ping.

Thanks,
Joe
> 
> Thanks,
> Joe
> > 
> > Checked on x86_64-linux-gnu and aarch64-linux-gnu.
> > ---
> >  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);
> >  }
> >  
> >  
> > -- 
> > 2.39.2
> > 
>
  
Joe Simmons-Talbott Aug. 28, 2023, 1:20 p.m. UTC | #3
Ping.

On Tue, Aug 15, 2023 at 10:48:43AM -0400, Joe Simmons-Talbott via Libc-alpha wrote:
> On Thu, Aug 03, 2023 at 09:10:26AM -0400, Joe Simmons-Talbott via Libc-alpha wrote:
> > On Thu, Jul 13, 2023 at 12:04:10PM -0400, Joe Simmons-Talbott wrote:
> > > Convert to scratch_buffers to avoid potential stack overflow.
> > 
> > Ping.
> Ping.
> 
> Thanks,
> Joe
> > 
> > Thanks,
> > Joe
> > > 
> > > Checked on x86_64-linux-gnu and aarch64-linux-gnu.
> > > ---
> > >  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);
> > >  }
> > >  
> > >  
> > > -- 
> > > 2.39.2
> > > 
> > 
>
  
Adhemerval Zanella Aug. 28, 2023, 4:40 p.m. UTC | #4
On 13/07/23 13:04, Joe Simmons-Talbott via Libc-alpha wrote:
> Convert to scratch_buffers to avoid potential stack overflow.
> 
> Checked on x86_64-linux-gnu and aarch64-linux-gnu.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  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);
>  }
>  
>
  

Patch

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);
 }