Skip to contentSkip to conversation

multipath-tools series: some cleanups and fixes checked by codedex tool

PATCH Superseded lixiaokeng 2 participants 14 messages Sep 8, 2020 Device mapper

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

lixiaokeng

author
#0 lore raw

v2 cover multipath-tools series: some cleanups and fixes checked by codedex tool

Patches 01, 02, 04, 05, 06, 09, 11, 12, 14 have some changes.

Changes in V2:
- patch 01: set major and minor to -1 at begining, as suggested by Martin Wilck
- patch 02: chanege malloc to calloc to calloc, as suggested by Martin Wilck
- patch 04: delete seting ret when jump to out, as suggested by Martin Wilck
- patch 05: turn back MALLOC to malloc and reomve error message, as suggested by Martin Wilck
- patch 06: change malloc to xmalloc, as suggested by Martin Wilck
- patch 09: use ?: instead of checking mp->features, as suggested by Benjamin Marzinski
- patch 11: change num_transport to num_transportids to combine them, as suggested by Benjamin Marzinski
- patch 12: delete continue and add remove_map, as suggested by Martin Wilck
- patch 14: modify patch because of patch 11 "mpathpersist: check whether malloc paramp->trnptid_list fails in handle_args func" changing

Zhiqiang Liu (7):
multipathd: use MALLOC and check return value in cli_getprkey func
kpartx: check return value of malloc in main func
libmultipath: check return value of dm_mapname in sysfs_check_holders
libmultipath: donot free *dst if REALLOC fails in merge_words
libmultipath: check whether mp->features is NUll in assemble_map util/tests: use assert_non_null to ensure malloc returns non-null
pointer
mpathpersist: check whether malloc paramp->trnptid_list fails in
handle_args func

lixiaokeng (7):
multipathd: initialize major and minor in cli_add_map
libmultipath: change malloc to calloc in print_foreign_topology
libmultipath: use map instead of dm_task_get_name
multipathd: check MALLOC return value in mpath_pr_event_handler_fn
libmultipathpersist: use update_multipath_table/status in get_mpvec
multipath: use update_multipath_table/status in check_useable_paths
multipathpersist: delete unused variable in handle_args

 kpartx/kpartx.c                 |  5 ++-
 libmpathpersist/mpath_persist.c | 15 +++-----
 libmultipath/devmapper.c        |  2 +-
 libmultipath/dmparser.c         | 17 +++------
 libmultipath/foreign.c          |  4 +-
 libmultipath/sysfs.c            |  6 ++-
 mpathpersist/main.c             | 66 +++++++++++++++++++++++++--------
 multipath/main.c                |  9 ++---
 multipathd/cli_handlers.c       |  4 +-
 multipathd/main.c               |  8 ++--
 tests/util.c                    |  2 +
 11 files changed, 86 insertions(+), 52 deletions(-)

--

author
#1 lore raw

PATCH v2 1/14 multipathd: initialize major and minor in cli_add_map

If dm_get_major_minor failed, log with major and minor should not be printed to avoid major and minor used before initialization.

V1->V2: set major and minor to -1 at begining

---
 multipathd/cli_handlers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
multipathd/cli_handlers.c +1 -1
index 8db37961..e9698704 100644
@@ -820,7 +820,7 @@ cli_add_map (void * v, char ** reply, int * len, void * data)
 {
        struct vectors * vecs = (struct vectors *)data;
        char * param = get_keyparam(v, MAP);
-       int major, minor;
+       int major = -1, minor = -1;
        char dev_path[PATH_SIZE];
        char *refwwid, *alias = NULL;
        int rc, count = 0;

--

author
#2 lore raw

PATCH v2 2/14 libmultipath: change malloc to calloc in print_foreign_topology

In reply to lixiaokeng

We check the return value of malloc in print_foreign_topology.

V1->V2: chanege malloc to calloc

---
 libmultipath/foreign.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libmultipath/foreign.c +2 -2
index e8f61351..fce19347 100644
@@ -544,8 +544,8 @@ void print_foreign_topology(int verbosity)
        int buflen = MAX_LINE_LEN * MAX_LINES;
        char *buf = NULL, *tmp = NULL;
 
-       buf = malloc(buflen);
-       buf[0] = '\0';
+       buf = calloc(1, buflen);
+
        while (buf != NULL) {
                char *c = buf;
 

--

author
#3 lore raw

PATCH v2 4/14 multipathd: check MALLOC return value in mpath_pr_event_handler_fn

In reply to lixiaokeng

In mpath_pr_event_handler_fn, we use MALLOC instead of malloc, and check the return value of MALLOC.

V1->V2: delete seting ret when jump to out

---
 multipathd/main.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
multipathd/main.c +4 -4
index 67e9af11..f1264459 100644
@@ -3365,7 +3365,6 @@ void *  mpath_pr_event_handler_fn (void * pathp )
        if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
        {
                condlog(1, "%s: No key found. Device may not be registered.", pp->dev);
-               ret = MPATH_PR_SUCCESS;
                goto out;
        }
        condlog(2, "Multipath  reservation_key: 0x%" PRIx64 " ",
@@ -3387,12 +3386,13 @@ void *  mpath_pr_event_handler_fn (void * pathp )
        {
                condlog(0, "%s: Either device not registered or ", pp->dev);
                condlog(0, "host is not authorised for registration. Skip path");
-               ret = MPATH_PR_OTHER;
                goto out;
        }
 
-       param= malloc(sizeof(struct prout_param_descriptor));
-       memset(param, 0 , sizeof(struct prout_param_descriptor));
+       param = (struct prout_param_descriptor *)MALLOC(sizeof(struct prout_param_descriptor));
+       if (!param)
+               goto out;
+
        param->sa_flags = mpp->sa_flags;
        memcpy(param->sa_key, &mpp->reservation_key, 8);
        param->num_transportid = 0;

--

author
#4 lore raw

PATCH v2 5/14 multipathd: use MALLOC and check return value in cli_getprkey func

In reply to lixiaokeng

In cli_getprkey func, we use MALLOC instead of malloc, and check the return value of MALLOC.

V1->V2: turn back MALLOC to malloc and reomve error message

---
 multipathd/cli_handlers.c | 2 ++
 1 file changed, 2 insertions(+)
multipathd/cli_handlers.c +2 -0
index e9698704..235e2a2e 100644
@@ -1531,6 +1531,8 @@ cli_getprkey(void * v, char ** reply, int * len, void * data)
                return 1;
 
        *reply = malloc(26);
+       if (!*reply)
+               return 1;
 
        if (!get_be64(mpp->reservation_key)) {
                sprintf(*reply, "none\n");

--

author
#5 lore raw

PATCH v2 6/14 kpartx: check return value of malloc in main func

In reply to lixiaokeng

In main func of kpartx.c, we should check return value of malloc before using it.

V1->V2: change malloc to xmalloc

---
 kpartx/kpartx.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kpartx/kpartx.c +4 -1
index 98f6176e..6de3c9c4 100644
@@ -61,6 +61,9 @@ struct pt {
 int ptct = 0;
 int udev_sync = 1;
 
+extern void *
+xmalloc (size_t size);
+
 static void
 addpts(char *t, ptreader f)
 {
@@ -383,7 +386,7 @@ main(int argc, char **argv){
                mapname = device + off;
 
        if (delim == NULL) {
-               delim = malloc(DELIM_SIZE);
+               delim = xmalloc(DELIM_SIZE);
                memset(delim, 0, DELIM_SIZE);
                set_delimiter(mapname, delim);
        }

--

author
#6 lore raw

PATCH v2 9/14 libmultipath: check whether mp->features is NUll in assemble_map

In reply to lixiaokeng

In assemble_map func, if add_feature fails and mp->features is default value (NULL), STRDUP(mp->features) will cause a seg-fault. In addition, f = STRDUP(mp->features) is just used for APPEND(). We can directly pass mp->features to APPEND().

Here, we firstly check whether mp->features is null.

V1->V2: use ?: instead of checking mp->features

---
 libmultipath/dmparser.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
libmultipath/dmparser.c +3 -7
index 482e9d0e..3c5e016a 100644
@@ -65,7 +65,7 @@ assemble_map (struct multipath * mp, char * params, int len)
        int i, j;
        int minio;
        int nr_priority_groups, initial_pg_nr;
-       char * p, * f;
+       char * p;
        const char *const end = params + len;
        char no_path_retry[] = "queue_if_no_path";
        char retain_hwhandler[] = "retain_attached_hw_handler";
@@ -86,10 +86,8 @@ assemble_map (struct multipath * mp, char * params, int len)
            get_linux_version_code() < KERNEL_VERSION(4, 3, 0))
                add_feature(&mp->features, retain_hwhandler);
 
-       f = STRDUP(mp->features);
-
-       APPEND(p, end, "%s %s %i %i", f, mp->hwhandler, nr_priority_groups,
-              initial_pg_nr);
+       APPEND(p, end, "%s %s %i %i", mp->features ? mp->features : 0, mp->hwhandler,
+               nr_priority_groups, initial_pg_nr);
 
        vector_foreach_slot (mp->pg, pgp, i) {
                pgp = VECTOR_SLOT(mp->pg, i);
@@ -110,12 +108,10 @@ assemble_map (struct multipath * mp, char * params, int len)
                }
        }
 
-       FREE(f);
        condlog(4, "%s: assembled map [%s]", mp->alias, params);
        return 0;
 
 err:
-       FREE(f);
        return 1;
 }
 

--

author
#7 lore raw

PATCH v2 11/14 mpathpersist: check whether malloc paramp->trnptid_list fails in handle_args func

In reply to lixiaokeng

In handle_args func, we donot check whether malloc paramp and each paramp->trnptid_list[j] fails before using them, it may cause access NULL pointer.

Here, we add alloc_prout_param_descriptor to allocate and init paramp, and we add free_prout_param_descriptor to free paramp and each paramp->trnptid_list[j].

V1->V2: change num_transport to num_transportids to combine them.

---
 mpathpersist/main.c | 65 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 50 insertions(+), 15 deletions(-)
mpathpersist/main.c +50 -15
index 28bfe410..da67c15c 100644
@@ -153,6 +153,38 @@ static int do_batch_file(const char *batch_fn)
        return ret;
 }
 
+static struct prout_param_descriptor *
+alloc_prout_param_descriptor(int num_transportid)
+{
+       struct prout_param_descriptor *paramp;
+
+       if (num_transportid < 0 || num_transportid > MPATH_MX_TIDS)
+               return NULL;
+
+       paramp= malloc(sizeof(struct prout_param_descriptor) +
+                               (sizeof(struct transportid *) * num_transportid));
+
+       if (!paramp)
+               return NULL;
+
+       paramp->num_transportid = num_transportid;
+       memset(paramp, 0, sizeof(struct prout_param_descriptor) +
+                       (sizeof(struct transportid *) * num_transportid));
+       return paramp;
+}
+
+static void free_prout_param_descriptor(struct prout_param_descriptor *paramp)
+{
+       int i;
+       if (!paramp)
+               return;
+
+       for (i = 0; i < paramp->num_transportid; i++)
+               free(paramp->trnptid_list[i]);
+
+       free(paramp);
+}
+
 static int handle_args(int argc, char * argv[], int nline)
 {
        int c;
@@ -177,7 +209,6 @@ static int handle_args(int argc, char * argv[], int nline)
        int prin = 1;
        int prin_sa = -1;
        int prout_sa = -1;
-       int num_transport =0;
        char *batch_fn = NULL;
        void *resp = NULL;
        struct transportid * tmp;
@@ -334,13 +365,13 @@ static int handle_args(int argc, char * argv[], int nline)
                                break;
 
                        case 'X':
-                               if (0 != construct_transportid(optarg, transportids, num_transport)) {
+                               if (0 != construct_transportid(optarg, transportids, num_transportids)) {
                                        fprintf(stderr, "bad argument to '--transport-id'\n");
                                        ret = MPATH_PR_SYNTAX_ERROR;
                                        goto out;
                                }
 
-                               ++num_transport;
+                               ++num_transportids;
                                break;
 
                        case 'l':
@@ -525,9 +556,12 @@ static int handle_args(int argc, char * argv[], int nline)
                int j;
                struct prout_param_descriptor *paramp;
 
-               paramp= malloc(sizeof(struct prout_param_descriptor) + (sizeof(struct transportid *)*(MPATH_MX_TIDS )));
-
-               memset(paramp, 0, sizeof(struct prout_param_descriptor) + (sizeof(struct transportid *)*(MPATH_MX_TIDS)));
+               paramp = alloc_prout_param_descriptor(num_transportids);
+               if (!paramp) {
+                       fprintf(stderr, "malloc paramp failed\n");
+                       ret = MPATH_PR_OTHER;
+                       goto out_fd;
+               }
 
                for (j = 7; j >= 0; --j) {
                        paramp->key[j] = (param_rk & 0xff);
@@ -544,13 +578,19 @@ static int handle_args(int argc, char * argv[], int nline)
                if (param_aptpl)
                        paramp->sa_flags |= MPATH_F_APTPL_MASK;
 
-               if (num_transport)
+               if (num_transportids)
                {
                        paramp->sa_flags |= MPATH_F_SPEC_I_PT_MASK;
-                       paramp->num_transportid = num_transport;
-                       for (j = 0 ; j < num_transport; j++)
+                       paramp->num_transportid = num_transportids;
+                       for (j = 0 ; j < num_transportids; j++)
                        {
                                paramp->trnptid_list[j] = (struct transportid *)malloc(sizeof(struct transportid));
+                               if (!paramp->trnptid_list[j]) {
+                                       fprintf(stderr, "malloc paramp->trnptid_list[%d] failed.\n", j);
+                                       ret = MPATH_PR_OTHER;
+                                       free_prout_param_descriptor(paramp);
+                                       goto out_fd;
+                               }
                                memcpy(paramp->trnptid_list[j], &transportids[j],sizeof(struct transportid));
                        }
                }
@@ -558,12 +598,7 @@ static int handle_args(int argc, char * argv[], int nline)
                /* PROUT commands other than 'register and move' */
                ret = __mpath_persistent_reserve_out (fd, prout_sa, 0, prout_type,
                                paramp, noisy);
-               for (j = 0 ; j < num_transport; j++)
-               {
-                       tmp = paramp->trnptid_list[j];
-                       free(tmp);
-               }
-               free(paramp);
+               free_prout_param_descriptor(paramp);
        }
 
        if (ret != MPATH_PR_SUCCESS)

--

author
#8 lore raw

PATCH v2 12/14 libmultipathpersist: use update_multipath_table/status in get_mpvec

In reply to lixiaokeng

The return values of dm_get_map, disassemble_map in get_mpvec were not checked. Use update_multipath_table/status to instead of them.

V1->V2: delete continue and add remove_map

---
 libmpathpersist/mpath_persist.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
libmpathpersist/mpath_persist.c +6 -9
index e7256049..1f9817ed 100644
@@ -323,7 +323,6 @@ get_mpvec (vector curmp, vector pathvec, char * refwwid)
 {
        int i;
        struct multipath *mpp;
-       char params[PARAMS_SIZE], status[PARAMS_SIZE];
 
        vector_foreach_slot (curmp, mpp, i){
                /*
@@ -341,14 +340,12 @@ get_mpvec (vector curmp, vector pathvec, char * refwwid)
                if (refwwid && strncmp (mpp->alias, refwwid, WWID_SIZE - 1))
                        continue;
 
-               dm_get_map(mpp->alias, &mpp->size, params);
-               condlog(3, "params = %s", params);
-               dm_get_status(mpp->alias, status);
-               condlog(3, "status = %s", status);
-               disassemble_map (pathvec, params, mpp);
-               update_pathvec_from_dm(pathvec, mpp, DI_CHECKER);
-               disassemble_status (status, mpp);
-
+               if (update_multipath_table(mpp, pathvec, DI_CHECKER) != DMP_OK ||
+                   update_multipath_status(mpp) != DMP_OK) {
+                       condlog(1, "error parsing map %s", mpp->wwid);
+                       remove_map(mpp, pathvec, curmp, PURGE_VEC);
+                       i--;
+               }
        }
        return MPATH_PR_SUCCESS ;
 }

--

author
#9 lore raw

PATCH v2 14/14 multipathpersist: delete unused variable in handle_args

In reply to lixiaokeng

In handle_args, the tmp isn't used. We delete it.

V1->V2: modify patch because of "mpathpersist: check whether malloc paramp->trnptid_list fails in handle_args func" changing

---
 mpathpersist/main.c | 1 -
 1 file changed, 1 deletion(-)
mpathpersist/main.c +0 -1
index da67c15c..85453ac2 100644
@@ -211,7 +211,6 @@ static int handle_args(int argc, char * argv[], int nline)
        int prout_sa = -1;
        char *batch_fn = NULL;
        void *resp = NULL;
-       struct transportid * tmp;
 
        memset(transportids, 0, MPATH_MX_TIDS * sizeof(struct transportid));
 

--

#10 lore raw
In reply to lixiaokeng
Quoted 1 line · lixiaokeng

Patches 01, 02, 04, 05, 06, 09, 11, 12, 14 have some changes.

Thanks.
What about No. 08? Did you drop it? If yes, please say so.

Regards,
Martin

author
#11 lore raw
Quoted 9 lines · Martin Wilck
Quoted 1 line · lixiaokeng

Patches 01, 02, 04, 05, 06, 09, 11, 12, 14 have some changes.

Thanks.
What about No. 08? Did you drop it? If yes, please say so.

Regards,
Martin

Hi Martin,
I drop No. 08. I'm sorry for forgetting to say that.

Regards,
Lixiaokeng

#12 lore raw
In reply to lixiaokeng

Hello lixiaokeng,

Quoted 1 line · lixiaokeng

Patches 01, 02, 04, 05, 06, 09, 11, 12, 14 have some changes.

I have problems applying your patches. It seems to me that the whitespace has been messed up by your mail program (tab indentation has been replaced by spaces).

The v1 series you sent was OK, but v2 is different.

Regards,
Martin

Quoted 58 lines

Changes in V2:
- patch 01: set major and minor to -1 at begining, as suggested by Martin Wilck
- patch 02: chanege malloc to calloc to calloc, as suggested by Martin Wilck
- patch 04: delete seting ret when jump to out, as suggested by Martin Wilck
- patch 05: turn back MALLOC to malloc and reomve error message, as suggested by Martin Wilck
- patch 06: change malloc to xmalloc, as suggested by Martin Wilck
- patch 09: use ?: instead of checking mp->features, as suggested by Benjamin Marzinski
- patch 11: change num_transport to num_transportids to combine them, as suggested by Benjamin Marzinski
- patch 12: delete continue and add remove_map, as suggested by Martin Wilck
- patch 14: modify patch because of patch 11 "mpathpersist: check whether malloc paramp->trnptid_list fails in handle_args func" changing

Zhiqiang Liu (7):
multipathd: use MALLOC and check return value in cli_getprkey func
kpartx: check return value of malloc in main func
libmultipath: check return value of dm_mapname in
sysfs_check_holders
libmultipath: donot free *dst if REALLOC fails in merge_words
libmultipath: check whether mp->features is NUll in assemble_map util/tests: use assert_non_null to ensure malloc returns non-null
pointer
mpathpersist: check whether malloc paramp->trnptid_list fails in
handle_args func

lixiaokeng (7):
multipathd: initialize major and minor in cli_add_map
libmultipath: change malloc to calloc in print_foreign_topology
libmultipath: use map instead of dm_task_get_name
multipathd: check MALLOC return value in mpath_pr_event_handler_fn
libmultipathpersist: use update_multipath_table/status in get_mpvec
multipath: use update_multipath_table/status in check_useable_paths
multipathpersist: delete unused variable in handle_args

 kpartx/kpartx.c                 |  5 ++-
 libmpathpersist/mpath_persist.c | 15 +++-----
 libmultipath/devmapper.c        |  2 +-
 libmultipath/dmparser.c         | 17 +++------
 libmultipath/foreign.c          |  4 +-
 libmultipath/sysfs.c            |  6 ++-
 mpathpersist/main.c             | 66 +++++++++++++++++++++++++----
----
 multipath/main.c                |  9 ++---
 multipathd/cli_handlers.c       |  4 +-
 multipathd/main.c               |  8 ++--
 tests/util.c                    |  2 +
 11 files changed, 86 insertions(+), 52 deletions(-)

--

author
#13 lore raw
Quoted 5 lines · Martin Wilck

Hello lixiaokeng,

Quoted 1 line · lixiaokeng

Patches 01, 02, 04, 05, 06, 09, 11, 12, 14 have some changes.

Quoted 10 lines

I have problems applying your patches. It seems to me that the whitespace has been messed up by your mail program (tab indentation has been replaced by spaces).

The v1 series you sent was OK, but v2 is different.

Regards,
Martin

Hello Martin,

I will check why it happens and send patches v3.

-Lixiaokeng