From 0d5ef48b4d2e9e07dd0c3306671a468ed36ebf48 Mon Sep 17 00:00:00 2001 From: d0k3 Date: Sun, 10 Apr 2016 16:05:09 +0200 Subject: [PATCH] Prevent f_lseek() beyond file size when reading images --- source/fatfs/image.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/fatfs/image.c b/source/fatfs/image.c index 15e85fe..fb0b06c 100644 --- a/source/fatfs/image.c +++ b/source/fatfs/image.c @@ -7,9 +7,12 @@ u32 mount_state = 0; int ReadImageSectors(u8* buffer, u32 sector, u32 count) { UINT bytes_read; UINT ret; + if (!count) return -1; if (!mount_state) return FR_INVALID_OBJECT; - if (f_tell(&mount_file) != sector * 0x200) + if (f_tell(&mount_file) != sector * 0x200) { + if (f_size(&mount_file) < sector * 0x200) return -1; f_lseek(&mount_file, sector * 0x200); + } ret = f_read(&mount_file, buffer, count * 0x200, &bytes_read); return (ret != 0) ? ret : (bytes_read != count * 0x200) ? -1 : 0; } @@ -17,6 +20,7 @@ int ReadImageSectors(u8* buffer, u32 sector, u32 count) { int WriteImageSectors(const u8* buffer, u32 sector, u32 count) { UINT bytes_written; UINT ret; + if (!count) return -1; if (!mount_state) return FR_INVALID_OBJECT; if (f_tell(&mount_file) != sector * 0x200) f_lseek(&mount_file, sector * 0x200);