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