git » libjio » commit f798230

Fix some datatypes to avoid overflows and losses.

author Alberto Bertogli
2005-03-10 06:42:49 UTC
committer Alberto Bertogli
2007-07-15 13:49:02 UTC
parent afede6e6bf89315fd70ffad9adaa337177f0b21e

Fix some datatypes to avoid overflows and losses.

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

ansi.c +4 -3
check.c +5 -3
jiofsck.c +1 -1
libjio.h +1 -1
trans.c +5 -3

diff --git a/ansi.c b/ansi.c
index 8ded686..97a9f44 100644
--- a/ansi.c
+++ b/ansi.c
@@ -175,7 +175,7 @@ int jferror(struct jfs *stream)
 /* fseek wrapper */
 int jfseek(struct jfs *stream, long offset, int whence)
 {
-	long pos;
+	off_t pos;
 
 	pthread_mutex_lock(&(stream->lock));
 	pos = lseek(stream->fd, offset, whence);
@@ -189,9 +189,10 @@ int jfseek(struct jfs *stream, long offset, int whence)
 }
 
 /* ftell wrapper */
-int jftell(struct jfs *stream)
+long jftell(struct jfs *stream)
 {
-	return lseek(stream->fd, 0, SEEK_CUR);
+	/* forced conversion to long to meet the prototype */
+	return (long) lseek(stream->fd, 0, SEEK_CUR);
 }
 
 /* rewind wrapper */
diff --git a/check.c b/check.c
index 95dca6a..2257d78 100644
--- a/check.c
+++ b/check.c
@@ -106,7 +106,7 @@ int jfsck(const char *name, const char *jdir, struct jfsck_result *res)
 	DIR *dir;
 	struct dirent *dent;
 	unsigned char *map;
-	off_t filelen;
+	off_t filelen, lr;
 
 	tfd = -1;
 	filelen = 0;
@@ -239,13 +239,15 @@ int jfsck(const char *name, const char *jdir, struct jfsck_result *res)
 
 		/* try to lock the transaction file, if it's locked then it is
 		 * currently being used so we skip it */
-		rv = plockf(tfd, F_TLOCKW, 0, 0);
-		if (rv == -1) {
+		lr = plockf(tfd, F_TLOCKW, 0, 0);
+		if (lr == -1) {
 			res->in_progress++;
 			goto loop;
 		}
 
 		filelen = lseek(tfd, 0, SEEK_END);
+		/* no overflow problems because we know the transaction size
+		 * is limited to SSIZE_MAX */
 		map = mmap(0, filelen, PROT_READ, MAP_SHARED, tfd, 0);
 		if (map == MAP_FAILED) {
 			res->broken++;
diff --git a/jiofsck.c b/jiofsck.c
index d2cf7e4..46331fa 100644
--- a/jiofsck.c
+++ b/jiofsck.c
@@ -9,7 +9,7 @@
 #include "libjio.h"
 
 
-void usage()
+static void usage()
 {
 	printf("\
 Use: jiofsck [clean=1] [dir=DIR] FILE\n\
diff --git a/libjio.h b/libjio.h
index 0ff1ba6..ce7651e 100644
--- a/libjio.h
+++ b/libjio.h
@@ -141,7 +141,7 @@ int jfeof(struct jfs *stream);
 void jclearerr(struct jfs *stream);
 int jferror(struct jfs *stream);
 int jfseek(struct jfs *stream, long offset, int whence);
-int jftell(struct jfs *stream);
+long jftell(struct jfs *stream);
 void jrewind(struct jfs *stream);
 FILE *jfsopen(struct jfs *stream, const char *mode);
 
diff --git a/trans.c b/trans.c
index a09ff85..dcf68e3 100644
--- a/trans.c
+++ b/trans.c
@@ -278,9 +278,10 @@ ssize_t jtrans_commit(struct jtrans *ts)
 	 * same spots and we could end up with interleaved writes, that could
 	 * break atomicity warantees if we need to rollback */
 	if (!(ts->flags & J_NOLOCK)) {
+		off_t lr;
 		for (op = ts->op; op != NULL; op = op->next) {
-			rv = plockf(ts->fs->fd, F_LOCKW, op->offset, op->len);
-			if (rv == -1)
+			lr = plockf(ts->fs->fd, F_LOCKW, op->offset, op->len);
+			if (lr == -1)
 				/* note it can fail with EDEADLK */
 				goto unlink_exit;
 			op->locked = 1;
@@ -345,7 +346,8 @@ ssize_t jtrans_commit(struct jtrans *ts)
 		curpos += op->len;
 	}
 
-	/* compute and save the checksum */
+	/* compute and save the checksum (curpos is always small, so there's
+	 * no overflow possibility when we convert to size_t) */
 	if (!checksum(fd, curpos, &csum))
 		goto unlink_exit;