Skip to contentSkip to conversation

btrfs: reflink: Assure length != 0 in btrfs_extent_same()

PATCH In review Sidong Yang 3 participants 7 messages Aug 18, 2021 – Aug 20, 2021 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: reflink: Assure length != 0 in btrfs_extent_same()

btrfs_extent_same() cannot be called with zero length. Because when length is zero, it would be filtered by condition in btrfs_remap_file_range(). But if this function is used in other case in future, it can make ret as uninitialized.

---
 fs/btrfs/reflink.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/reflink.c +1 -0
index 9b0814318e72..69eb50f2f0b4 100644
@@ -653,6 +653,7 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
 	u64 i, tail_len, chunk_count;
 	struct btrfs_root *root_dst = BTRFS_I(dst)->root;
 
+	ASSERT(olen);
 	spin_lock(&root_dst->root_item_lock);
 	if (root_dst->send_in_progress) {
 		btrfs_warn_rl(root_dst->fs_info,
Signature · 1 line
2.25.1
#1 lore raw
Quoted 4 lines · Sidong Yang

btrfs_extent_same() cannot be called with zero length. Because when length is zero, it would be filtered by condition in btrfs_remap_file_range(). But if this function is used in other case in future, it can make ret as uninitialized.

Do you have a specific future in mind? Adding the assert won't hurt so ok.

#2 lore raw
Quoted 8 lines · David Sterba
Quoted 4 lines · Sidong Yang

btrfs_extent_same() cannot be called with zero length. Because when length is zero, it would be filtered by condition in btrfs_remap_file_range(). But if this function is used in other case in future, it can make ret as uninitialized.

Do you have a specific future in mind? Adding the assert won't hurt so ok.

No, sorry, I just want to make it safe. is there any way to better than adding assert? Would it better to initialize ret?

#3 lore raw
In reply to Sidong Yang
Quoted 6 lines · Sidong Yang

btrfs_extent_same() cannot be called with zero length. Because when length is zero, it would be filtered by condition in btrfs_remap_file_range(). But if this function is used in other case in future, it can make ret as uninitialized.

This is not sufficient, with the assert compiled out the error would still be in place. It seem that it is sufficient to initialize ret to some non-arbitrary value i.e -EINVAL ?

Quoted 17 lines
---
 fs/btrfs/reflink.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/reflink.c +1 -0
index 9b0814318e72..69eb50f2f0b4 100644
@@ -653,6 +653,7 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
 	u64 i, tail_len, chunk_count;
 	struct btrfs_root *root_dst = BTRFS_I(dst)->root;
 
+	ASSERT(olen);
 	spin_lock(&root_dst->root_item_lock);
 	if (root_dst->send_in_progress) {
 		btrfs_warn_rl(root_dst->fs_info,
#4 lore raw
Quoted 8 lines · Nikolay Borisov
Quoted 5 lines · Sidong Yang

btrfs_extent_same() cannot be called with zero length. Because when length is zero, it would be filtered by condition in btrfs_remap_file_range(). But if this function is used in other case in future, it can make ret as uninitialized.

Quoted 5 lines
Quoted 1 line

This is not sufficient, with the assert compiled out the error would still be in place. It seem that it is sufficient to initialize ret to some non-arbitrary value i.e -EINVAL ?

I agree. It's better way to assign intial value than adding assert. If there is code that initialize ret, It seems that assert is no need for this.

Quoted 18 lines
Quoted 18 lines
---
 fs/btrfs/reflink.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/reflink.c +1 -0
index 9b0814318e72..69eb50f2f0b4 100644
@@ -653,6 +653,7 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
 	u64 i, tail_len, chunk_count;
 	struct btrfs_root *root_dst = BTRFS_I(dst)->root;
 
+	ASSERT(olen);
 	spin_lock(&root_dst->root_item_lock);
 	if (root_dst->send_in_progress) {
 		btrfs_warn_rl(root_dst->fs_info,
#5 lore raw
Quoted 11 lines · Sidong Yang
Quoted 10 lines · Nikolay Borisov
Quoted 6 lines · Sidong Yang

btrfs_extent_same() cannot be called with zero length. Because when length is zero, it would be filtered by condition in btrfs_remap_file_range(). But if this function is used in other case in future, it can make ret as uninitialized.

Quoted 7 lines
Quoted 3 lines

This is not sufficient, with the assert compiled out the error would still be in place. It seem that it is sufficient to initialize ret to some non-arbitrary value i.e -EINVAL ?

I agree. It's better way to assign intial value than adding assert. If there is code that initialize ret, It seems that assert is no need for this.

Patch with assert removed, please send the one initializing the return value, thanks.

#6 lore raw
Quoted 16 lines · David Sterba
Quoted 15 lines · Sidong Yang
Quoted 13 lines · Nikolay Borisov
Quoted 6 lines · Sidong Yang

btrfs_extent_same() cannot be called with zero length. Because when length is zero, it would be filtered by condition in btrfs_remap_file_range(). But if this function is used in other case in future, it can make ret as uninitialized.

This is not sufficient, with the assert compiled out the error would still be in place. It seem that it is sufficient to initialize ret to some non-arbitrary value i.e -EINVAL ?

Quoted 6 lines
Quoted 3 lines

I agree. It's better way to assign intial value than adding assert. If there is code that initialize ret, It seems that assert is no need for this.

Patch with assert removed, please send the one initializing the return value, thanks.

Sure, I'll write it in v2.

Thanks,
Sidong