

If not initialized properly, error paths will close fds where it shouldn't.
This can easily happen in a normal path, like jfsck() when the jdir doesn't
exist.

This patch initialize values properly so error paths work as expected.

Thanks to Pieter Grimmerink for the report and the fix.


---

 cur-root/check.c |   17 +++++++++--------
 cur-root/trans.c |    8 +++++++-
 2 files changed, 16 insertions(+), 9 deletions(-)

diff -puN check.c~initialize_fds check.c
--- cur/check.c~initialize_fds	2004-08-28 21:22:57.393622488 -0300
+++ cur-root/check.c	2004-08-28 21:22:57.397621880 -0300
@@ -95,7 +95,7 @@ error:
 /* check the journal and rollback incomplete transactions */
 int jfsck(const char *name, struct jfsck_result *res)
 {
-	int fd, tfd, rv, i, ret;
+	int tfd, rv, i, ret;
 	unsigned int maxtid;
 	uint32_t csum1, csum2;
 	char jdir[PATH_MAX], jlockfile[PATH_MAX], tname[PATH_MAX];
@@ -108,20 +108,22 @@ int jfsck(const char *name, struct jfsck
 	unsigned char *map;
 	off_t filelen;
 
-	fd = tfd = -1;
+	tfd = -1;
 	filelen = 0;
 	dir = NULL;
-	fs.jmap = NULL;
+	fs.fd = -1;
+	fs.jfd = -1;
+	fs.jdirfd = -1;
+	fs.jmap = MAP_FAILED;
 	map = NULL;
 	ret = 0;
 
-	fd = open(name, O_RDWR | O_SYNC | O_LARGEFILE);
-	if (fd < 0) {
+	fs.fd = open(name, O_RDWR | O_SYNC | O_LARGEFILE);
+	if (fs.fd < 0) {
 		ret = J_ENOENT;
 		goto exit;
 	}
 
-	fs.fd = fd;
 	fs.name = (char *) name;
 
 	if (!get_jdir(name, jdir)) {
@@ -154,7 +156,6 @@ int jfsck(const char *name, struct jfsck
 			PROT_READ | PROT_WRITE, MAP_SHARED, fs.jfd, 0);
 	if (fs.jmap == MAP_FAILED) {
 		ret = J_ENOJOURNAL;
-		fs.jmap = NULL;
 		goto exit;
 	}
 
@@ -283,7 +284,7 @@ exit:
 		close(fs.jdirfd);
 	if (dir != NULL)
 		closedir(dir);
-	if (fs.jmap != NULL)
+	if (fs.jmap != MAP_FAILED)
 		munmap(fs.jmap, sizeof(unsigned int));
 
 	return ret;
diff -puN trans.c~initialize_fds trans.c
--- cur/trans.c~initialize_fds	2004-08-28 21:22:57.394622336 -0300
+++ cur-root/trans.c	2004-08-28 21:22:57.398621728 -0300
@@ -506,6 +506,11 @@ int jopen(struct jfs *fs, const char *na
 	char jdir[PATH_MAX], jlockfile[PATH_MAX];
 	struct stat sinfo;
 
+	fs->fd = -1;
+	fs->jfd = -1;
+	fs->jdirfd = -1;
+	fs->jmap = MAP_FAILED;
+
 	/* we always need read and write access, because when we commit a
 	 * transaction we read the current contents before applying, and write
 	 * access is needed for locking with fcntl */
@@ -627,7 +632,8 @@ int jclose(struct jfs *fs)
 	if (fs->name)
 		/* allocated by strdup() in jopen() */
 		free(fs->name);
-	munmap(fs->jmap, sizeof(unsigned int));
+	if (fs->jmap != MAP_FAILED)
+		munmap(fs->jmap, sizeof(unsigned int));
 
 	pthread_mutex_unlock(&(fs->lock));
 	pthread_mutex_destroy(&(fs->lock));
_
