opkg/libbb: Patch from Esben Haabendal <esbenhaabendal gmail com>
authorticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Mon, 15 Dec 2008 04:53:20 +0000 (04:53 +0000)
committerticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Mon, 15 Dec 2008 04:53:20 +0000 (04:53 +0000)
Fix the usage of dirname() in libbb/make_directory.c, as it is not correct according to the standard specification for dirname.

git-svn-id: http://opkg.googlecode.com/svn/trunk@48 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358

libbb/make_directory.c

index 797ce270c436e3d486cb92ba7a8a93600c3c0acc..5fa2a7ebe180f611d086de4c606598f2955bdd21 100644 (file)
@@ -50,17 +50,25 @@ int make_directory (const char *path, long mode, int flags)
 
                if (stat (path, &st) < 0 && errno == ENOENT) {
                        int status;
 
                if (stat (path, &st) < 0 && errno == ENOENT) {
                        int status;
-                       char *buf, *parent;
+                       char *pathcopy, *parent, *parentcopy;
                        mode_t mask;
 
                        mask = umask (0);
                        umask (mask);
 
                        mode_t mask;
 
                        mask = umask (0);
                        umask (mask);
 
-                       buf = xstrdup (path);
-                       parent = dirname (buf);
-                       status = make_directory (parent, (0777 & ~mask) | 0300,
-                                       FILEUTILS_RECUR);
-                       free (buf);
+                       /* dirname is unsafe, it may both modify the
+                          memory of the path argument and may return
+                          a pointer to static memory, which can then
+                          be modified by consequtive calls to dirname */
+                       
+                       pathcopy = xstrdup (path);
+                       parent = dirname (pathcopy);
+                       parentcopy = xstrdup (parent);
+                       status = make_directory (parentcopy, (0777 & ~mask)
+                                                                        | 0300, FILEUTILS_RECUR);
+                       free (pathcopy);
+                       free (parentcopy);
+
 
                        if (status < 0 || make_directory (path, mode, 0) < 0)
                                return -1;
 
                        if (status < 0 || make_directory (path, mode, 0) < 0)
                                return -1;