argp-help: Get rid of alloca.

Message ID 20230712170206.3675587-1-josimmon@redhat.com
State Superseded
Headers
Series argp-help: 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-aarch64 fail Testing failed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed

Commit Message

Joe Simmons-Talbott July 12, 2023, 5:02 p.m. UTC
  Replace alloca with a scratch_buffer to avoid potential stack overflow.

Checked on x86_64-linux-gnu
---
 argp/argp-help.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)
  

Comments

Joe Simmons-Talbott Aug. 3, 2023, 1:11 p.m. UTC | #1
On Wed, Jul 12, 2023 at 01:02:06PM -0400, Joe Simmons-Talbott wrote:
> Replace alloca with a scratch_buffer to avoid potential stack overflow.

Ping.

Thanks,
Joe
> 
> Checked on x86_64-linux-gnu
> ---
>  argp/argp-help.c | 26 +++++++++++++++++++++++---
>  1 file changed, 23 insertions(+), 3 deletions(-)
> 
> diff --git a/argp/argp-help.c b/argp/argp-help.c
> index d019ed58d2..a5982334f6 100644
> --- a/argp/argp-help.c
> +++ b/argp/argp-help.c
> @@ -40,6 +40,7 @@ char *alloca ();
>  # endif
>  #endif
>  
> +#include <scratch_buffer.h>
>  #include <stdbool.h>
>  #include <stddef.h>
>  #include <stdlib.h>
> @@ -1450,8 +1451,17 @@ 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;
> +
> +      if (!scratch_buffer_set_array_size (&buf, 1,
> +					   strlen (hol->short_options) + 1))
> +        return;
> +      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 +1488,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 +1710,13 @@ _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;
> +
> +      if (!scratch_buffer_set_array_size (&buf, 1, num_pattern_levels))
> +        return;
> +      pattern_levels = buf.data;
>  
>        memset (pattern_levels, 0, num_pattern_levels);
>  
> @@ -1746,6 +1764,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
>
  
Joe Simmons-Talbott Aug. 15, 2023, 2:49 p.m. UTC | #2
On Thu, Aug 03, 2023 at 09:11:37AM -0400, Joe Simmons-Talbott via Libc-alpha wrote:
> On Wed, Jul 12, 2023 at 01:02:06PM -0400, Joe Simmons-Talbott wrote:
> > Replace alloca with a scratch_buffer to avoid potential stack overflow.
> 
> Ping.
Ping.

Thanks,
Joe
> 
> Thanks,
> Joe
> > 
> > Checked on x86_64-linux-gnu
> > ---
> >  argp/argp-help.c | 26 +++++++++++++++++++++++---
> >  1 file changed, 23 insertions(+), 3 deletions(-)
> > 
> > diff --git a/argp/argp-help.c b/argp/argp-help.c
> > index d019ed58d2..a5982334f6 100644
> > --- a/argp/argp-help.c
> > +++ b/argp/argp-help.c
> > @@ -40,6 +40,7 @@ char *alloca ();
> >  # endif
> >  #endif
> >  
> > +#include <scratch_buffer.h>
> >  #include <stdbool.h>
> >  #include <stddef.h>
> >  #include <stdlib.h>
> > @@ -1450,8 +1451,17 @@ 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;
> > +
> > +      if (!scratch_buffer_set_array_size (&buf, 1,
> > +					   strlen (hol->short_options) + 1))
> > +        return;
> > +      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 +1488,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 +1710,13 @@ _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;
> > +
> > +      if (!scratch_buffer_set_array_size (&buf, 1, num_pattern_levels))
> > +        return;
> > +      pattern_levels = buf.data;
> >  
> >        memset (pattern_levels, 0, num_pattern_levels);
> >  
> > @@ -1746,6 +1764,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
> > 
>
  
Joe Simmons-Talbott Aug. 28, 2023, 1:20 p.m. UTC | #3
Ping.

On Tue, Aug 15, 2023 at 10:49:54AM -0400, Joe Simmons-Talbott via Libc-alpha wrote:
> On Thu, Aug 03, 2023 at 09:11:37AM -0400, Joe Simmons-Talbott via Libc-alpha wrote:
> > On Wed, Jul 12, 2023 at 01:02:06PM -0400, Joe Simmons-Talbott wrote:
> > > Replace alloca with a scratch_buffer to avoid potential stack overflow.
> > 
> > Ping.
> Ping.
> 
> Thanks,
> Joe
> > 
> > Thanks,
> > Joe
> > > 
> > > Checked on x86_64-linux-gnu
> > > ---
> > >  argp/argp-help.c | 26 +++++++++++++++++++++++---
> > >  1 file changed, 23 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/argp/argp-help.c b/argp/argp-help.c
> > > index d019ed58d2..a5982334f6 100644
> > > --- a/argp/argp-help.c
> > > +++ b/argp/argp-help.c
> > > @@ -40,6 +40,7 @@ char *alloca ();
> > >  # endif
> > >  #endif
> > >  
> > > +#include <scratch_buffer.h>
> > >  #include <stdbool.h>
> > >  #include <stddef.h>
> > >  #include <stdlib.h>
> > > @@ -1450,8 +1451,17 @@ 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;
> > > +
> > > +      if (!scratch_buffer_set_array_size (&buf, 1,
> > > +					   strlen (hol->short_options) + 1))
> > > +        return;
> > > +      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 +1488,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 +1710,13 @@ _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;
> > > +
> > > +      if (!scratch_buffer_set_array_size (&buf, 1, num_pattern_levels))
> > > +        return;
> > > +      pattern_levels = buf.data;
> > >  
> > >        memset (pattern_levels, 0, num_pattern_levels);
> > >  
> > > @@ -1746,6 +1764,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
> > > 
> > 
>
  
Adhemerval Zanella Netto Aug. 28, 2023, 4:52 p.m. UTC | #4
On 12/07/23 14:02, Joe Simmons-Talbott via Libc-alpha wrote:
> Replace alloca with a scratch_buffer to avoid potential stack overflow.
> 
> Checked on x86_64-linux-gnu
> ---
>  argp/argp-help.c | 26 +++++++++++++++++++++++---
>  1 file changed, 23 insertions(+), 3 deletions(-)
> 
> diff --git a/argp/argp-help.c b/argp/argp-help.c
> index d019ed58d2..a5982334f6 100644
> --- a/argp/argp-help.c
> +++ b/argp/argp-help.c
> @@ -40,6 +40,7 @@ char *alloca ();
>  # endif
>  #endif
>  
> +#include <scratch_buffer.h>
>  #include <stdbool.h>
>  #include <stddef.h>
>  #include <stdlib.h>
> @@ -1450,8 +1451,17 @@ 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;
> +
> +      if (!scratch_buffer_set_array_size (&buf, 1,
> +					   strlen (hol->short_options) + 1))
> +        return;

I am not sure how to properly handle memory allocation failures here, since the interface
does have a way to return failures. On other places that call malloc, such as make_hol,
it at least adds a assert.  I think it should do the same here. 

> +      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 +1488,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 +1710,13 @@ _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;
> +
> +      if (!scratch_buffer_set_array_size (&buf, 1, num_pattern_levels))
> +        return;
> +      pattern_levels = buf.data;
>  
>        memset (pattern_levels, 0, num_pattern_levels);
>  

Same as before.

> @@ -1746,6 +1764,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)
  
Joe Simmons-Talbott Aug. 28, 2023, 6:29 p.m. UTC | #5
On Mon, Aug 28, 2023 at 01:52:30PM -0300, Adhemerval Zanella Netto wrote:
> 
> 
> On 12/07/23 14:02, Joe Simmons-Talbott via Libc-alpha wrote:
> > Replace alloca with a scratch_buffer to avoid potential stack overflow.
> > 
> > Checked on x86_64-linux-gnu
> > ---
> >  argp/argp-help.c | 26 +++++++++++++++++++++++---
> >  1 file changed, 23 insertions(+), 3 deletions(-)
> > 
> > diff --git a/argp/argp-help.c b/argp/argp-help.c
> > index d019ed58d2..a5982334f6 100644
> > --- a/argp/argp-help.c
> > +++ b/argp/argp-help.c
> > @@ -40,6 +40,7 @@ char *alloca ();
> >  # endif
> >  #endif
> >  
> > +#include <scratch_buffer.h>
> >  #include <stdbool.h>
> >  #include <stddef.h>
> >  #include <stdlib.h>
> > @@ -1450,8 +1451,17 @@ 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;
> > +
> > +      if (!scratch_buffer_set_array_size (&buf, 1,
> > +					   strlen (hol->short_options) + 1))
> > +        return;
> 
> I am not sure how to properly handle memory allocation failures here, since the interface
> does have a way to return failures. On other places that call malloc, such as make_hol,
> it at least adds a assert.  I think it should do the same here. 

Fixed in v2.

> 
> > +      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 +1488,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 +1710,13 @@ _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;
> > +
> > +      if (!scratch_buffer_set_array_size (&buf, 1, num_pattern_levels))
> > +        return;
> > +      pattern_levels = buf.data;
> >  
> >        memset (pattern_levels, 0, num_pattern_levels);
> >  
> 
> Same as before.
> 

Fixed in v2.

> > @@ -1746,6 +1764,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)
>
  

Patch

diff --git a/argp/argp-help.c b/argp/argp-help.c
index d019ed58d2..a5982334f6 100644
--- a/argp/argp-help.c
+++ b/argp/argp-help.c
@@ -40,6 +40,7 @@  char *alloca ();
 # endif
 #endif
 
+#include <scratch_buffer.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdlib.h>
@@ -1450,8 +1451,17 @@  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;
+
+      if (!scratch_buffer_set_array_size (&buf, 1,
+					   strlen (hol->short_options) + 1))
+        return;
+      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 +1488,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 +1710,13 @@  _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;
+
+      if (!scratch_buffer_set_array_size (&buf, 1, num_pattern_levels))
+        return;
+      pattern_levels = buf.data;
 
       memset (pattern_levels, 0, num_pattern_levels);
 
@@ -1746,6 +1764,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)