git » libjio » commit edfb2c0

libjio: Fix some signed/unsigned comparisons

author Alberto Bertogli
2009-07-18 21:36:54 UTC
committer Alberto Bertogli
2009-07-18 21:36:54 UTC
parent d7ec1c640dd73442e7f842515e08f2576072da9e

libjio: Fix some signed/unsigned comparisons

After building using -Wextra (which enables signed/unsigned comparison
warnings), these were the ones that made sense fixing.

All the others were valid cases, like:

	int rv;
	unsigned int blah;

	...
	rv = read(...);
	if (rv < 0)
		return ERROR;

	if (rv == blah)
		...

Where in the second comparison we know it's safe because rv is >= 0.
We could add a cast, but it would just make the code uglier for no real
reason.

The comparisons fixed by this commit are valid warnings, and can usually
be hit by committing very large transactions.

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

libjio/common.c +16 -15
libjio/journal.c +2 -1
libjio/unix.c +2 -1

diff --git a/libjio/common.c b/libjio/common.c
index 02f5f4a..30b7e93 100644
--- a/libjio/common.c
+++ b/libjio/common.c
@@ -52,17 +52,15 @@ off_t plockf(int fd, int cmd, off_t offset, off_t len)
  * less than count it's because EOF was reached */
 ssize_t spread(int fd, void *buf, size_t count, off_t offset)
 {
-	ssize_t rv, c;
+	ssize_t rv;
+	size_t c;
 
 	c = 0;
 
 	while (c < count) {
 		rv = pread(fd, (char *) buf + c, count - c, offset + c);
 
-		if (rv == count)
-			/* we're done */
-			return count;
-		else if (rv < 0)
+		if (rv < 0)
 			/* error */
 			return rv;
 		else if (rv == 0)
@@ -79,16 +77,15 @@ ssize_t spread(int fd, void *buf, size_t count, off_t offset)
 /** Like spread() but for pwrite() */
 ssize_t spwrite(int fd, const void *buf, size_t count, off_t offset)
 {
-	ssize_t rv, c;
+	ssize_t rv;
+	size_t c;
 
 	c = 0;
 
 	while (c < count) {
 		rv = pwrite(fd, (char *) buf + c, count - c, offset + c);
 
-		if (rv == count)
-			return count;
-		else if (rv < 0)
+		if (rv < 0)
 			return rv;
 
 		/* incomplete write, keep on writing */
@@ -104,8 +101,8 @@ ssize_t spwrite(int fd, const void *buf, size_t count, off_t offset)
 ssize_t swritev(int fd, struct iovec *iov, int iovcnt)
 {
 	int i;
-	ssize_t rv, c, t;
-	size_t total;
+	ssize_t rv;
+	size_t c, t, total;
 
 	total = 0;
 	for (i = 0; i < iovcnt; i++)
@@ -115,13 +112,17 @@ ssize_t swritev(int fd, struct iovec *iov, int iovcnt)
 	while (c < total) {
 		rv = writev(fd, iov, iovcnt);
 
-		if (rv == total)
-			return total;
-		else if (rv < 0)
+		if (rv < 0)
 			return rv;
 
-		/* incomplete write, advance iov and try again */
 		c += rv;
+
+		/* avoid going into the complex calculations for the common
+		 * case of writev() doing a complete write */
+		if (c == total)
+			break;
+
+		/* incomplete write, advance iov and try again */
 		t = 0;
 		for (i = 0; i < iovcnt; i++) {
 			if (t + iov[i].iov_len > rv) {
diff --git a/libjio/journal.c b/libjio/journal.c
index 80c6324..fcde61d 100644
--- a/libjio/journal.c
+++ b/libjio/journal.c
@@ -419,7 +419,8 @@ exit:
  */
 int fill_trans(unsigned char *map, off_t len, struct jtrans *ts)
 {
-	int rv, numops;
+	int rv;
+	unsigned int numops;
 	unsigned char *p;
 	struct operation *op, *tmp;
 	struct on_disk_hdr hdr;
diff --git a/libjio/unix.c b/libjio/unix.c
index f1af948..33aeadf 100644
--- a/libjio/unix.c
+++ b/libjio/unix.c
@@ -143,7 +143,8 @@ exit:
 ssize_t jwritev(struct jfs *fs, const struct iovec *vector, int count)
 {
 	int i;
-	size_t sum, rv;
+	size_t sum;
+	ssize_t rv;
 	off_t ipos, t;
 	struct jtrans *ts;