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 – Sep 9, 2020 Device mapper

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

lixiaokeng

author
#0 lore raw

v3 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.

V3 is same as V2.

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 v3 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 v3 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 v3 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 v3 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");
#5 lore raw

Re: [PATCH V3 05/14] multipathd: use MALLOC and check return value in cli_getprkey func

Quoted 4 lines · 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

Ok, but now you need to change the patch subject as well...

Also, please don't include v1->v2 changes in the commit message itself. Once this is committed, it's not interesting how it differs from earlier versions of the patch. Put it below the "---" marker, or just mention the version history in the cover letter.

Please see
https://www.kernel.org/doc/html/v4.17/process/submitting-patches.html

Regards,
Martin

author
#6 lore raw

Re: [PATCH V3 05/14] multipathd: use MALLOC and check return value in cli_getprkey func

Quoted 13 lines · Martin Wilck
Quoted 4 lines · 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

Ok, but now you need to change the patch subject as well...

Also, please don't include v1->v2 changes in the commit message itself. Once this is committed, it's not interesting how it differs from earlier versions of the patch. Put it below the "---" marker, or just mention the version history in the cover letter.

Quoted 6 lines

Please see
https://www.kernel.org/doc/html/v4.17/process/submitting-patches.html

Regards,
Martin

I will change the message of patches 1, 2, 4, 5, 6, 7, 9, 11, 12, 14
and send them as V4.

Regards,
Lixiaokeng

author
#7 lore raw

Re: [PATCH V3 06/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);
 	}
#8 lore raw

Re: [PATCH V3 06/14] kpartx: check return value of malloc in main func

Quoted 16 lines · 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 +0 -0
index 98f6176e..6de3c9c4 100644

kpartx/kpartx.c struct pt { · line 66

Quoted 6 lines
kpartx/kpartx.c +3 -0
@@ -61,6 +61,9 @@ struct pt {
 int ptct = 0;
 int udev_sync = 1;
 
+extern void *
+xmalloc (size_t size);
+

Please don't use "extern". It's misleading, because this function is defined in the same file. You should actually change xmalloc() to a static function.

Regards,
Martin

author
#9 lore raw

Re: [PATCH V3 06/14] kpartx: check return value of malloc in main func

Quoted 28 lines · Martin Wilck
Quoted 16 lines · 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 +0 -0
index 98f6176e..6de3c9c4 100644

kpartx/kpartx.c struct pt { · line 66

Quoted 6 lines
kpartx/kpartx.c +3 -0
@@ -61,6 +61,9 @@ struct pt {
 int ptct = 0;
 int udev_sync = 1;
 
+extern void *
+xmalloc (size_t size);
+

Please don't use "extern". It's misleading, because this function is defined in the same file. You should actually change xmalloc() to a static function.

Quoted 3 lines

Regards,
Martin

Hi Martin,

The function xmalloc should be externed before main even it is changed
to static function. So do you think it is a good idea to move xmalloc before main? Or move it and change it to static func?

Regards,
Lixiaokeng

author
#10 lore raw

PATCH v3 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
#11 lore raw

PATCH v3 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
#12 lore raw

PATCH v3 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
#13 lore raw

PATCH v3 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));