Skip to contentSkip to conversation

btrfs: reflink: Initialize return value in btrfs_extent_same()

PATCH Superseded Sidong Yang 4 participants 7 messages Aug 20, 2021 – Aug 26, 2021 Btrfs file system

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 btrfs: reflink: Initialize return value in btrfs_extent_same()

btrfs_extent_same() cannot be called with zero length. This patch add code that initialize ret as -EINVAL to make it safe.

---
v2:
- Removed assert and added initializing ret

---
 fs/btrfs/reflink.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/reflink.c +1 -0
index 9b0814318e72..864f42198c5c 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;
 
+	ret = -EINVAL;
 	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
Reviewed-by on v2 1/1
#1 lore raw
Quoted 4 lines · Sidong Yang

btrfs_extent_same() cannot be called with zero length. This patch add code that initialize ret as -EINVAL to make it safe.

One minor nit: In this case the variable could have been initialized during definition to save 1 extra line of code.

Quoted 20 lines

---
v2:
- Removed assert and added initializing ret

---
 fs/btrfs/reflink.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/reflink.c +1 -0
index 9b0814318e72..864f42198c5c 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;
 
+	ret = -EINVAL;
 	spin_lock(&root_dst->root_item_lock);
 	if (root_dst->send_in_progress) {
 		btrfs_warn_rl(root_dst->fs_info,
#2 lore raw
Quoted 6 lines · Nikolay Borisov
Quoted 3 lines · Sidong Yang

btrfs_extent_same() cannot be called with zero length. This patch add code that initialize ret as -EINVAL to make it safe.

Quoted 6 lines
Quoted 1 line

One minor nit: In this case the variable could have been initialized during definition to save 1 extra line of code.

Yeah the function is short enough to have the declaration initialization in sight, I've changed that.

#3 lore raw
Quoted 9 lines · David Sterba
Quoted 8 lines · Nikolay Borisov
Quoted 4 lines · Sidong Yang

btrfs_extent_same() cannot be called with zero length. This patch add code that initialize ret as -EINVAL to make it safe.

Quoted 7 lines
Quoted 4 lines

One minor nit: In this case the variable could have been initialized during definition to save 1 extra line of code.

Yeah the function is short enough to have the declaration initialization in sight, I've changed that.

Thanks, I'll keep in mind. Any nit would be welcome.

Thanks,
Sidong

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

btrfs_extent_same() cannot be called with zero length. This patch add code that initialize ret as -EINVAL to make it safe.

I suppose the motivation of the patch is to fix a warning from smatch, or other similar tools, about 'ret' not being initialized when olen is 0.
Initializing 'ret' to some value surely makes the warning go away, even though it's not possible for olen to be 0 at btrfs_extent_same(), as that
is filtered up in the call chain.

However setting to -EINVAL by default is confusing and counter intuitive because dedupe operations are supposed to return 0 (success) for a 0 length range.
So 'ret' should be initialized to 0 to avoid any confusion.

Thanks.

Quoted 24 lines

---
v2:
- Removed assert and added initializing ret

---
 fs/btrfs/reflink.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/reflink.c +1 -1
index 9b0814318e72..864f42198c5c 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;
 
+       ret = -EINVAL;
        spin_lock(&root_dst->root_item_lock);
        if (root_dst->send_in_progress) {
                btrfs_warn_rl(root_dst->fs_info,
--

2.25.1

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”
#5 lore raw
Quoted 8 lines · Filipe Manana
Quoted 3 lines · Sidong Yang

btrfs_extent_same() cannot be called with zero length. This patch add code that initialize ret as -EINVAL to make it safe.

I suppose the motivation of the patch is to fix a warning from smatch, or other similar tools, about 'ret' not being initialized when olen is 0.

Yes, Actually I used smatch you said.

Quoted 8 lines

Initializing 'ret' to some value surely makes the warning go away, even though it's not possible for olen to be 0 at btrfs_extent_same(), as that
is filtered up in the call chain.

However setting to -EINVAL by default is confusing and counter intuitive because dedupe operations are supposed to return 0 (success) for a 0 length range.

Yeah, I think it depends on btrfs_extent_same()'s concept. It does nothing when 0 length. It's okay if we consider it's normal operation and it seems natural.

Quoted 2 lines

So 'ret' should be initialized to 0 to avoid any confusion.

Agree. I want to know other people's thoughts.

Thanks,
Sidong

Quoted 32 lines

Thanks.

Quoted 24 lines

---
v2:
- Removed assert and added initializing ret

---
 fs/btrfs/reflink.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/reflink.c +1 -1
index 9b0814318e72..864f42198c5c 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;
 
+       ret = -EINVAL;
        spin_lock(&root_dst->root_item_lock);
        if (root_dst->send_in_progress) {
                btrfs_warn_rl(root_dst->fs_info,
--

2.25.1

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”
#6 lore raw
Quoted 11 lines · Sidong Yang
Quoted 8 lines · Filipe Manana
Quoted 3 lines · Sidong Yang

btrfs_extent_same() cannot be called with zero length. This patch add code that initialize ret as -EINVAL to make it safe.

I suppose the motivation of the patch is to fix a warning from smatch, or other similar tools, about 'ret' not being initialized when olen is 0.

Yes, Actually I used smatch you said.

It's good to mention that it's a warning reported by some tool, not all such warnings are valid or have to be fixed.

Quoted 9 lines
Quoted 8 lines

Initializing 'ret' to some value surely makes the warning go away, even though it's not possible for olen to be 0 at btrfs_extent_same(), as that
is filtered up in the call chain.

However setting to -EINVAL by default is confusing and counter intuitive because dedupe operations are supposed to return 0 (success) for a 0 length range.

Quoted 8 lines

Yeah, I think it depends on btrfs_extent_same()'s concept. It does nothing when 0 length. It's okay if we consider it's normal operation and it seems natural.

Quoted 2 lines

So 'ret' should be initialized to 0 to avoid any confusion.

Agree. I want to know other people's thoughts.

I agree with Filipe, please resend the patch with ret = 0 and explanation why want it. Thanks.