diff mbox

[13/17] btrfs-progs: close fd on cmd_subvol_get_default return

Message ID 1361832890-40921-14-git-send-email-sandeen@redhat.com (mailing list archive)
State Under Review, archived
Headers show

Commit Message

Eric Sandeen Feb. 25, 2013, 10:54 p.m. UTC
Without this we leak the fd when we return from the
function.

Also, remove the senseless random return values.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---
 cmds-subvolume.c |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)
diff mbox

Patch

diff --git a/cmds-subvolume.c b/cmds-subvolume.c
index f9258fc..0dfaefe 100644
--- a/cmds-subvolume.c
+++ b/cmds-subvolume.c
@@ -624,7 +624,7 @@  static const char * const cmd_subvol_get_default_usage[] = {
 
 static int cmd_subvol_get_default(int argc, char **argv)
 {
-	int fd;
+	int fd = -1;
 	int ret;
 	char *subvol;
 	struct btrfs_list_filter_set *filter_set;
@@ -638,35 +638,36 @@  static int cmd_subvol_get_default(int argc, char **argv)
 	ret = test_issubvolume(subvol);
 	if (ret < 0) {
 		fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
-		return 12;
+		return 1;
 	}
 	if (!ret) {
 		fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
-		return 13;
+		return 1;
 	}
 
 	fd = open_file_or_dir(subvol);
 	if (fd < 0) {
 		fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
-		return 12;
+		return 1;
 	}
 
 	ret = btrfs_list_get_default_subvolume(fd, &default_id);
 	if (ret) {
 		fprintf(stderr, "ERROR: can't perform the search - %s\n",
 			strerror(errno));
-		return ret;
+		goto out;
 	}
 
+	ret = 1;
 	if (default_id == 0) {
 		fprintf(stderr, "ERROR: 'default' dir item not found\n");
-		return ret;
+		goto out;
 	}
 
 	/* no need to resolve roots if FS_TREE is default */
 	if (default_id == BTRFS_FS_TREE_OBJECTID) {
 		printf("ID 5 (FS_TREE)\n");
-		return ret;
+		goto out;
 	}
 
 	filter_set = btrfs_list_alloc_filter_set();
@@ -684,8 +685,11 @@  static int cmd_subvol_get_default(int argc, char **argv)
 
 	if (filter_set)
 		btrfs_list_free_filter_set(filter_set);
+out:
+	if (fd != -1)
+		close(fd);
 	if (ret)
-		return 19;
+		return 1;
 	return 0;
 }