From: batt Date: Wed, 24 Sep 2008 16:36:13 +0000 (+0000) Subject: Handle reading outside EOF. X-Git-Tag: 2.0.0~92 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=133c08ba914cdfe533693a1633fe4377b6e255f9;p=bertos.git Handle reading outside EOF. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1842 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/fs/battfs.c b/bertos/fs/battfs.c index 9bc8bdfd..11908590 100644 --- a/bertos/fs/battfs.c +++ b/bertos/fs/battfs.c @@ -665,7 +665,7 @@ static size_t battfs_read(struct KFile *fd, void *_buf, size_t size) pgaddr_t addr_offset; pgaddr_t read_len; - size = MIN((kfile_off_t)size, fd->size - fd->seek_pos); + size = MIN((kfile_off_t)size, MAX(fd->size - fd->seek_pos, 0)); while (size) { diff --git a/bertos/fs/battfs_test.c b/bertos/fs/battfs_test.c index 8ab33a48..c1cd89e6 100644 --- a/bertos/fs/battfs_test.c +++ b/bertos/fs/battfs_test.c @@ -748,6 +748,44 @@ static void increaseFile(BattFsSuper *disk) TRACEMSG("17: passed\n"); } +static void readEOF(BattFsSuper *disk) +{ + BattFs fd1; + uint8_t buf[16]; + + TRACEMSG("18: reading over EOF test\n"); + + fp = fopen(test_filename, "w+"); + + unsigned int PAGE_FILL = PAGE_SIZE - BATTFS_HEADER_LEN; + inode_t INODE = 0; + inode_t INODE2 = 4; + unsigned int MODE = 0; + + disk->erase(disk, 0); + battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0); + battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1); + battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1); + disk->erase(disk, 4); + battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0); + battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1); + battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1); + + fclose(fp); + + ASSERT(battfs_init(disk)); + ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE)); + ASSERT(kfile_seek(&fd1.fd, fd1.fd.size + 10, SEEK_SET) == fd1.fd.size + 10); + ASSERT(fd1.fd.seek_pos == fd1.fd.size + 10); + ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == 0); + + ASSERT(kfile_close(&fd1.fd) == 0); + ASSERT(battfs_close(disk)); + + TRACEMSG("18: passed\n"); +} + + int battfs_testRun(void) { BattFsSuper disk; @@ -776,6 +814,7 @@ int battfs_testRun(void) createFile(&disk); multipleWrite(&disk); increaseFile(&disk); + readEOF(&disk); kprintf("All tests passed!\n"); diff --git a/bertos/kern/kfile.c b/bertos/kern/kfile.c index cab20aa6..2d8a2f84 100644 --- a/bertos/kern/kfile.c +++ b/bertos/kern/kfile.c @@ -197,8 +197,7 @@ kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode wh /* Bound check */ if (seek_pos + offset > fd->size) { - ASSERT(0); - return EOF; + LOG_INFO("seek outside EOF\n"); } fd->seek_pos = seek_pos + offset;