Skip to contentSkip to conversation

fs/btrfs: Fix uninitialized variable

PATCH-next Khaled ROMDHANI 3 participants 4 messages Apr 13, 2021 – Apr 17, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

The variable zone is not initialized. It
may causes a failed assertion.

Addresses-Coverity: ("Uninitialized variables")

---
 fs/btrfs/zoned.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/zoned.c +1 -1
index eeb3ebe11d7a..ee15ab8dccb5 100644
@@ -136,7 +136,7 @@ static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
  */
 static inline u32 sb_zone_number(int shift, int mirror)
 {
-	u64 zone;
+	u64 zone = 0;
 
 	ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
 	switch (mirror) {
Signature · 1 line
2.17.1
Reviewed-by
#1 lore raw
Quoted 6 lines · Khaled ROMDHANI

The variable zone is not initialized. It
may causes a failed assertion.

Addresses-Coverity: ("Uninitialized variables")

Quoted 9 lines
---
 fs/btrfs/zoned.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/zoned.c +0 -0
index eeb3ebe11d7a..ee15ab8dccb5 100644

fs/btrfs/zoned.c static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones, · line 141

Quoted 7 lines
fs/btrfs/zoned.c +1 -1
@@ -136,7 +136,7 @@ static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
  */
 static inline u32 sb_zone_number(int shift, int mirror)
 {
-	u64 zone;
+	u64 zone = 0;
 
 	ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);

Thanks for the fix.

I assume this was dug up by coverity static analysis rather than hitting it in a live system?

Since there is already an assert for the pre-condition 'mirror < max', I feel like it would make sense to also add one for mirror > 0.

Quoted 4 lines
fs/btrfs/zoned.c +0 -0
 	switch (mirror) {
Signature · 1 line
2.17.1
#2 lore raw
In reply to Khaled ROMDHANI
Quoted 2 lines · Khaled ROMDHANI

The variable zone is not initialized. It
may causes a failed assertion.

Failed assertion means the 2nd one checking that the result still fits to 32bit type. That would mean that none of the cases were hit, but all callers pass valid values.

It would be better to add a default: case to catch that explicitly, though hitting that is considered 'will not happen'.

#3 lore raw
Quoted 10 lines · David Sterba
Quoted 2 lines · Khaled ROMDHANI

The variable zone is not initialized. It
may causes a failed assertion.

Failed assertion means the 2nd one checking that the result still fits to 32bit type. That would mean that none of the cases were hit, but all callers pass valid values.

It would be better to add a default: case to catch that explicitly, though hitting that is considered 'will not happen'.

Yes. I will send a V2.