diff mbox series

btrfs-progs: convert: Make ASSERT not truncate cctx.total_bytes value

Message ID 20200826180820.31695-1-marcos@mpdesouza.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: convert: Make ASSERT not truncate cctx.total_bytes value | expand

Commit Message

Marcos Paulo de Souza Aug. 26, 2020, 6:08 p.m. UTC
From: Marcos Paulo de Souza <mpdesouza@suse.com>

Commit 670a19828ad ("btrfs-progs: convert: prevent 32bit overflow for
cctx->total_bytes") added an assert to ensure that cctxx.total_bytes did
not overflow, but this ASSERT calls assert_trace, which expects a long
value.

By converting the u64 to long overflows in a 32bit machine, leading the
assert_trace to be triggered since cctx.total_bytes turns to zero.

Fix this problem by comparing the cctx.total_bytes with zero when
calling ASSERT.

Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
---
 convert/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Qu Wenruo Aug. 26, 2020, 10:55 p.m. UTC | #1
On 2020/8/27 上午2:08, Marcos Paulo de Souza wrote:
> From: Marcos Paulo de Souza <mpdesouza@suse.com>
> 
> Commit 670a19828ad ("btrfs-progs: convert: prevent 32bit overflow for
> cctx->total_bytes") added an assert to ensure that cctxx.total_bytes did
> not overflow, but this ASSERT calls assert_trace, which expects a long
> value.

Oh, I forgot the type converting problem!

> 
> By converting the u64 to long overflows in a 32bit machine, leading the
> assert_trace to be triggered since cctx.total_bytes turns to zero.
> 
> Fix this problem by comparing the cctx.total_bytes with zero when
> calling ASSERT.
> 
> Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>

Awesome fix.

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu

> ---
>  convert/main.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/convert/main.c b/convert/main.c
> index 5f8f64c5..378fd61a 100644
> --- a/convert/main.c
> +++ b/convert/main.c
> @@ -1158,7 +1158,7 @@ static int do_convert(const char *devname, u32 convert_flags, u32 nodesize,
>  	if (ret)
>  		goto fail;
>  
> -	ASSERT(cctx.total_bytes);
> +	ASSERT(cctx.total_bytes != 0);
>  	blocksize = cctx.blocksize;
>  	total_bytes = (u64)blocksize * (u64)cctx.block_count;
>  	if (blocksize < 4096) {
>
David Sterba Aug. 31, 2020, 3:01 p.m. UTC | #2
On Wed, Aug 26, 2020 at 03:08:20PM -0300, Marcos Paulo de Souza wrote:
> From: Marcos Paulo de Souza <mpdesouza@suse.com>
> 
> Commit 670a19828ad ("btrfs-progs: convert: prevent 32bit overflow for
> cctx->total_bytes") added an assert to ensure that cctxx.total_bytes did
> not overflow, but this ASSERT calls assert_trace, which expects a long
> value.
> 
> By converting the u64 to long overflows in a 32bit machine, leading the
> assert_trace to be triggered since cctx.total_bytes turns to zero.
> 
> Fix this problem by comparing the cctx.total_bytes with zero when
> calling ASSERT.
> 
> Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>

Added to devel, thanks.
diff mbox series

Patch

diff --git a/convert/main.c b/convert/main.c
index 5f8f64c5..378fd61a 100644
--- a/convert/main.c
+++ b/convert/main.c
@@ -1158,7 +1158,7 @@  static int do_convert(const char *devname, u32 convert_flags, u32 nodesize,
 	if (ret)
 		goto fail;
 
-	ASSERT(cctx.total_bytes);
+	ASSERT(cctx.total_bytes != 0);
 	blocksize = cctx.blocksize;
 	total_bytes = (u64)blocksize * (u64)cctx.block_count;
 	if (blocksize < 4096) {