2021-04-17 15:41:34

by Khaled Romdhani

[permalink] [raw]
Subject: [PATCH v2] 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")
Signed-off-by: Khaled ROMDHANI <[email protected]>
---
v2: add a default case as proposed by David Sterba
---
fs/btrfs/zoned.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index eeb3ebe11d7a..82527308d165 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -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);
--
2.17.1


2021-04-19 22:12:18

by David Sterba

[permalink] [raw]
Subject: Re: [PATCH v2] fs/btrfs: Fix uninitialized variable

On Sat, Apr 17, 2021 at 04:36:16PM +0100, Khaled ROMDHANI wrote:
> As reported by the Coverity static analysis.
> The variable zone is not initialized which
> may causes a failed assertion.
>
> Addresses-Coverity: ("Uninitialized variables")
> Signed-off-by: Khaled ROMDHANI <[email protected]>
> ---
> v2: add a default case as proposed by David Sterba
> ---
> fs/btrfs/zoned.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
> index eeb3ebe11d7a..82527308d165 100644
> --- a/fs/btrfs/zoned.c
> +++ b/fs/btrfs/zoned.c
> @@ -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.

2021-04-20 10:25:09

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH v2] fs/btrfs: Fix uninitialized variable

On Sat, Apr 17, 2021 at 04:36:16PM +0100, Khaled ROMDHANI wrote:
> As reported by the Coverity static analysis.
> The variable zone is not initialized which
> may causes a failed assertion.
>
> Addresses-Coverity: ("Uninitialized variables")
> Signed-off-by: Khaled ROMDHANI <[email protected]>
> ---
> v2: add a default case as proposed by David Sterba
> ---
> fs/btrfs/zoned.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
> index eeb3ebe11d7a..82527308d165 100644
> --- a/fs/btrfs/zoned.c
> +++ b/fs/btrfs/zoned.c
> @@ -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.

> + default:
> + zone = 0;
> + break;

This break needs to be indented one more tab.

> }
>
> ASSERT(zone <= U32_MAX);

regards,
dan carpenter

2021-04-20 13:22:12

by Khaled Romdhani

[permalink] [raw]
Subject: Re: [PATCH v2] fs/btrfs: Fix uninitialized variable

On Mon, Apr 19, 2021 at 07:32:25PM +0200, David Sterba wrote:
> On Sat, Apr 17, 2021 at 04:36:16PM +0100, Khaled ROMDHANI wrote:
> > As reported by the Coverity static analysis.
> > The variable zone is not initialized which
> > may causes a failed assertion.
> >
> > Addresses-Coverity: ("Uninitialized variables")
> > Signed-off-by: Khaled ROMDHANI <[email protected]>
> > ---
> > v2: add a default case as proposed by David Sterba
> > ---
> > fs/btrfs/zoned.c | 3 +++
> > 1 file changed, 3 insertions(+)
> >
> > diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
> > index eeb3ebe11d7a..82527308d165 100644
> > --- a/fs/btrfs/zoned.c
> > +++ b/fs/btrfs/zoned.c
> > @@ -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.

2021-04-20 13:42:46

by Khaled Romdhani

[permalink] [raw]
Subject: Re: [PATCH v2] fs/btrfs: Fix uninitialized variable

On Tue, Apr 20, 2021 at 01:22:14PM +0300, Dan Carpenter wrote:
> On Sat, Apr 17, 2021 at 04:36:16PM +0100, Khaled ROMDHANI wrote:
> > As reported by the Coverity static analysis.
> > The variable zone is not initialized which
> > may causes a failed assertion.
> >
> > Addresses-Coverity: ("Uninitialized variables")
> > Signed-off-by: Khaled ROMDHANI <[email protected]>
> > ---
> > v2: add a default case as proposed by David Sterba
> > ---
> > fs/btrfs/zoned.c | 3 +++
> > 1 file changed, 3 insertions(+)
> >
> > diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
> > index eeb3ebe11d7a..82527308d165 100644
> > --- a/fs/btrfs/zoned.c
> > +++ b/fs/btrfs/zoned.c
> > @@ -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.
>
> > + default:
> > + zone = 0;
> > + break;
>
> This break needs to be indented one more tab.
>
> > }
> >
> > 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.