btrfs: Turn an 'else if' into an 'else' in btrfs_uuid_tree_add
PATCH 1/1 btrfs: Turn an 'else if' into an 'else' in btrfs_uuid_tree_add
When building with -Wsometimes-uninitialized, Clang warns:
fs/btrfs/uuid-tree.c:129:13: warning: variable 'eb' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized] fs/btrfs/uuid-tree.c:129:13: warning: variable 'offset' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
Clang can't tell that all cases are covered with this final else if. Just turn it into an else so that it is clear.
--- fs/btrfs/uuid-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/uuid-tree.c +1 -1
@@ -126,7 +126,7 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type, slot = path->slots[0 ]; offset =btrfs_item_ptr_offset (eb, slot); offset +=btrfs_item_size_nr (eb, slot) -sizeof (subid_le); - }else if (ret <0 ) { + }else { btrfs_warn(fs_info, "insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!", ret, (unsigned long long)key.objectid,
Signature · 1 line
2.21.0
Quoted 6 lines · Nathan Chancellor
When building with -Wsometimes-uninitialized, Clang warns:
fs/btrfs/uuid-tree.c:129:13: warning: variable 'eb' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]fs/btrfs/uuid-tree.c:129:13: warning: variable 'offset' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]Clang can't tell that all cases are covered with this final else if.
The chain of conditions is
if (ret >= 0)
else if (ret == -EEXIST)
else if (ret < 0)
I'd think that compiler should be able to somehow represent the coverage of the ranges and report if it's not complete and leading to some uninitialized variables.
What if the last condition is 'ret < -EINVAL' obscuring the numerical value, it'd be easy to miss that there are 20 unhandled values. In such case the final 'else' would be the right thing to do.
Anyway, I'll apply the patch because it makes the code easier to follow. Thanks.
Quoted 7 lines · David Sterba
Quoted 5 lines · Nathan Chancellor
When building with -Wsometimes-uninitialized, Clang warns:
fs/btrfs/uuid-tree.c:129:13: warning: variable 'eb' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]fs/btrfs/uuid-tree.c:129:13: warning: variable 'offset' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
Quoted 7 lines
Quoted 1 line
Clang can't tell that all cases are covered with this final else if.
The chain of conditions is
if (ret >= 0)
else if (ret ==-EEXIST)
else if (ret < 0)
In the few cases we looked at, it seemed that the compiler's heuristic for coverage doesn't try very hard. I assume once you start having more complicated expressions is gets quite difficult to prove.
Thanks for the patch Nathan!
Signature · 2 lines
Thanks, ~Nick Desaulniers