btrfs-progs: Add further checks to btrfs replace start command
[platform/upstream/btrfs-progs.git] / cmds-receive.c
index 8dcdd29..d4b3103 100644 (file)
@@ -28,6 +28,7 @@
 #include <wait.h>
 #include <assert.h>
 #include <getopt.h>
+#include <limits.h>
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -56,15 +57,20 @@ struct btrfs_receive
        int dest_dir_fd;
 
        int write_fd;
-       char *write_path;
+       char write_path[PATH_MAX];
 
        char *root_path;
        char *dest_dir_path; /* relative to root_path */
-       char *full_subvol_path;
+       char full_subvol_path[PATH_MAX];
        char *full_root_path;
        int dest_dir_chroot;
 
        struct subvol_info cur_subvol;
+       /*
+        * Substitute for cur_subvol::path which is a pointer and we cannot
+        * change it to an array as it's a public API.
+        */
+       char cur_subvol_path[PATH_MAX];
 
        struct subvol_uuid_search sus;
 
@@ -87,15 +93,15 @@ static int finish_subvol(struct btrfs_receive *r)
        char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
        u64 flags;
 
-       if (r->cur_subvol.path == NULL)
+       if (r->cur_subvol_path[0] == 0)
                return 0;
 
-       subvol_fd = openat(r->mnt_fd, r->cur_subvol.path,
+       subvol_fd = openat(r->mnt_fd, r->cur_subvol_path,
                        O_RDONLY | O_NOATIME);
        if (subvol_fd < 0) {
                ret = -errno;
                fprintf(stderr, "ERROR: open %s failed. %s\n",
-                               r->cur_subvol.path, strerror(-ret));
+                               r->cur_subvol_path, strerror(-ret));
                goto out;
        }
 
@@ -139,9 +145,8 @@ static int finish_subvol(struct btrfs_receive *r)
        ret = 0;
 
 out:
-       if (r->cur_subvol.path) {
-               free(r->cur_subvol.path);
-               r->cur_subvol.path = NULL;
+       if (r->cur_subvol_path[0]) {
+               r->cur_subvol_path[0] = 0;
        }
        if (subvol_fd != -1)
                close(subvol_fd);
@@ -161,13 +166,24 @@ static int process_subvol(const char *path, const u8 *uuid, u64 ctransid,
                goto out;
 
        BUG_ON(r->cur_subvol.path);
+       BUG_ON(r->cur_subvol_path[0]);
 
-       if (strlen(r->dest_dir_path) == 0)
-               r->cur_subvol.path = strdup(path);
-       else
-               r->cur_subvol.path = path_cat(r->dest_dir_path, path);
-       free(r->full_subvol_path);
-       r->full_subvol_path = path_cat3(r->root_path, r->dest_dir_path, path);
+       if (strlen(r->dest_dir_path) == 0) {
+               strncpy_null(r->cur_subvol_path, path);
+       } else {
+               ret = path_cat_out(r->cur_subvol_path, r->dest_dir_path, path);
+               if (ret < 0) {
+                       fprintf(stderr, "ERROR: subvol: path invalid: %s\n",
+                                       path);
+                       goto out;
+               }
+       }
+       ret = path_cat3_out(r->full_subvol_path, r->root_path,
+                       r->dest_dir_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: subvol: path invalid: %s\n", path);
+               goto out;
+       }
 
        fprintf(stderr, "At subvol %s\n", path);
 
@@ -210,13 +226,24 @@ static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid,
                goto out;
 
        BUG_ON(r->cur_subvol.path);
+       BUG_ON(r->cur_subvol_path[0]);
 
-       if (strlen(r->dest_dir_path) == 0)
-               r->cur_subvol.path = strdup(path);
-       else
-               r->cur_subvol.path = path_cat(r->dest_dir_path, path);
-       free(r->full_subvol_path);
-       r->full_subvol_path = path_cat3(r->root_path, r->dest_dir_path, path);
+       if (strlen(r->dest_dir_path) == 0) {
+               strncpy_null(r->cur_subvol_path, path);
+       } else {
+               ret = path_cat_out(r->cur_subvol_path, r->dest_dir_path, path);
+               if (ret < 0) {
+                       fprintf(stderr, "ERROR: snapshot: path invalid: %s\n",
+                                       path);
+                       goto out;
+               }
+       }
+       ret = path_cat3_out(r->full_subvol_path, r->root_path,
+                       r->dest_dir_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: snapshot: path invalid: %s\n", path);
+               goto out;
+       }
 
        fprintf(stdout, "At snapshot %s\n", path);
 
@@ -339,7 +366,13 @@ static int process_mkfile(const char *path, void *user)
 {
        int ret;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: mkfile: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "mkfile %s\n", path);
@@ -355,7 +388,6 @@ static int process_mkfile(const char *path, void *user)
        ret = 0;
 
 out:
-       free(full_path);
        return ret;
 }
 
@@ -363,7 +395,13 @@ static int process_mkdir(const char *path, void *user)
 {
        int ret;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: mkdir: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "mkdir %s\n", path);
@@ -375,7 +413,7 @@ static int process_mkdir(const char *path, void *user)
                                strerror(-ret));
        }
 
-       free(full_path);
+out:
        return ret;
 }
 
@@ -383,7 +421,13 @@ static int process_mknod(const char *path, u64 mode, u64 dev, void *user)
 {
        int ret;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: mknod: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "mknod %s mode=%llu, dev=%llu\n",
@@ -396,7 +440,7 @@ static int process_mknod(const char *path, u64 mode, u64 dev, void *user)
                                strerror(-ret));
        }
 
-       free(full_path);
+out:
        return ret;
 }
 
@@ -404,7 +448,13 @@ static int process_mkfifo(const char *path, void *user)
 {
        int ret;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: mkfifo: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "mkfifo %s\n", path);
@@ -416,7 +466,7 @@ static int process_mkfifo(const char *path, void *user)
                                strerror(-ret));
        }
 
-       free(full_path);
+out:
        return ret;
 }
 
@@ -424,7 +474,13 @@ static int process_mksock(const char *path, void *user)
 {
        int ret;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: mksock: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "mksock %s\n", path);
@@ -436,7 +492,7 @@ static int process_mksock(const char *path, void *user)
                                strerror(-ret));
        }
 
-       free(full_path);
+out:
        return ret;
 }
 
@@ -444,7 +500,13 @@ static int process_symlink(const char *path, const char *lnk, void *user)
 {
        int ret;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: symlink: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "symlink %s -> %s\n", path, lnk);
@@ -456,7 +518,7 @@ static int process_symlink(const char *path, const char *lnk, void *user)
                                lnk, strerror(-ret));
        }
 
-       free(full_path);
+out:
        return ret;
 }
 
@@ -464,8 +526,22 @@ static int process_rename(const char *from, const char *to, void *user)
 {
        int ret;
        struct btrfs_receive *r = user;
-       char *full_from = path_cat(r->full_subvol_path, from);
-       char *full_to = path_cat(r->full_subvol_path, to);
+       char full_from[PATH_MAX];
+       char full_to[PATH_MAX];
+
+       ret = path_cat_out(full_from, r->full_subvol_path, from);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: rename: source path invalid: %s\n",
+                               from);
+               goto out;
+       }
+
+       ret = path_cat_out(full_to, r->full_subvol_path, to);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: rename: target path invalid: %s\n",
+                               to);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "rename %s -> %s\n", from, to);
@@ -477,8 +553,7 @@ static int process_rename(const char *from, const char *to, void *user)
                                to, strerror(-ret));
        }
 
-       free(full_from);
-       free(full_to);
+out:
        return ret;
 }
 
@@ -486,8 +561,22 @@ static int process_link(const char *path, const char *lnk, void *user)
 {
        int ret;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
-       char *full_link_path = path_cat(r->full_subvol_path, lnk);
+       char full_path[PATH_MAX];
+       char full_link_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: link: source path invalid: %s\n",
+                               full_path);
+               goto out;
+       }
+
+       ret = path_cat_out(full_link_path, r->full_subvol_path, lnk);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: link: target path invalid: %s\n",
+                               full_link_path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "link %s -> %s\n", path, lnk);
@@ -499,8 +588,7 @@ static int process_link(const char *path, const char *lnk, void *user)
                                lnk, strerror(-ret));
        }
 
-       free(full_path);
-       free(full_link_path);
+out:
        return ret;
 }
 
@@ -509,7 +597,13 @@ static int process_unlink(const char *path, void *user)
 {
        int ret;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: unlink: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "unlink %s\n", path);
@@ -521,7 +615,7 @@ static int process_unlink(const char *path, void *user)
                                strerror(-ret));
        }
 
-       free(full_path);
+out:
        return ret;
 }
 
@@ -529,7 +623,13 @@ static int process_rmdir(const char *path, void *user)
 {
        int ret;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: rmdir: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "rmdir %s\n", path);
@@ -541,11 +641,10 @@ static int process_rmdir(const char *path, void *user)
                                strerror(-ret));
        }
 
-       free(full_path);
+out:
        return ret;
 }
 
-
 static int open_inode_for_write(struct btrfs_receive *r, const char *path)
 {
        int ret = 0;
@@ -564,8 +663,7 @@ static int open_inode_for_write(struct btrfs_receive *r, const char *path)
                                strerror(-ret));
                goto out;
        }
-       free(r->write_path);
-       r->write_path = strdup(path);
+       strncpy_null(r->write_path, path);
 
 out:
        return ret;
@@ -586,10 +684,16 @@ static int process_write(const char *path, const void *data, u64 offset,
 {
        int ret = 0;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
        u64 pos = 0;
        int w;
 
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: write: path invalid: %s\n", path);
+               goto out;
+       }
+
        ret = open_inode_for_write(r, full_path);
        if (ret < 0)
                goto out;
@@ -607,7 +711,6 @@ static int process_write(const char *path, const void *data, u64 offset,
        }
 
 out:
-       free(full_path);
        return ret;
 }
 
@@ -620,11 +723,18 @@ static int process_clone(const char *path, u64 offset, u64 len,
        struct btrfs_receive *r = user;
        struct btrfs_ioctl_clone_range_args clone_args;
        struct subvol_info *si = NULL;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
        char *subvol_path = NULL;
-       char *full_clone_path = NULL;
+       char full_clone_path[PATH_MAX];
        int clone_fd = -1;
 
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: clone: source path invalid: %s\n",
+                               path);
+               goto out;
+       }
+
        ret = open_inode_for_write(r, full_path);
        if (ret < 0)
                goto out;
@@ -635,7 +745,7 @@ static int process_clone(const char *path, u64 offset, u64 len,
                if (memcmp(clone_uuid, r->cur_subvol.received_uuid,
                                BTRFS_UUID_SIZE) == 0) {
                        /* TODO check generation of extent */
-                       subvol_path = strdup(r->cur_subvol.path);
+                       subvol_path = strdup(r->cur_subvol_path);
                } else {
                        ret = -ENOENT;
                        fprintf(stderr, "ERROR: did not find source subvol.\n");
@@ -660,7 +770,12 @@ static int process_clone(const char *path, u64 offset, u64 len,
                subvol_path = strdup(si->path);
        }
 
-       full_clone_path = path_cat(subvol_path, clone_path);
+       ret = path_cat_out(full_clone_path, subvol_path, clone_path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: clone: target path invalid: %s\n",
+                               clone_path);
+               goto out;
+       }
 
        clone_fd = openat(r->mnt_fd, full_clone_path, O_RDONLY | O_NOATIME);
        if (clone_fd < 0) {
@@ -687,8 +802,6 @@ out:
                free(si->path);
                free(si);
        }
-       free(full_path);
-       free(full_clone_path);
        free(subvol_path);
        if (clone_fd != -1)
                close(clone_fd);
@@ -701,7 +814,13 @@ static int process_set_xattr(const char *path, const char *name,
 {
        int ret = 0;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: set_xattr: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (strcmp("security.capability", name) == 0) {
                if (g_verbose >= 3)
@@ -736,7 +855,6 @@ static int process_set_xattr(const char *path, const char *name,
        }
 
 out:
-       free(full_path);
        return ret;
 }
 
@@ -744,7 +862,14 @@ static int process_remove_xattr(const char *path, const char *name, void *user)
 {
        int ret = 0;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: remove_xattr: path invalid: %s\n",
+                               path);
+               goto out;
+       }
 
        if (g_verbose >= 2) {
                fprintf(stderr, "remove_xattr %s - name=%s\n",
@@ -760,7 +885,6 @@ static int process_remove_xattr(const char *path, const char *name, void *user)
        }
 
 out:
-       free(full_path);
        return ret;
 }
 
@@ -768,7 +892,13 @@ static int process_truncate(const char *path, u64 size, void *user)
 {
        int ret = 0;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: truncate: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "truncate %s size=%llu\n", path, size);
@@ -782,7 +912,6 @@ static int process_truncate(const char *path, u64 size, void *user)
        }
 
 out:
-       free(full_path);
        return ret;
 }
 
@@ -790,7 +919,13 @@ static int process_chmod(const char *path, u64 mode, void *user)
 {
        int ret = 0;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: chmod: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "chmod %s - mode=0%o\n", path, (int)mode);
@@ -804,7 +939,6 @@ static int process_chmod(const char *path, u64 mode, void *user)
        }
 
 out:
-       free(full_path);
        return ret;
 }
 
@@ -812,7 +946,13 @@ static int process_chown(const char *path, u64 uid, u64 gid, void *user)
 {
        int ret = 0;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
+
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: chown: path invalid: %s\n", path);
+               goto out;
+       }
 
        if (g_verbose >= 2)
                fprintf(stderr, "chown %s - uid=%llu, gid=%llu\n", path,
@@ -844,7 +984,6 @@ static int process_chown(const char *path, u64 uid, u64 gid, void *user)
        }
 
 out:
-       free(full_path);
        return ret;
 }
 
@@ -854,9 +993,15 @@ static int process_utimes(const char *path, struct timespec *at,
 {
        int ret = 0;
        struct btrfs_receive *r = user;
-       char *full_path = path_cat(r->full_subvol_path, path);
+       char full_path[PATH_MAX];
        struct timespec tv[2];
 
+       ret = path_cat_out(full_path, r->full_subvol_path, path);
+       if (ret < 0) {
+               fprintf(stderr, "ERROR: utimes: path invalid: %s\n", path);
+               goto out;
+       }
+
        if (g_verbose >= 2)
                fprintf(stderr, "utimes %s\n", path);
 
@@ -871,7 +1016,6 @@ static int process_utimes(const char *path, struct timespec *at,
        }
 
 out:
-       free(full_path);
        return ret;
 }
 
@@ -920,7 +1064,7 @@ static int do_receive(struct btrfs_receive *r, const char *tomnt,
        u64 subvol_id;
        int ret;
        char *dest_dir_full_path;
-       char *root_subvol_path;
+       char root_subvol_path[PATH_MAX];
        int end = 0;
 
        dest_dir_full_path = realpath(tomnt, NULL);
@@ -939,7 +1083,7 @@ static int do_receive(struct btrfs_receive *r, const char *tomnt,
                goto out;
        }
 
-       if (realmnt) {
+       if (realmnt[0]) {
                r->root_path = realmnt;
        } else {
                ret = find_mount_root(dest_dir_full_path, &r->root_path);
@@ -978,14 +1122,7 @@ static int do_receive(struct btrfs_receive *r, const char *tomnt,
                goto out;
        }
 
-       root_subvol_path = malloc(PATH_MAX);
-       if (!root_subvol_path) {
-               ret = -ENOMEM;
-               fprintf(stderr, "ERROR: couldn't allocate buffer for the root "
-                       "subvol path\n");
-               goto out;
-       }
-
+       root_subvol_path[0] = 0;
        ret = btrfs_subvolid_resolve(r->mnt_fd, root_subvol_path,
                                     PATH_MAX, subvol_id);
        if (ret) {
@@ -999,8 +1136,6 @@ static int do_receive(struct btrfs_receive *r, const char *tomnt,
         */
        if (strlen(root_subvol_path))
                r->full_root_path = root_subvol_path;
-       else
-               free(root_subvol_path);
 
        if (r->dest_dir_chroot) {
                if (chroot(dest_dir_full_path)) {
@@ -1067,16 +1202,8 @@ out:
        }
        free(r->root_path);
        r->root_path = NULL;
-       free(r->write_path);
-       r->write_path = NULL;
-       free(r->full_subvol_path);
-       r->full_subvol_path = NULL;
        r->dest_dir_path = NULL;
        free(dest_dir_full_path);
-       if (r->cur_subvol.path) {
-               free(r->cur_subvol.path);
-               r->cur_subvol.path = NULL;
-       }
        subvol_uuid_search_finit(&r->sus);
        if (r->mnt_fd != -1) {
                close(r->mnt_fd);
@@ -1086,18 +1213,15 @@ out:
                close(r->dest_dir_fd);
                r->dest_dir_fd = -1;
        }
-       if (r->full_root_path) {
-               free(r->full_root_path);
-               r->full_root_path = NULL;
-       }
+
        return ret;
 }
 
 int cmd_receive(int argc, char **argv)
 {
        char *tomnt = NULL;
-       char *fromfile = NULL;
-       char *realmnt = NULL;
+       char fromfile[PATH_MAX];
+       char realmnt[PATH_MAX];
        struct btrfs_receive r;
        int receive_fd = fileno(stdin);
        u64 max_errors = 1;
@@ -1108,6 +1232,8 @@ int cmd_receive(int argc, char **argv)
        r.write_fd = -1;
        r.dest_dir_fd = -1;
        r.dest_dir_chroot = 0;
+       realmnt[0] = 0;
+       fromfile[0] = 0;
 
        while (1) {
                int c;
@@ -1126,7 +1252,13 @@ int cmd_receive(int argc, char **argv)
                        g_verbose++;
                        break;
                case 'f':
-                       fromfile = optarg;
+                       if (arg_copy_path(fromfile, optarg, sizeof(fromfile))) {
+                               fprintf(stderr,
+                                   "ERROR: input file path too long (%zu)\n",
+                                   strlen(optarg));
+                               ret = 1;
+                               goto out;
+                       }
                        break;
                case 'e':
                        r.honor_end_cmd = 1;
@@ -1138,10 +1270,11 @@ int cmd_receive(int argc, char **argv)
                        max_errors = arg_strtou64(optarg);
                        break;
                case 'm':
-                       free(realmnt);
-                       realmnt = strdup(optarg);
-                       if (!realmnt) {
-                               fprintf(stderr, "ERROR: couldn't allocate realmnt.\n");
+                       if (arg_copy_path(realmnt, optarg, sizeof(realmnt))) {
+                               fprintf(stderr,
+                                   "ERROR: mount point path too long (%zu)\n",
+                                   strlen(optarg));
+                               ret = 1;
                                goto out;
                        }
                        break;
@@ -1157,7 +1290,7 @@ int cmd_receive(int argc, char **argv)
 
        tomnt = argv[optind];
 
-       if (fromfile) {
+       if (fromfile[0]) {
                receive_fd = open(fromfile, O_RDONLY | O_NOATIME);
                if (receive_fd < 0) {
                        fprintf(stderr, "ERROR: failed to open %s\n", fromfile);
@@ -1168,7 +1301,6 @@ int cmd_receive(int argc, char **argv)
        ret = do_receive(&r, tomnt, realmnt, receive_fd, max_errors);
 
 out:
-       free(realmnt);
 
        return !!ret;
 }