]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
unstrip: Stop using strndupa.
authorÉrico Rolim <erico.erc@gmail.com>
Tue, 27 Oct 2020 19:19:58 +0000 (16:19 -0300)
committerMark Wielaard <mark@klomp.org>
Tue, 27 Oct 2020 21:49:33 +0000 (22:49 +0100)
This functon has inherent safety issues, since a long enough path can
lead to memory clobbering. Due to the recursive nature of
make_directories(), multiple calls could also stack overflow. Instead,
the string can be allocated in the heap.

As a bonus, this improves musl compatibility, since musl doesn't include
the strndupa macro for now.

Also add braces around while loop.

Signed-off-by: Érico Rolim <erico.erc@gmail.com>
src/ChangeLog
src/unstrip.c

index 112b2242a7172d25d5bc8d807b1dafc3b72c3c3b..7f923c5d6a5f96484bfc1a858f327256831d52b4 100644 (file)
@@ -1,3 +1,7 @@
+2020-10-27  Érico N. Rolim  <erico.erc@gmail.com>
+
+       * unstrip.c (make_directories): Use strndup, not strndupa.
+
 2020-09-28  Mark Wielaard  <mark@klomp.org>
 
        * readelf.c (next_listptr_offset): Take idx as pointer, to be updated
index a855038af85b4b8715135f3b6d2a91e3105e0b79..0257d9cc374034b8fbadd30151c8eb2af7f916ec 100644 (file)
@@ -311,12 +311,18 @@ make_directories (const char *path)
   if (lastslash == path)
     return;
 
-  char *dir = strndupa (path, lastslash - path);
+  char *dir = strndup (path, lastslash - path);
+  if (dir == NULL)
+    error(EXIT_FAILURE, errno, _("memory exhausted"));
+
   while (mkdir (dir, 0777) < 0 && errno != EEXIST)
-    if (errno == ENOENT)
-      make_directories (dir);
-    else
-      error (EXIT_FAILURE, errno, _("cannot create directory '%s'"), dir);
+    {
+      if (errno == ENOENT)
+        make_directories (dir);
+      else
+        error (EXIT_FAILURE, errno, _("cannot create directory '%s'"), dir);
+    }
+  free (dir);
 }
 
 /* Keep track of new section data we are creating, so we can free it