Commit ce9d2b4f authored by Tom Rix's avatar Tom Rix Committed by Jens Axboe

block/rnbd-clt: Improve find_or_create_sess() return check

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: default avatarTom Rix <trix@redhat.com>
Signed-off-by: default avatarJack Wang <jinpu.wang@ionos.com>
Signed-off-by: default avatarGioh Kim <gi-oh.kim@ionos.com>
Link: https://lore.kernel.org/r/20210419073722.15351-12-gi-oh.kim@ionos.comSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent c77bfa8f
...@@ -910,6 +910,7 @@ static struct rnbd_clt_session *__find_and_get_sess(const char *sessname) ...@@ -910,6 +910,7 @@ static struct rnbd_clt_session *__find_and_get_sess(const char *sessname)
return NULL; return NULL;
} }
/* caller is responsible for initializing 'first' to false */
static struct static struct
rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first) rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first)
{ {
...@@ -925,8 +926,7 @@ rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first) ...@@ -925,8 +926,7 @@ rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first)
} }
list_add(&sess->list, &sess_list); list_add(&sess->list, &sess_list);
*first = true; *first = true;
} else }
*first = false;
mutex_unlock(&sess_lock); mutex_unlock(&sess_lock);
return sess; return sess;
...@@ -1194,13 +1194,11 @@ find_and_get_or_create_sess(const char *sessname, ...@@ -1194,13 +1194,11 @@ find_and_get_or_create_sess(const char *sessname,
struct rnbd_clt_session *sess; struct rnbd_clt_session *sess;
struct rtrs_attrs attrs; struct rtrs_attrs attrs;
int err; int err;
bool first; bool first = false;
struct rtrs_clt_ops rtrs_ops; struct rtrs_clt_ops rtrs_ops;
sess = find_or_create_sess(sessname, &first); sess = find_or_create_sess(sessname, &first);
if (sess == ERR_PTR(-ENOMEM)) if (!first)
return ERR_PTR(-ENOMEM);
else if (!first)
return sess; return sess;
if (!path_cnt) { if (!path_cnt) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment