btrfs-progs: tests: fssum, fix memory leak
diff mbox

Message ID 20170426012241.29814-1-lufq.fnst@cn.fujitsu.com
State New
Headers show

Commit Message

Lu Fengqi April 26, 2017, 1:22 a.m. UTC
Free the alloced memory and close dir before exit.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
---
 tests/fssum.c | 35 ++++++++++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 5 deletions(-)

Comments

David Sterba May 2, 2017, 2:25 p.m. UTC | #1
On Wed, Apr 26, 2017 at 09:22:41AM +0800, Lu Fengqi wrote:
> Free the alloced memory and close dir before exit.
> 
> Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>

Applied, thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch
diff mbox

diff --git a/tests/fssum.c b/tests/fssum.c
index 83bd4106..8be44547 100644
--- a/tests/fssum.c
+++ b/tests/fssum.c
@@ -420,6 +420,9 @@  check_manifest(char *fn, char *m, char *c, int last_call)
 	while ((l = getln(line, sizeof(line), in_fp))) {
 		rem_c = strrchr(l, ' ');
 		if (!rem_c) {
+			if (checksum)
+				free(checksum);
+
 			/* final cs */
 			checksum = strdup(l);
 			break;
@@ -503,6 +506,7 @@  sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
 		}
 		++entries;
 	}
+
 	qsort(namelist, entries, sizeof(*namelist), namecmp);
 	for (i = 0; i < entries; ++i) {
 		struct stat64 st;
@@ -624,7 +628,11 @@  sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
 		sum_add_sum(dircs, &meta);
 next:
 		free(path);
+		free(namelist[i]);
 	}
+
+	free(namelist);
+	closedir(d);
 }
 
 int
@@ -636,7 +644,9 @@  main(int argc, char *argv[])
 	char *path;
 	int fd;
 	sum_t cs;
+	char *sumstring;
 	char flagstring[sizeof(flchar)];
+	int ret = 0;
 	int i;
 	int plen;
 	int elen;
@@ -736,6 +746,9 @@  main(int argc, char *argv[])
 		} else if ((p = strchr(l, ':'))) {
 			*p++ = 0;
 			parse_flags(l);
+
+			if (checksum)
+				free(checksum);
 			checksum = strdup(p);
 		} else {
 			fprintf(stderr, "invalid input file format\n");
@@ -798,16 +811,28 @@  main(int argc, char *argv[])
 		if (!gen_manifest)
 			fprintf(out_fp, "%s:", flagstring);
 
-		fprintf(out_fp, "%s\n", sum_to_string(&cs));
+		sumstring = sum_to_string(&cs);
+		fprintf(out_fp, "%s\n", sumstring);
+		free(sumstring);
 	} else {
-		if (strcmp(checksum, sum_to_string(&cs)) == 0) {
+		sumstring = sum_to_string(&cs);
+		if (strcmp(checksum, sumstring) == 0) {
 			printf("OK\n");
-			exit(0);
+			ret = 0;
 		} else {
 			printf("FAIL\n");
-			exit(1);
+			ret = 1;
 		}
+
+		free(checksum);
+		free(sumstring);
 	}
 
-	exit(0);
+	if (in_fp)
+		fclose(in_fp);
+
+	if (out_fp != stdout)
+		fclose(out_fp);
+
+	exit(ret);
 }