Fix a memory leak if parent directory creation failed.
[oweals/busybox.git] / libbb / concat_path_file.c
index d53dc0e2ed934708faccba770103c8ad6593115c..86dd2fbbf8678feac063a31e5f91d100cc1881e4 100644 (file)
@@ -6,19 +6,21 @@
  *
 */
 
+#include <string.h>
 #include "libbb.h"
 
 extern char *concat_path_file(const char *path, const char *filename)
 {
        char *outbuf;
-       int  l;
-       int  flg_slash = 1;
+       char *lc;
+
+       if (!path)
+           path="";
+       lc = last_char_is(path, '/');
+       while (*filename == '/')
+               filename++;
+       outbuf = xmalloc(strlen(path)+strlen(filename)+1+(lc==NULL));
+       sprintf(outbuf, "%s%s%s", path, (lc==NULL)? "/" : "", filename);
 
-       l = strlen(path);
-       if(l>0 && path[l-1] == '/')
-               flg_slash--;
-       l += strlen(filename);
-       outbuf = xmalloc(l+1+flg_slash);
-       sprintf(outbuf, (flg_slash ? "%s/%s" : "%s%s"), path, filename);
        return outbuf;
 }