[PATCH] ar: Replace one alloca use by xmalloc

Mark Wielaard mark@klomp.org
Tue Apr 30 14:39:17 GMT 2024


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



More information about the Elfutils-devel mailing list