btrfs-progs: and new path_cat helpers to send utils
authorDavid Sterba <dsterba@suse.cz>
Fri, 12 Jun 2015 15:06:48 +0000 (17:06 +0200)
committerDavid Sterba <dsterba@suse.cz>
Mon, 15 Jun 2015 12:09:11 +0000 (14:09 +0200)
Add versions of path_cat and path_cat3 that do not allocate the memory.
The unhandled memory allocations are still there.

Signed-off-by: David Sterba <dsterba@suse.cz>
send-utils.c
send-utils.h

index e342f71..c3d8c3e 100644 (file)
@@ -21,6 +21,7 @@
 #include <sys/ioctl.h>
 #include <uuid/uuid.h>
 #include <limits.h>
+#include <errno.h>
 
 #include "ctree.h"
 #include "send-utils.h"
@@ -709,26 +710,42 @@ void subvol_uuid_search_finit(struct subvol_uuid_search *s)
 }
 #endif
 
-char *path_cat(const char *p1, const char *p2)
+int path_cat_out(char *out, const char *p1, const char *p2)
 {
        int p1_len = strlen(p1);
        int p2_len = strlen(p2);
-       char *new = malloc(p1_len + p2_len + 2);
+
+       if (p1_len + p2_len + 2 >= PATH_MAX)
+               return -ENAMETOOLONG;
 
        if (p1_len && p1[p1_len - 1] == '/')
                p1_len--;
        if (p2_len && p2[p2_len - 1] == '/')
                p2_len--;
-       sprintf(new, "%.*s/%.*s", p1_len, p1, p2_len, p2);
+       sprintf(out, "%.*s/%.*s", p1_len, p1, p2_len, p2);
+
+       return 0;
+}
+
+char *path_cat(const char *p1, const char *p2)
+{
+       int p1_len = strlen(p1);
+       int p2_len = strlen(p2);
+       char *new = malloc(p1_len + p2_len + 2);
+
+       path_cat_out(new, p1, p2);
+
        return new;
 }
 
-char *path_cat3(const char *p1, const char *p2, const char *p3)
+int path_cat3_out(char *out, const char *p1, const char *p2, const char *p3)
 {
        int p1_len = strlen(p1);
        int p2_len = strlen(p2);
        int p3_len = strlen(p3);
-       char *new = malloc(p1_len + p2_len + p3_len + 3);
+
+       if (p1_len + p2_len + p3_len + 3 >= PATH_MAX)
+               return -ENAMETOOLONG;
 
        if (p1_len && p1[p1_len - 1] == '/')
                p1_len--;
@@ -736,6 +753,19 @@ char *path_cat3(const char *p1, const char *p2, const char *p3)
                p2_len--;
        if (p3_len && p3[p3_len - 1] == '/')
                p3_len--;
-       sprintf(new, "%.*s/%.*s/%.*s", p1_len, p1, p2_len, p2, p3_len, p3);
+       sprintf(out, "%.*s/%.*s/%.*s", p1_len, p1, p2_len, p2, p3_len, p3);
+
+       return 0;
+}
+
+char *path_cat3(const char *p1, const char *p2, const char *p3)
+{
+       int p1_len = strlen(p1);
+       int p2_len = strlen(p2);
+       int p3_len = strlen(p3);
+       char *new = malloc(p1_len + p2_len + p3_len + 3);
+
+       path_cat3_out(new, p1, p2, p3);
+
        return new;
 }
index d28c8d5..32e4fdc 100644 (file)
@@ -90,7 +90,9 @@ void subvol_uuid_search_add(struct subvol_uuid_search *s,
 int btrfs_subvolid_resolve(int fd, char *path, size_t path_len, u64 subvol_id);
 
 char *path_cat(const char *p1, const char *p2);
+int path_cat_out(char *out, const char *p1, const char *p2);
 char *path_cat3(const char *p1, const char *p2, const char *p3);
+int path_cat3_out(char *out, const char *p1, const char *p2, const char *p3);
 
 #ifdef __cplusplus
 }