git » libjio » commit 793cc2c

There were several memory leaks inside jfsck(), they've probably been there for a very long time.

author Alberto Bertogli
2004-07-25 02:05:53 UTC
committer Alberto Bertogli
2007-07-15 13:16:57 UTC
parent 03fb278c6ddbc71ca3de14ddbe553dbad024ac10

There were several memory leaks inside jfsck(), they've probably been there for a very long time.

There were several memory leaks inside jfsck(), they've probably been there
for a very long time.

Mostly mmap without munmap, and some missing free() calls.

check.c +25 -4

diff --git a/check.c b/check.c
index 44a763c..f0ce63e 100644
--- a/check.c
+++ b/check.c
@@ -102,13 +102,17 @@ int jfsck(const char *name, struct jfsck_result *res)
 	struct stat sinfo;
 	struct jfs fs;
 	struct jtrans *curts;
+	struct joper *tmpop;
 	DIR *dir;
 	struct dirent *dent;
 	unsigned char *map;
 	off_t filelen;
 
 	fd = tfd = -1;
+	filelen = 0;
 	dir = NULL;
+	fs.jmap = NULL;
+	map = NULL;
 	ret = 0;
 
 	fd = open(name, O_RDWR | O_SYNC | O_LARGEFILE);
@@ -150,6 +154,7 @@ int jfsck(const char *name, struct jfsck_result *res)
 			PROT_READ | PROT_WRITE, MAP_SHARED, fs.jfd, 0);
 	if (fs.jmap == MAP_FAILED) {
 		ret = J_ENOJOURNAL;
+		fs.jmap = NULL;
 		goto exit;
 	}
 
@@ -216,6 +221,11 @@ int jfsck(const char *name, struct jfsck_result *res)
 
 		filelen = lseek(tfd, 0, SEEK_END);
 		map = mmap(0, filelen, PROT_READ, MAP_SHARED, tfd, 0);
+		if (map == MAP_FAILED) {
+			res->broken++;
+			map = NULL;
+			goto loop;
+		}
 		rv = fill_trans(map, filelen, curts);
 		if (rv != 1) {
 			res->broken++;
@@ -235,21 +245,30 @@ int jfsck(const char *name, struct jfsck_result *res)
 
 		rv = jtrans_commit(curts);
 
-		munmap(map, filelen);
-
 		if (rv < 0) {
 			res->apply_error++;
 			goto loop;
 		}
 		res->reapplied++;
 
-
 loop:
 		if (tfd >= 0) {
 			close(tfd);
 			tfd = -1;
 		}
-
+		if (map != NULL)
+			munmap(map, filelen);
+
+		if (curts->name)
+			free(curts->name);
+		while (curts->op != NULL) {
+			tmpop = curts->op->next;
+			if (curts->op->pdata)
+				free(curts->op->pdata);
+			free(curts->op);
+			curts->op = tmpop;
+		}
+		pthread_mutex_destroy(&(curts->lock));
 		free(curts);
 
 		res->total++;
@@ -264,6 +283,8 @@ exit:
 		close(fs.jdirfd);
 	if (dir != NULL)
 		closedir(dir);
+	if (fs.jmap != NULL)
+		munmap(fs.jmap, sizeof(unsigned int));
 
 	return ret;