Skip to contentSkip to conversation

fs/btrfs: Fix uninitialized variable

PATCH Superseded Khaled ROMDHANI 3 participants 5 messages Apr 17, 2021 – Apr 20, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fs/btrfs: Fix uninitialized variable

As reported by the Coverity static analysis.
The variable zone is not initialized which
may causes a failed assertion.

Addresses-Coverity: ("Uninitialized variables")

---
v2: add a default case as proposed by David Sterba

---
 fs/btrfs/zoned.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/zoned.c +3 -0
index eeb3ebe11d7a..82527308d165 100644
@@ -143,6 +143,9 @@ static inline u32 sb_zone_number(int shift, int mirror)
 	case 0: zone = 0; break;
 	case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
 	case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
+	default:
+		zone = 0;
+	break;
 	}
 
 	ASSERT(zone <= U32_MAX);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 17 lines · Khaled ROMDHANI

As reported by the Coverity static analysis.
The variable zone is not initialized which
may causes a failed assertion.

Addresses-Coverity: ("Uninitialized variables")

---
v2: add a default case as proposed by David Sterba

---
 fs/btrfs/zoned.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/zoned.c +0 -0
index eeb3ebe11d7a..82527308d165 100644

fs/btrfs/zoned.c static inline u32 sb_zone_number(int shift, int mirror) · line 147

Quoted 5 lines
fs/btrfs/zoned.c +2 -0
@@ -143,6 +143,9 @@ static inline u32 sb_zone_number(int shift, int mirror)
 	case 0: zone = 0; break;
 	case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
 	case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
+	default:
+		zone = 0;

Well yeah but this is not a valid case at all, we'd rather catch that as an assertion failure than letting is silently continue.

#2 lore raw
Quoted 13 lines · David Sterba
Quoted 12 lines · Khaled ROMDHANI

As reported by the Coverity static analysis.
The variable zone is not initialized which
may causes a failed assertion.

Addresses-Coverity: ("Uninitialized variables")

---
v2: add a default case as proposed by David Sterba

---
 fs/btrfs/zoned.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 13 lines

fs/btrfs/zoned.c static inline u32 sb_zone_number(int shift, int mirror) · line 147

Quoted 10 lines
fs/btrfs/zoned.c +2 -0
index eeb3ebe11d7a..82527308d165 100644
@@ -143,6 +143,9 @@ static inline u32 sb_zone_number(int shift, int mirror)
 	case 0: zone = 0; break;
 	case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
 	case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
+	default:
+		zone = 0;

Well yeah but this is not a valid case at all, we'd rather catch that as an assertion failure than letting is silently continue.

So, as all callers pass valid value. It would be
better to catch that as an assertion failure.

#3 lore raw
In reply to Khaled ROMDHANI
Quoted 17 lines · Khaled ROMDHANI

As reported by the Coverity static analysis.
The variable zone is not initialized which
may causes a failed assertion.

Addresses-Coverity: ("Uninitialized variables")

---
v2: add a default case as proposed by David Sterba

---
 fs/btrfs/zoned.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/zoned.c +0 -0
index eeb3ebe11d7a..82527308d165 100644

fs/btrfs/zoned.c static inline u32 sb_zone_number(int shift, int mirror) · line 145

Quoted 3 lines
fs/btrfs/zoned.c +0 -0
@@ -143,6 +143,9 @@ static inline u32 sb_zone_number(int shift, int mirror)
 	case 0: zone = 0; break;
 	case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
 	case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;

It took me a while to spot these break statements.

fs/btrfs/zoned.c static inline u32 sb_zone_number(int shift, int mirror) · line 148

Quoted 3 lines
fs/btrfs/zoned.c +3 -0
+	default:
+		zone = 0;
+	break;

This break needs to be indented one more tab.

fs/btrfs/zoned.c static inline u32 sb_zone_number(int shift, int mirror) · line 151

Quoted 3 lines
fs/btrfs/zoned.c +0 -0
 	}
 
 	ASSERT(zone <= U32_MAX);

regards,
dan carpenter

#4 lore raw
Quoted 30 lines · Dan Carpenter
Quoted 17 lines · Khaled ROMDHANI

As reported by the Coverity static analysis.
The variable zone is not initialized which
may causes a failed assertion.

Addresses-Coverity: ("Uninitialized variables")

---
v2: add a default case as proposed by David Sterba

---
 fs/btrfs/zoned.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/zoned.c +0 -0
index eeb3ebe11d7a..82527308d165 100644

fs/btrfs/zoned.c static inline u32 sb_zone_number(int shift, int mirror) · line 145

Quoted 3 lines
fs/btrfs/zoned.c +0 -0
@@ -143,6 +143,9 @@ static inline u32 sb_zone_number(int shift, int mirror)
 	case 0: zone = 0; break;
 	case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
 	case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;

It took me a while to spot these break statements.

fs/btrfs/zoned.c static inline u32 sb_zone_number(int shift, int mirror) · line 148

Quoted 3 lines
fs/btrfs/zoned.c +3 -0
+	default:
+		zone = 0;
+	break;

This break needs to be indented one more tab.

Quoted 6 lines

fs/btrfs/zoned.c static inline u32 sb_zone_number(int shift, int mirror) · line 151

Quoted 3 lines
fs/btrfs/zoned.c +0 -0
 	}
 
 	ASSERT(zone <= U32_MAX);

regards,
dan carpenter

Sorry, but I checked the patch using checkpatch.pl before sending it. Is that blocks some smatch parsing process?

In any cases, I will send a V3.

Thanks.