git » libjio » commit 587e378

jread() and jpread() can return partial reads under some circumnstances; this is quite wrong, dangerous and seriously broken, and I shouldn't have let it happen.

author Alberto Bertogli
2004-05-05 23:15:33 UTC
committer Alberto Bertogli
2007-07-15 12:44:27 UTC
parent eb542d6f81ef3b262464c85256ec7e2b92558317

jread() and jpread() can return partial reads under some circumnstances; this is quite wrong, dangerous and seriously broken, and I shouldn't have let it happen.

jread() and jpread() can return partial reads under some circumnstances; this
is quite wrong, dangerous and seriously broken, and I shouldn't have let it
happen.

I guess I was quite distracted when I wrote those wrappers.

This patch make jread() and jpread() use spread() which never returns partial
reads, unless there was an error.

jreadv() is not fixed and can potentially have the same issue too; but I'll
leave the fix for another patch because it's much more complex and I don't
even know if it's worth it (people doing scatter/gather I/O should already be
taking care of this).

libjio.c +10 -2

diff --git a/libjio.c b/libjio.c
index 4cc25ae..cd51dae 100644
--- a/libjio.c
+++ b/libjio.c
@@ -507,10 +507,18 @@ ssize_t jread(struct jfs *fs, void *buf, size_t count)
 	off_t pos;
 
 	pthread_mutex_lock(&(fs->lock));
+
 	pos = lseek(fs->fd, 0, SEEK_CUR);
+
 	plockf(fs->fd, F_LOCK, pos, count);
-	rv = read(fs->fd, buf, count);
+	rv = spread(fs->fd, buf, count, pos);
 	plockf(fs->fd, F_ULOCK, pos, count);
+
+	if (rv == count) {
+		/* if success, advance the file pointer */
+		lseek(fs->fd, count, SEEK_CUR);
+	}
+
 	pthread_mutex_unlock(&(fs->lock));
 
 	return rv;
@@ -522,7 +530,7 @@ ssize_t jpread(struct jfs *fs, void *buf, size_t count, off_t offset)
 	int rv;
 
 	plockf(fs->fd, F_LOCK, offset, count);
-	rv = pread(fs->fd, buf, count, offset);
+	rv = spread(fs->fd, buf, count, offset);
 	plockf(fs->fd, F_ULOCK, offset, count);
 	
 	return rv;