btrfs-progs: don't leak fd in resize
authorZach Brown <zab@redhat.com>
Wed, 23 Jan 2013 23:11:24 +0000 (15:11 -0800)
committerZach Brown <zab@redhat.com>
Wed, 6 Feb 2013 00:09:40 +0000 (16:09 -0800)
Argument validation failed to close the fd that was opened.  So check
the arguments before opening anything.

Signed-off-by: Zach Brown <zab@redhat.com>
cmds-filesystem.c

index 507239a..045c896 100644 (file)
@@ -464,11 +464,6 @@ static int cmd_resize(int argc, char **argv)
        amount = argv[1];
        path = argv[2];
 
-       fd = open_file_or_dir(path);
-       if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access to '%s'\n", path);
-               return 12;
-       }
        len = strlen(amount);
        if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
                fprintf(stderr, "ERROR: size value too long ('%s)\n",
@@ -476,6 +471,12 @@ static int cmd_resize(int argc, char **argv)
                return 14;
        }
 
+       fd = open_file_or_dir(path);
+       if (fd < 0) {
+               fprintf(stderr, "ERROR: can't access to '%s'\n", path);
+               return 12;
+       }
+
        printf("Resize '%s' of '%s'\n", path, amount);
        strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
        args.name[BTRFS_PATH_NAME_MAX-1] = 0;