diff options
-rw-r--r-- | send-utils.c | 42 | ||||
-rw-r--r-- | send-utils.h | 2 |
2 files changed, 38 insertions, 6 deletions
diff --git a/send-utils.c b/send-utils.c index e342f71a..c3d8c3e0 100644 --- a/send-utils.c +++ b/send-utils.c @@ -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; } diff --git a/send-utils.h b/send-utils.h index d28c8d51..32e4fdcd 100644 --- a/send-utils.h +++ b/send-utils.h @@ -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 } |