git » libjio » commit ed3a770

libjio: Check for readdir() faults in jfsck() and jfsck_cleanup()

author Alberto Bertogli
2009-07-16 14:51:04 UTC
committer Alberto Bertogli
2009-07-16 15:39:06 UTC
parent 87a9c519df00c3ec700825ed0bdb3a8e6098898e

libjio: Check for readdir() faults in jfsck() and jfsck_cleanup()

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

libjio/check.c +14 -2

diff --git a/libjio/check.c b/libjio/check.c
index 2927147..4cd97b4 100644
--- a/libjio/check.c
+++ b/libjio/check.c
@@ -47,7 +47,8 @@ static int jfsck_cleanup(const char *name, const char *jdir)
 	else if (dir == NULL)
 		return -1;
 
-	for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
+	for (errno = 0, dent = readdir(dir); dent != NULL;
+			errno = 0, dent = readdir(dir)) {
 		/* we only care about transactions (named as numbers > 0) and
 		 * the lockfile (named "lock"); ignore everything else */
 		if (strcmp(dent->d_name, "lock") && atoi(dent->d_name) <= 0)
@@ -69,6 +70,12 @@ static int jfsck_cleanup(const char *name, const char *jdir)
 			return -1;
 		}
 	}
+
+	if (errno) {
+		closedir(dir);
+		return -1;
+	}
+
 	if (closedir(dir) != 0)
 		return -1;
 
@@ -191,7 +198,8 @@ enum jfsck_return jfsck(const char *name, const char *jdir,
 	/* find the greatest transaction number by looking into the journal
 	 * directory */
 	maxtid = 0;
-	for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
+	for (errno = 0, dent = readdir(dir); dent != NULL;
+			errno = 0, dent = readdir(dir)) {
 		/* see if the file is named like a transaction, ignore
 		 * otherwise; as transactions are named as numbers > 0, a
 		 * simple atoi() is enough testing */
@@ -201,6 +209,10 @@ enum jfsck_return jfsck(const char *name, const char *jdir,
 		if (rv > maxtid)
 			maxtid = rv;
 	}
+	if (errno) {
+		ret = J_EIO;
+		goto exit;
+	}
 
 	/* rewrite the lockfile, writing the new maxtid on it, so that when we
 	 * rollback a transaction it doesn't step over existing ones */