From b6a77a272b1a687f90999fe7aacf7a85523f0aa6 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Fri, 12 Jun 2015 17:06:48 +0200 Subject: btrfs-progs: and new path_cat helpers to send utils 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 --- send-utils.c | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'send-utils.c') diff --git a/send-utils.c b/send-utils.c index e342f71..c3d8c3e 100644 --- a/send-utils.c +++ b/send-utils.c @@ -21,6 +21,7 @@ #include #include #include +#include #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; } -- cgit v1.2.1