btrfs-progs: fix a false alert on an uninitialized variable when BUG_ON() is involved

[FALSE ALERT]
Clang 15.0.7 gives the following false alert in get_dev_extent_len():

  kernel-shared/extent-tree.c:3328:2: warning: variable 'div' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]
          default:
          ^~~~~~~
  kernel-shared/extent-tree.c:3332:24: note: uninitialized use occurs here
          return map->ce.size / div;
                                ^~~
  kernel-shared/extent-tree.c:3311:9: note: initialize the variable 'div' to silence this warning
          int div;
                 ^
                  = 0

And one in btrfs_stripe_length() too:

  kernel-shared/volumes.c:2781:2: warning: variable 'stripe_len' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]
          default:
          ^~~~~~~
  kernel-shared/volumes.c:2785:9: note: uninitialized use occurs here
          return stripe_len;
                 ^~~~~~~~~~
  kernel-shared/volumes.c:2754:16: note: initialize the variable 'stripe_len' to silence this warning
          u64 stripe_len;
                        ^
                         = 0

[CAUSE]
Clang doesn't really understand what BUG_ON() means, thus in that
default case, we won't get uninitialized value but crash directly.

[FIX]
Silent the errors by assigning the default value properly using the
value of SINGLE profile.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Qu Wenruo 2023-01-27 13:41:15 +08:00 committed by David Sterba
parent e2806fd624
commit 66dad3a8f5
2 changed files with 4 additions and 3 deletions

View File

@ -3308,7 +3308,7 @@ out:
static u64 get_dev_extent_len(struct map_lookup *map)
{
int div;
int div = 1;
switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
case 0: /* Single */
@ -3316,7 +3316,7 @@ static u64 get_dev_extent_len(struct map_lookup *map)
case BTRFS_BLOCK_GROUP_RAID1:
case BTRFS_BLOCK_GROUP_RAID1C3:
case BTRFS_BLOCK_GROUP_RAID1C4:
div = 1;
/* The default value can already handle it. */
break;
case BTRFS_BLOCK_GROUP_RAID5:
case BTRFS_BLOCK_GROUP_RAID6:

View File

@ -2758,6 +2758,7 @@ u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,
BTRFS_BLOCK_GROUP_PROFILE_MASK;
chunk_len = btrfs_chunk_length(leaf, chunk);
stripe_len = chunk_len;
switch (profile) {
case 0: /* Single profile */
@ -2765,7 +2766,7 @@ u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,
case BTRFS_BLOCK_GROUP_RAID1C3:
case BTRFS_BLOCK_GROUP_RAID1C4:
case BTRFS_BLOCK_GROUP_DUP:
stripe_len = chunk_len;
/* The default value is already fine. */
break;
case BTRFS_BLOCK_GROUP_RAID0:
stripe_len = chunk_len / num_stripes;