public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] ar: Replace one alloca use by xmalloc
@ 2024-04-30 14:39 Mark Wielaard
  2024-05-10 21:16 ` Aaron Merey
  0 siblings, 1 reply; 3+ messages in thread
From: Mark Wielaard @ 2024-04-30 14:39 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Mark Wielaard

This alloca use is inside a lexical block and is used to replace one
element of argv. Use a function local variable, xmalloc and free to
make memory usage pattern more clear.

    * src/ar.c (main): Move newp char pointer declaration up.
    Use xmalloc to allocate space. free at end of main.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ar.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/ar.c b/src/ar.c
index e6d6d58f2b3b..fcb8bfb90a9f 100644
--- a/src/ar.c
+++ b/src/ar.c
@@ -41,6 +41,7 @@
 #include <system.h>
 #include <printversion.h>
 
+#include "libeu.h"
 #include "arlib.h"
 
 
@@ -154,10 +155,11 @@ main (int argc, char *argv[])
 
   /* For historical reasons the options in the first parameter need
      not be preceded by a dash.  Add it now if necessary.  */
+  char *newp = NULL;
   if (argc > 1 && argv[1][0] != '-')
     {
       size_t len = strlen (argv[1]) + 1;
-      char *newp = alloca (len + 1);
+      newp = (char *) xmalloc (len + 1);
       newp[0] = '-';
       memcpy (&newp[1], argv[1], len);
       argv[1] = newp;
@@ -271,6 +273,8 @@ MEMBER parameter required for 'a', 'b', and 'i' modifiers"));
       break;
     }
 
+  free (newp);
+
   return status;
 }
 
-- 
2.44.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ar: Replace one alloca use by xmalloc
  2024-04-30 14:39 [PATCH] ar: Replace one alloca use by xmalloc Mark Wielaard
@ 2024-05-10 21:16 ` Aaron Merey
  2024-05-12 21:55   ` Mark Wielaard
  0 siblings, 1 reply; 3+ messages in thread
From: Aaron Merey @ 2024-05-10 21:16 UTC (permalink / raw)
  To: Mark Wielaard; +Cc: elfutils-devel

Hi Mark,

On Tue, Apr 30, 2024 at 10:39 AM Mark Wielaard <mark@klomp.org> wrote:
>
> This alloca use is inside a lexical block and is used to replace one
> element of argv. Use a function local variable, xmalloc and free to
> make memory usage pattern more clear.
>
>     * src/ar.c (main): Move newp char pointer declaration up.
>     Use xmalloc to allocate space. free at end of main.
>
> Signed-off-by: Mark Wielaard <mark@klomp.org>

LGTM. This patch shouldn't change anything except possibly help
some static analyzers detect correct memory usage.

Aaron

> ---
>  src/ar.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/src/ar.c b/src/ar.c
> index e6d6d58f2b3b..fcb8bfb90a9f 100644
> --- a/src/ar.c
> +++ b/src/ar.c
> @@ -41,6 +41,7 @@
>  #include <system.h>
>  #include <printversion.h>
>
> +#include "libeu.h"
>  #include "arlib.h"
>
>
> @@ -154,10 +155,11 @@ main (int argc, char *argv[])
>
>    /* For historical reasons the options in the first parameter need
>       not be preceded by a dash.  Add it now if necessary.  */
> +  char *newp = NULL;
>    if (argc > 1 && argv[1][0] != '-')
>      {
>        size_t len = strlen (argv[1]) + 1;
> -      char *newp = alloca (len + 1);
> +      newp = (char *) xmalloc (len + 1);
>        newp[0] = '-';
>        memcpy (&newp[1], argv[1], len);
>        argv[1] = newp;
> @@ -271,6 +273,8 @@ MEMBER parameter required for 'a', 'b', and 'i' modifiers"));
>        break;
>      }
>
> +  free (newp);
> +
>    return status;
>  }
>
> --
> 2.44.0
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ar: Replace one alloca use by xmalloc
  2024-05-10 21:16 ` Aaron Merey
@ 2024-05-12 21:55   ` Mark Wielaard
  0 siblings, 0 replies; 3+ messages in thread
From: Mark Wielaard @ 2024-05-12 21:55 UTC (permalink / raw)
  To: Aaron Merey; +Cc: elfutils-devel

Hi Aaron,

On Fri, May 10, 2024 at 05:16:06PM -0400, Aaron Merey wrote:
> On Tue, Apr 30, 2024 at 10:39 AM Mark Wielaard <mark@klomp.org> wrote:
> >
> > This alloca use is inside a lexical block and is used to replace one
> > element of argv. Use a function local variable, xmalloc and free to
> > make memory usage pattern more clear.
> >
> >     * src/ar.c (main): Move newp char pointer declaration up.
> >     Use xmalloc to allocate space. free at end of main.
> >
> > Signed-off-by: Mark Wielaard <mark@klomp.org>
> 
> LGTM. This patch shouldn't change anything except possibly help
> some static analyzers detect correct memory usage.

Thanks. Pushed as commit 3c71cab7c5bfba0549d0a1716e7061d07eafd794

Cheers,

Mark


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-05-12 21:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-30 14:39 [PATCH] ar: Replace one alloca use by xmalloc Mark Wielaard
2024-05-10 21:16 ` Aaron Merey
2024-05-12 21:55   ` Mark Wielaard

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).