* [PATCH] block/rnbd-clt: improve find_or_create_sess() return check
@ 2021-01-10 21:57 trix
2021-01-11 5:14 ` Nathan Chancellor
2021-01-11 6:57 ` Jinpu Wang
0 siblings, 2 replies; 4+ messages in thread
From: trix @ 2021-01-10 21:57 UTC (permalink / raw)
To: danil.kipnis, jinpu.wang, axboe, natechancellor, ndesaulniers
Cc: linux-block, linux-kernel, clang-built-linux, Tom Rix
From: Tom Rix <trix@redhat.com>
clang static analysis reports this problem
rnbd-clt.c:1212:11: warning: Branch condition evaluates to a
garbage value
else if (!first)
^~~~~~
This is triggered in the find_and_get_or_create_sess() call
because the variable first is not initialized and the
earlier check is specifically for
if (sess == ERR_PTR(-ENOMEM))
This is false positive.
But the if-check can be reduced by initializing first to
false and then returning if the call to find_or_creat_sess()
does not set it to true. When it remains false, either
sess will be valid or not. The not case is caught by
find_and_get_or_create_sess()'s caller rnbd_clt_map_device()
sess = find_and_get_or_create_sess(...);
if (IS_ERR(sess))
return ERR_CAST(sess);
Since find_and_get_or_create_sess() initializes first to false
setting it in find_or_create_sess() is not needed.
Signed-off-by: Tom Rix <trix@redhat.com>
---
drivers/block/rnbd/rnbd-clt.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/drivers/block/rnbd/rnbd-clt.c b/drivers/block/rnbd/rnbd-clt.c
index 96e3f9fe8241..251f747cf10d 100644
--- a/drivers/block/rnbd/rnbd-clt.c
+++ b/drivers/block/rnbd/rnbd-clt.c
@@ -919,6 +919,7 @@ static struct rnbd_clt_session *__find_and_get_sess(const char *sessname)
return NULL;
}
+/* caller is responsible for initializing 'first' to false */
static struct
rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first)
{
@@ -934,8 +935,7 @@ rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first)
}
list_add(&sess->list, &sess_list);
*first = true;
- } else
- *first = false;
+ }
mutex_unlock(&sess_lock);
return sess;
@@ -1203,13 +1203,11 @@ find_and_get_or_create_sess(const char *sessname,
struct rnbd_clt_session *sess;
struct rtrs_attrs attrs;
int err;
- bool first;
+ bool first = false;
struct rtrs_clt_ops rtrs_ops;
sess = find_or_create_sess(sessname, &first);
- if (sess == ERR_PTR(-ENOMEM))
- return ERR_PTR(-ENOMEM);
- else if (!first)
+ if (!first)
return sess;
if (!path_cnt) {
--
2.27.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] block/rnbd-clt: improve find_or_create_sess() return check
2021-01-10 21:57 [PATCH] block/rnbd-clt: improve find_or_create_sess() return check trix
@ 2021-01-11 5:14 ` Nathan Chancellor
2021-01-11 14:49 ` Tom Rix
2021-01-11 6:57 ` Jinpu Wang
1 sibling, 1 reply; 4+ messages in thread
From: Nathan Chancellor @ 2021-01-11 5:14 UTC (permalink / raw)
To: trix
Cc: danil.kipnis, jinpu.wang, axboe, ndesaulniers, linux-block,
linux-kernel, clang-built-linux
On Sun, Jan 10, 2021 at 01:57:26PM -0800, trix@redhat.com wrote:
> From: Tom Rix <trix@redhat.com>
>
> clang static analysis reports this problem
>
> rnbd-clt.c:1212:11: warning: Branch condition evaluates to a
> garbage value
> else if (!first)
> ^~~~~~
Ah, is it complaining that the 'if (IS_ERR(sess)) {' section in
find_or_create_sess() does not initialize first even though that will be
caught by the 'if (sess == ERR_PTR(-ENOMEM))' in
find_and_get_or_create_sess() because alloc_sess() only returns an
-ENOMEM error pointer?
> This is triggered in the find_and_get_or_create_sess() call
> because the variable first is not initialized and the
> earlier check is specifically for
>
> if (sess == ERR_PTR(-ENOMEM))
>
> This is false positive.
>
> But the if-check can be reduced by initializing first to
> false and then returning if the call to find_or_creat_sess()
> does not set it to true. When it remains false, either
> sess will be valid or not. The not case is caught by
> find_and_get_or_create_sess()'s caller rnbd_clt_map_device()
>
> sess = find_and_get_or_create_sess(...);
> if (IS_ERR(sess))
> return ERR_CAST(sess);
>
> Since find_and_get_or_create_sess() initializes first to false
> setting it in find_or_create_sess() is not needed.
>
> Signed-off-by: Tom Rix <trix@redhat.com>
Every maintainer has their preference for where and how stuff gets
initialized but this makes sense to me. I am not sure the commit above
find_or_create_sess() is needed but again, personal preference.
Reviewed-by: Nathan Chancellor <natechancellor@gmail.com>
> ---
> drivers/block/rnbd/rnbd-clt.c | 10 ++++------
> 1 file changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/block/rnbd/rnbd-clt.c b/drivers/block/rnbd/rnbd-clt.c
> index 96e3f9fe8241..251f747cf10d 100644
> --- a/drivers/block/rnbd/rnbd-clt.c
> +++ b/drivers/block/rnbd/rnbd-clt.c
> @@ -919,6 +919,7 @@ static struct rnbd_clt_session *__find_and_get_sess(const char *sessname)
> return NULL;
> }
>
> +/* caller is responsible for initializing 'first' to false */
> static struct
> rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first)
> {
> @@ -934,8 +935,7 @@ rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first)
> }
> list_add(&sess->list, &sess_list);
> *first = true;
> - } else
> - *first = false;
> + }
> mutex_unlock(&sess_lock);
>
> return sess;
> @@ -1203,13 +1203,11 @@ find_and_get_or_create_sess(const char *sessname,
> struct rnbd_clt_session *sess;
> struct rtrs_attrs attrs;
> int err;
> - bool first;
> + bool first = false;
> struct rtrs_clt_ops rtrs_ops;
>
> sess = find_or_create_sess(sessname, &first);
> - if (sess == ERR_PTR(-ENOMEM))
> - return ERR_PTR(-ENOMEM);
> - else if (!first)
> + if (!first)
> return sess;
>
> if (!path_cnt) {
> --
> 2.27.0
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] block/rnbd-clt: improve find_or_create_sess() return check
2021-01-10 21:57 [PATCH] block/rnbd-clt: improve find_or_create_sess() return check trix
2021-01-11 5:14 ` Nathan Chancellor
@ 2021-01-11 6:57 ` Jinpu Wang
1 sibling, 0 replies; 4+ messages in thread
From: Jinpu Wang @ 2021-01-11 6:57 UTC (permalink / raw)
To: Tom Rix, Nathan Chancellor
Cc: Danil Kipnis, Jens Axboe, ndesaulniers, linux-block, open list,
clang-built-linux
On Sun, Jan 10, 2021 at 10:58 PM <trix@redhat.com> wrote:
>
> From: Tom Rix <trix@redhat.com>
>
> clang static analysis reports this problem
>
> rnbd-clt.c:1212:11: warning: Branch condition evaluates to a
> garbage value
> else if (!first)
> ^~~~~~
>
> This is triggered in the find_and_get_or_create_sess() call
> because the variable first is not initialized and the
> earlier check is specifically for
>
> if (sess == ERR_PTR(-ENOMEM))
>
> This is false positive.
>
> But the if-check can be reduced by initializing first to
> false and then returning if the call to find_or_creat_sess()
> does not set it to true. When it remains false, either
> sess will be valid or not. The not case is caught by
> find_and_get_or_create_sess()'s caller rnbd_clt_map_device()
>
> sess = find_and_get_or_create_sess(...);
> if (IS_ERR(sess))
> return ERR_CAST(sess);
>
> Since find_and_get_or_create_sess() initializes first to false
> setting it in find_or_create_sess() is not needed.
>
> Signed-off-by: Tom Rix <trix@redhat.com>
Less LOC is better :)
Acked-by: Jack Wang <jinpu.wang@cloud.ionos.com>
Thanks Tom and Nathan!
> ---
> drivers/block/rnbd/rnbd-clt.c | 10 ++++------
> 1 file changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/block/rnbd/rnbd-clt.c b/drivers/block/rnbd/rnbd-clt.c
> index 96e3f9fe8241..251f747cf10d 100644
> --- a/drivers/block/rnbd/rnbd-clt.c
> +++ b/drivers/block/rnbd/rnbd-clt.c
> @@ -919,6 +919,7 @@ static struct rnbd_clt_session *__find_and_get_sess(const char *sessname)
> return NULL;
> }
>
> +/* caller is responsible for initializing 'first' to false */
> static struct
> rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first)
> {
> @@ -934,8 +935,7 @@ rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first)
> }
> list_add(&sess->list, &sess_list);
> *first = true;
> - } else
> - *first = false;
> + }
> mutex_unlock(&sess_lock);
>
> return sess;
> @@ -1203,13 +1203,11 @@ find_and_get_or_create_sess(const char *sessname,
> struct rnbd_clt_session *sess;
> struct rtrs_attrs attrs;
> int err;
> - bool first;
> + bool first = false;
> struct rtrs_clt_ops rtrs_ops;
>
> sess = find_or_create_sess(sessname, &first);
> - if (sess == ERR_PTR(-ENOMEM))
> - return ERR_PTR(-ENOMEM);
> - else if (!first)
> + if (!first)
> return sess;
>
> if (!path_cnt) {
> --
> 2.27.0
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] block/rnbd-clt: improve find_or_create_sess() return check
2021-01-11 5:14 ` Nathan Chancellor
@ 2021-01-11 14:49 ` Tom Rix
0 siblings, 0 replies; 4+ messages in thread
From: Tom Rix @ 2021-01-11 14:49 UTC (permalink / raw)
To: Nathan Chancellor
Cc: danil.kipnis, jinpu.wang, axboe, ndesaulniers, linux-block,
linux-kernel, clang-built-linux
On 1/10/21 9:14 PM, Nathan Chancellor wrote:
> On Sun, Jan 10, 2021 at 01:57:26PM -0800, trix@redhat.com wrote:
>> From: Tom Rix <trix@redhat.com>
>>
>> clang static analysis reports this problem
>>
>> rnbd-clt.c:1212:11: warning: Branch condition evaluates to a
>> garbage value
>> else if (!first)
>> ^~~~~~
> Ah, is it complaining that the 'if (IS_ERR(sess)) {' section in
> find_or_create_sess() does not initialize first even though that will be
> caught by the 'if (sess == ERR_PTR(-ENOMEM))' in
> find_and_get_or_create_sess() because alloc_sess() only returns an
> -ENOMEM error pointer?
Reviewing the code, failure looks like it returns only -ENOMEM.
So the check is correct but brittle.
>
>> This is triggered in the find_and_get_or_create_sess() call
>> because the variable first is not initialized and the
>> earlier check is specifically for
>>
>> if (sess == ERR_PTR(-ENOMEM))
>>
>> This is false positive.
>>
>> But the if-check can be reduced by initializing first to
>> false and then returning if the call to find_or_creat_sess()
>> does not set it to true. When it remains false, either
>> sess will be valid or not. The not case is caught by
>> find_and_get_or_create_sess()'s caller rnbd_clt_map_device()
>>
>> sess = find_and_get_or_create_sess(...);
>> if (IS_ERR(sess))
>> return ERR_CAST(sess);
>>
>> Since find_and_get_or_create_sess() initializes first to false
>> setting it in find_or_create_sess() is not needed.
>>
>> Signed-off-by: Tom Rix <trix@redhat.com>
> Every maintainer has their preference for where and how stuff gets
> initialized but this makes sense to me. I am not sure the commit above
> find_or_create_sess() is needed but again, personal preference.
Mostly this removes two unneeded branches at the cost of initializing a variable.
Secondary, the static analysis complaint is resolved.
Tom
>
> Reviewed-by: Nathan Chancellor <natechancellor@gmail.com>
>
>> ---
>> drivers/block/rnbd/rnbd-clt.c | 10 ++++------
>> 1 file changed, 4 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/block/rnbd/rnbd-clt.c b/drivers/block/rnbd/rnbd-clt.c
>> index 96e3f9fe8241..251f747cf10d 100644
>> --- a/drivers/block/rnbd/rnbd-clt.c
>> +++ b/drivers/block/rnbd/rnbd-clt.c
>> @@ -919,6 +919,7 @@ static struct rnbd_clt_session *__find_and_get_sess(const char *sessname)
>> return NULL;
>> }
>>
>> +/* caller is responsible for initializing 'first' to false */
>> static struct
>> rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first)
>> {
>> @@ -934,8 +935,7 @@ rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first)
>> }
>> list_add(&sess->list, &sess_list);
>> *first = true;
>> - } else
>> - *first = false;
>> + }
>> mutex_unlock(&sess_lock);
>>
>> return sess;
>> @@ -1203,13 +1203,11 @@ find_and_get_or_create_sess(const char *sessname,
>> struct rnbd_clt_session *sess;
>> struct rtrs_attrs attrs;
>> int err;
>> - bool first;
>> + bool first = false;
>> struct rtrs_clt_ops rtrs_ops;
>>
>> sess = find_or_create_sess(sessname, &first);
>> - if (sess == ERR_PTR(-ENOMEM))
>> - return ERR_PTR(-ENOMEM);
>> - else if (!first)
>> + if (!first)
>> return sess;
>>
>> if (!path_cnt) {
>> --
>> 2.27.0
>>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-01-11 14:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-10 21:57 [PATCH] block/rnbd-clt: improve find_or_create_sess() return check trix
2021-01-11 5:14 ` Nathan Chancellor
2021-01-11 14:49 ` Tom Rix
2021-01-11 6:57 ` Jinpu Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).