When using btrfs check with -s option, if using '-s 2' on a small
device which doesn't have the third superblock, "No valid Btrfs found"
will be output, but it is not appropriate.

So check sb_bytenr against device size before scanning a device and
output proper error message.

Signed-off-by: Qu Wenruo <quwen...@cn.fujitsu.com>
---
 disk-io.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/disk-io.c b/disk-io.c
index 77fc610..d2599b5 100644
--- a/disk-io.c
+++ b/disk-io.c
@@ -1000,10 +1000,20 @@ int btrfs_scan_fs_devices(int fd, const char *path,
                          u64 sb_bytenr, int super_recover)
 {
        u64 total_devs;
+       u64 dev_size;
        int ret;
        if (!sb_bytenr)
                sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
 
+       dev_size = lseek(fd, 0, SEEK_END);
+       if (dev_size < 0)
+               return (int)(dev_size);
+       lseek(fd, 0, SEEK_SET);
+       if (sb_bytenr > dev_size) {
+               fprintf(stderr, "Superblock bytenr is larger than device 
size\n");
+               return -EINVAL;
+       }
+
        ret = btrfs_scan_one_device(fd, path, fs_devices,
                                    &total_devs, sb_bytenr, super_recover);
        if (ret) {
-- 
2.1.3

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to