Btrfs-progs: Fix a small memory leak in managing the btrfs list filter
authorAnand Jain <anand.jain@oracle.com>
Fri, 1 Feb 2013 07:56:31 +0000 (15:56 +0800)
committerDavid Sterba <dsterba@suse.cz>
Fri, 1 Feb 2013 15:55:06 +0000 (16:55 +0100)
Signed-off-by: Anand Jain <anand.jain@oracle.com>
cmds-subvolume.c

index bee7829..c1ccc7a 100644 (file)
@@ -306,9 +306,9 @@ static int cmd_subvol_list(int argc, char **argv)
        struct btrfs_list_filter_set *filter_set;
        struct btrfs_list_comparer_set *comparer_set;
        u64 flags = 0;
-       int fd;
+       int fd = -1;
        u64 top_id;
-       int ret;
+       int ret = -1, uerr = 0;
        int c;
        char *subvol;
        int is_tab_result = 0;
@@ -363,8 +363,10 @@ static int cmd_subvol_list(int argc, char **argv)
                        ret = btrfs_list_parse_filter_string(optarg,
                                                        &filter_set,
                                                        BTRFS_LIST_FILTER_GEN);
-                       if (ret)
-                               usage(cmd_subvol_list_usage);
+                       if (ret) {
+                               uerr = 1;
+                               goto out;
+                       }
                        break;
 
                case 'c':
@@ -372,18 +374,23 @@ static int cmd_subvol_list(int argc, char **argv)
                        ret = btrfs_list_parse_filter_string(optarg,
                                                        &filter_set,
                                                        BTRFS_LIST_FILTER_CGEN);
-                       if (ret)
-                               usage(cmd_subvol_list_usage);
+                       if (ret) {
+                               uerr = 1;
+                               goto out;
+                       }
                        break;
                case 'S':
                        ret = btrfs_list_parse_sort_string(optarg,
                                                           &comparer_set);
-                       if (ret)
-                               usage(cmd_subvol_list_usage);
+                       if (ret) {
+                               uerr = 1;
+                               goto out;
+                       }
                        break;
 
                default:
-                       usage(cmd_subvol_list_usage);
+                       uerr = 1;
+                       goto out;
                }
        }
 
@@ -391,25 +398,29 @@ static int cmd_subvol_list(int argc, char **argv)
                btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
                                        flags);
 
-       if (check_argc_exact(argc - optind, 1))
-               usage(cmd_subvol_list_usage);
+       if (check_argc_exact(argc - optind, 1)) {
+               uerr = 1;
+               goto out;
+       }
 
        subvol = argv[optind];
 
        ret = test_issubvolume(subvol);
        if (ret < 0) {
                fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
-               return 12;
+               goto out;
        }
        if (!ret) {
                fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
-               return 13;
+               ret = -1;
+               goto out;
        }
 
        fd = open_file_or_dir(subvol);
        if (fd < 0) {
+               ret = -1;
                fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
-               return 12;
+               goto out;
        }
 
        top_id = btrfs_list_get_path_rootid(fd);
@@ -437,9 +448,16 @@ static int cmd_subvol_list(int argc, char **argv)
                ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
                                BTRFS_LIST_LAYOUT_DEFAULT,
                                !is_list_all && !is_only_in_path, NULL);
-       if (ret)
-               return 19;
-       return 0;
+
+out:
+       if (filter_set)
+               btrfs_list_free_filter_set(filter_set);
+       if (comparer_set)
+               btrfs_list_free_comparer_set(comparer_set);
+       if (uerr)
+               usage(cmd_subvol_list_usage);
+
+       return ret;
 }
 
 static const char * const cmd_snapshot_usage[] = {
@@ -650,6 +668,9 @@ static int cmd_subvol_get_default(int argc, char **argv)
 
        ret = btrfs_list_subvols_print(fd, filter_set, NULL,
                BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
+
+       if (filter_set)
+               btrfs_list_free_filter_set(filter_set);
        if (ret)
                return 19;
        return 0;
@@ -869,6 +890,8 @@ static int cmd_subvol_show(int argc, char **argv)
                free(get_ri.name);
        if (get_ri.full_path)
                free(get_ri.full_path);
+       if (filter_set)
+               btrfs_list_free_filter_set(filter_set);
 
 out:
        if (mntfd >= 0)