[Ocfs2-test-devel] [PATCH 2/8] Ocfs2-test: Protect read_at() from running into endless loop.
Tristan Ye
tristan.ye at oracle.com
Fri Apr 15 02:01:46 PDT 2011
An attempt to read data from the EOF of file gets stuck in read_at()
currently, since it never gets the enough count after the reading
cross EOF.
Signed-off-by: Tristan Ye <tristan.ye at oracle.com>
---
programs/libocfs2test/file_verify.c | 23 ++++++++++++++++++++---
1 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/programs/libocfs2test/file_verify.c b/programs/libocfs2test/file_verify.c
index 0ad4d38..e940be1 100644
--- a/programs/libocfs2test/file_verify.c
+++ b/programs/libocfs2test/file_verify.c
@@ -147,11 +147,18 @@ int get_i_size(char *filename, unsigned long *size, int flags)
return ret;
}
-static int read_at(int fd, void *buf, size_t count, off_t offset)
+static int read_at(int fd, void *buf, size_t count, off_t offset, size_t i_size)
{
int ret;
size_t bytes_read;
+ if (((offset + count) > i_size) && (i_size >= offset)) {
+ count = i_size - offset;
+ }
+
+ if (!count)
+ return count;
+
ret = pread(fd, buf, count, offset);
if (ret < 0) {
ret = errno;
@@ -346,12 +353,22 @@ int do_read_chunk(int fd, unsigned long chunk_no, unsigned int chunksize,
{
int ret;
char *tmp_pattern;
- size_t count = chunksize;
+ size_t count = chunksize, i_size;
off_t offset = chunksize * chunk_no;
+ struct stat stat;
tmp_pattern = (char *)malloc(chunksize);
- ret = read_at(fd, tmp_pattern, count, offset);
+ ret = fstat(fd, &stat);
+ if (ret == -1) {
+ ret = errno;
+ fprintf(stderr, "stat failure %d: %s\n", ret, strerror(ret));
+ return ret;
+ }
+
+ i_size = stat.st_size;
+
+ ret = read_at(fd, tmp_pattern, count, offset, i_size);
if (ret < 0)
goto out;
--
1.6.5.2
More information about the Ocfs2-test-devel
mailing list