linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [BUG][BTRFS-PROGS] Fix Bug to corrupt the img file
@ 2012-10-24  2:04 Rock Lee
  2012-10-25 15:37 ` David Sterba
  0 siblings, 1 reply; 3+ messages in thread
From: Rock Lee @ 2012-10-24  2:04 UTC (permalink / raw)
  To: linux-btrfs

Fix Bug to corrupt the img file

Reproduce steps:
  > dd if=/dev/zero of=btrfs-small.img bs=1M count=1
  > ls -lh btrfs-small.img
  -rw-rw-r--. 1 rock rock 1.0M Oct 24 09:51 btrfs-small.img
  > mkfs.btrfs btrfs-small.img
  -rw-rw-r--. 1 rock rock 2.0M Oct 24 09:53 btrfs-small.img

Here you can see the original img file's size goes larger to 2Mbytes.

Signed-off-by: Rock Lee <zimilo@code-trick.com>
---
 utils.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/utils.c b/utils.c
index 205e667..3c88d2e 100644
--- a/utils.c
+++ b/utils.c
@@ -441,7 +441,7 @@ static int zero_blocks(int fd, off_t start, size_t len)
        return ret;
 }

-static int zero_dev_start(int fd)
+static int zero_dev_start(int fd, u64 dev_size)
 {
        off_t start = 0;
        size_t len = 2 * 1024 * 1024;
@@ -451,6 +451,10 @@ static int zero_dev_start(int fd)
        start = 1024;
        len -= 1024;
 #endif
+
+       if (dev_size < len)
+               return -EIO;
+
        return zero_blocks(fd, start, len);
 }

@@ -572,7 +576,7 @@ int btrfs_prepare_device(int fd, char *file, int
zero_end, u64 *block_count_ret,
                discard_blocks(fd, 0, block_count);
        }

-       ret = zero_dev_start(fd);
+       ret = zero_dev_start(fd, block_count);
        if (ret) {
                fprintf(stderr, "failed to zero device start %d\n", ret);
                exit(1);
-- 
1.7.7.6

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [BUG][BTRFS-PROGS] Fix Bug to corrupt the img file
  2012-10-24  2:04 [BUG][BTRFS-PROGS] Fix Bug to corrupt the img file Rock Lee
@ 2012-10-25 15:37 ` David Sterba
  2012-10-25 15:58   ` Rock Lee
  0 siblings, 1 reply; 3+ messages in thread
From: David Sterba @ 2012-10-25 15:37 UTC (permalink / raw)
  To: Rock Lee; +Cc: linux-btrfs

On Wed, Oct 24, 2012 at 10:04:47AM +0800, Rock Lee wrote:
> @@ -572,7 +576,7 @@ int btrfs_prepare_device(int fd, char *file, int
> zero_end, u64 *block_count_ret,
>                 discard_blocks(fd, 0, block_count);
>         }
> 
> -       ret = zero_dev_start(fd);
> +       ret = zero_dev_start(fd, block_count);
>         if (ret) {
>                 fprintf(stderr, "failed to zero device start %d\n", ret);
>                 exit(1);

Please add a separate check for a minimal image size instead, right after

 553         block_count = device_size(fd, &st);
 554         if (block_count == 0) {
 555                 fprintf(stderr, "unable to find %s size\n", file);
 556                 exit(1);
 557         }
 558         if (max_block_count)
 559                 block_count = min(block_count, max_block_count);

	     if (block_count < 2 MB ) {
		     fprintf(stderr, "Image too small (less than 2MB)");
		     exit
	     }

thanks,
david

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [BUG][BTRFS-PROGS] Fix Bug to corrupt the img file
  2012-10-25 15:37 ` David Sterba
@ 2012-10-25 15:58   ` Rock Lee
  0 siblings, 0 replies; 3+ messages in thread
From: Rock Lee @ 2012-10-25 15:58 UTC (permalink / raw)
  To: dave, Rock Lee, linux-btrfs

Got it.

2012/10/25 David Sterba <dave@jikos.cz>:
> On Wed, Oct 24, 2012 at 10:04:47AM +0800, Rock Lee wrote:
>> @@ -572,7 +576,7 @@ int btrfs_prepare_device(int fd, char *file, int
>> zero_end, u64 *block_count_ret,
>>                 discard_blocks(fd, 0, block_count);
>>         }
>>
>> -       ret = zero_dev_start(fd);
>> +       ret = zero_dev_start(fd, block_count);
>>         if (ret) {
>>                 fprintf(stderr, "failed to zero device start %d\n", ret);
>>                 exit(1);
>
> Please add a separate check for a minimal image size instead, right after
>
>  553         block_count = device_size(fd, &st);
>  554         if (block_count == 0) {
>  555                 fprintf(stderr, "unable to find %s size\n", file);
>  556                 exit(1);
>  557         }
>  558         if (max_block_count)
>  559                 block_count = min(block_count, max_block_count);
>
>              if (block_count < 2 MB ) {
>                      fprintf(stderr, "Image too small (less than 2MB)");
>                      exit
>              }
>
> thanks,
> david

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-10-25 15:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-24  2:04 [BUG][BTRFS-PROGS] Fix Bug to corrupt the img file Rock Lee
2012-10-25 15:37 ` David Sterba
2012-10-25 15:58   ` Rock Lee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).