Skip to contentSkip to conversation

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

PATCH Superseded lixiaokeng 3 participants 47 messages Sep 2, 2020 – Sep 9, 2020 Device mapper

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

lixiaokeng

author
#0 lore raw

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

Hi:
Now, we check multipath-tools codes with codedex tool. Here
are some some cleanups and fixes.

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: return if dm_get_major_minor failed in cli_add_map
libmultipath: check malloc return value 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                 |  4 +++
 libmpathpersist/mpath_persist.c | 11 ++-----
 libmultipath/devmapper.c        |  2 +-
 libmultipath/dmparser.c         | 18 +++++------
 libmultipath/foreign.c          |  4 +++
 libmultipath/sysfs.c            |  6 +++-
 mpathpersist/main.c             | 56 +++++++++++++++++++++++++++------
 multipath/main.c                |  9 ++----
 multipathd/cli_handlers.c       | 17 +++++++---
 multipathd/main.c               |  8 +++--
 tests/util.c                    |  2 ++
 11 files changed, 96 insertions(+), 41 deletions(-)
author
#1 lore raw

PATCH 1/14 multipathd: return if dm_get_major_minor failed 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.

---
 multipathd/cli_handlers.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
multipathd/cli_handlers.c +3 -2
index 8db37961..2d297fd0 100644
@@ -847,11 +847,12 @@ cli_add_map (void * v, char ** reply, int * len, void * data)
 		else {
 			sprintf(dev_path, "dm-%d", minor);
 			alias = dm_mapname(major, minor);
+			if (!alias)
+				condlog(2, "%s: mapname not found for %d:%d",
+					param, major, minor);
 		}
 		/*if there is no mapname found, we first create the device*/
 		if (!alias && !count) {
-			condlog(2, "%s: mapname not found for %d:%d",
-				param, major, minor);
 			get_refwwid(CMD_NONE, param, DEV_DEVMAP,
 				    vecs->pathvec, &refwwid);
 			if (refwwid) {
#2 lore raw

Re: [PATCH 01/14] multipathd: return if dm_get_major_minor failed in, cli_add_map

Quoted 15 lines · lixiaokeng

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

---
 multipathd/cli_handlers.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
multipathd/cli_handlers.c +0 -0
index 8db37961..2d297fd0 100644

multipathd/cli_handlers.c cli_add_map (void * v, char ** reply, int * · line 847

Quoted 1 line
multipathd/cli_handlers.c +0 -0
@@ -847,11 +847,12 @@ cli_add_map (void * v, char ** reply, int *
 len, void * data)

Why not just quit the "do" loop in the error case
for dm_get_major_minor()?

Martin

Quoted 16 lines
multipathd/cli_handlers.c +3 -0
 		else {
 			sprintf(dev_path, "dm-%d", minor);
 			alias = dm_mapname(major, minor);
+			if (!alias)
+				condlog(2, "%s: mapname not found for
 %d:%d",
+					param, major, minor);
 		}
 		/*if there is no mapname found, we first create the

device*/

multipathd/cli_handlers.c +0 -2
 		if (!alias && !count) {
-			condlog(2, "%s: mapname not found for %d:%d",
-				param, major, minor);
 			get_refwwid(CMD_NONE, param, DEV_DEVMAP,
 				    vecs->pathvec, &refwwid);
 			if (refwwid) {
author
#3 lore raw

Re: [PATCH 01/14] multipathd: return if dm_get_major_minor failed in, cli_add_map

Quoted 5 lines · Martin Wilck

Why not just quit the "do" loop in the error case
for dm_get_major_minor()?

Martin

If dm_get_major failed at first time, it will be executed again for some reason I don't know in the original code. Quiting the "do" loop in the error case for dm_get_major_minor() is against the twice attempt rule.

Lixiaokeng

Quoted 22 lines
Quoted 11 lines
multipathd/cli_handlers.c +2 -0
 		else {
 			sprintf(dev_path, "dm-%d", minor);
 			alias = dm_mapname(major, minor);
+			if (!alias)
+				condlog(2, "%s: mapname not found for

%d:%d",

multipathd/cli_handlers.c +1 -0
+					param, major, minor);
 		}
 		/*if there is no mapname found, we first create the

device*/

multipathd/cli_handlers.c cli_add_map (void * v, char ** reply, int * len, void * data) · line 858

Quoted 6 lines
multipathd/cli_handlers.c +0 -2
 		if (!alias && !count) {
-			condlog(2, "%s: mapname not found for %d:%d",
-				param, major, minor);
 			get_refwwid(CMD_NONE, param, DEV_DEVMAP,
 				    vecs->pathvec, &refwwid);
 			if (refwwid) {

.

#4 lore raw

Re: [PATCH 01/14] multipathd: return if dm_get_major_minor failed in, cli_add_map

Quoted 11 lines · lixiaokeng
Quoted 4 lines · Martin Wilck

Why not just quit the "do" loop in the error case
for dm_get_major_minor()?

Martin

If dm_get_major failed at first time, it will be executed again for some reason I don't know in the original code. Quiting the "do" loop in the error case for dm_get_major_minor() is against the twice attempt rule.

Right.

Then, to solve the problem you're concerned about, it should be sufficient to initialize both major and minor to -1, or simply refrain from printing them in the log message in the first place.

At a closer look, the logic of the function is flawed; at least if get_refwwid() doesn't return a usable refwwid, there's no point in trying again.

Thanks,
Martin

Quoted 22 lines

Lixiaokeng

Quoted 20 lines
Quoted 10 lines
multipathd/cli_handlers.c +2 -0
 		else {
 			sprintf(dev_path, "dm-%d", minor);
 			alias = dm_mapname(major, minor);
+			if (!alias)
+				condlog(2, "%s: mapname not found for

%d:%d",

multipathd/cli_handlers.c +1 -0
+					param, major, minor);
 		}
 		/*if there is no mapname found, we first create the

device*/

multipathd/cli_handlers.c cli_add_map (void * v, char ** reply, int * len, void * data) · line 858

Quoted 6 lines
multipathd/cli_handlers.c +0 -2
 		if (!alias && !count) {
-			condlog(2, "%s: mapname not found for %d:%d",
-				param, major, minor);
 			get_refwwid(CMD_NONE, param, DEV_DEVMAP,
 				    vecs->pathvec, &refwwid);
 			if (refwwid) {

.

author
#5 lore raw

PATCH 2/14 libmultipath: check malloc return value in, print_foreign_topology

In reply to lixiaokeng

We check the return value of malloc in print_foreign_topology.

---
 libmultipath/foreign.c | 4 ++++
 1 file changed, 4 insertions(+)
libmultipath/foreign.c +4 -0
index e8f61351..44f32d03 100644
@@ -545,6 +545,10 @@ void print_foreign_topology(int verbosity)
 	char *buf = NULL, *tmp = NULL;
 
 	buf = malloc(buflen);
+	if (!buf) {
+		condlog (0, "malloc buf failed.");
+		return;
+	}
 	buf[0] = '\0';
 	while (buf != NULL) {
 		char *c = buf;

--

#6 lore raw

Re: [PATCH 02/14] libmultipath: check malloc return value in, print_foreign_topology

Quoted 14 lines · lixiaokeng

We check the return value of malloc in print_foreign_topology.

---
 libmultipath/foreign.c | 4 ++++
 1 file changed, 4 insertions(+)
libmultipath/foreign.c +0 -0
index e8f61351..44f32d03 100644

libmultipath/foreign.c void print_foreign_topology(int verbosity) · line 551

Quoted 7 lines
libmultipath/foreign.c +4 -0
@@ -545,6 +545,10 @@ void print_foreign_topology(int verbosity)
 	char *buf = NULL, *tmp = NULL;
 
 	buf = malloc(buflen);
+	if (!buf) {
+		condlog (0, "malloc buf failed.");
+		return;
+	}

Just replace the malloc() by calloc() please, and remove the buf[0] initialization. No need for an error message.

Martin

Quoted 5 lines
libmultipath/foreign.c +0 -1
 	buf[0] = '\0';
 	while (buf != NULL) {
 		char *c = buf;
--
author
#7 lore raw

PATCH 3/14 libmultipath: use map instead of dm_task_get_name

In reply to lixiaokeng

In dm_mapname, dm_task_get_name(dmt) has been called and the return value has been stored in map. Use map instead of second dm_task_get_name.

---
 libmultipath/devmapper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/devmapper.c +1 -1
index cc2de1df..b4d77cb6 100644
@@ -1315,7 +1315,7 @@ dm_mapname(int major, int minor)
 
 	map = dm_task_get_name(dmt);
 	if (map && strlen(map))
-		response = STRDUP((const char *)dm_task_get_name(dmt));
+		response = STRDUP((const char *)map);
 
 	dm_task_destroy(dmt);
 	return response;
Reviewed-by on v1 3/14
#8 lore raw

Re: [PATCH 03/14] libmultipath: use map instead of dm_task_get_name

Quoted 16 lines · lixiaokeng

In dm_mapname, dm_task_get_name(dmt) has been called and the return value
has been stored in map. Use map instead of second dm_task_get_name.

---
 libmultipath/devmapper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/devmapper.c +0 -0
index cc2de1df..b4d77cb6 100644

libmultipath/devmapper.c dm_mapname(int major, int minor) · line 1318

Quoted 5 lines
libmultipath/devmapper.c +1 -1
@@ -1315,7 +1315,7 @@ dm_mapname(int major, int minor)
 
 	map = dm_task_get_name(dmt);
 	if (map && strlen(map))
-		response = STRDUP((const char *)dm_task_get_name(dmt));
+		response = STRDUP((const char *)map);

dm_task_get_name() is not an expensive call. Anyway:

Quoted 3 lines
libmultipath/devmapper.c +0 -0
 	dm_task_destroy(dmt);
 	return response;
author
#9 lore raw

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

---
 multipathd/main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
multipathd/main.c +6 -2
index 67e9af11..7180d3c1 100644
@@ -3391,8 +3391,12 @@ void *  mpath_pr_event_handler_fn (void * pathp )
 		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) {
+		ret = MPATH_PR_OTHER;
+		goto out;
+	}
+
 	param->sa_flags = mpp->sa_flags;
 	memcpy(param->sa_key, &mpp->reservation_key, 8);
 	param->num_transportid = 0;
#10 lore raw

Re: [PATCH 04/14] multipathd: check MALLOC return value in, mpath_pr_event_handler_fn

Quoted 18 lines · lixiaokeng

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

---
 multipathd/main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
multipathd/main.c +0 -0
index 67e9af11..7180d3c1 100644
@@ -3391,8 +3391,12 @@ void *  mpath_pr_event_handler_fn (void *
 pathp )
 		goto out;

multipathd/main.c void * mpath_pr_event_handler_fn (void * · line 3400

Quoted 10 lines
multipathd/main.c +5 -2
 	}
 
-	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) {
+		ret = MPATH_PR_OTHER;
+		goto out;
+	}

No need to set the local variable "ret" when you jump to "out". I can see this is done elsewhere in this function, too. It has been like that since day 1.

Regards,
Martin

author
#11 lore raw

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

---
 multipathd/cli_handlers.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
multipathd/cli_handlers.c +5 -1
index 27e4574f..d345afd3 100644
@@ -1535,7 +1535,11 @@ cli_getprkey(void * v, char ** reply, int * len, void * data)
 	if (!mpp)
 		return 1;
 
-	*reply = malloc(26);
+	*reply = MALLOC(26);
+	if (!*reply) {
+		condlog(0, "malloc *reply failed.");
+		return 1;
+	}
 
 	if (!get_be64(mpp->reservation_key)) {
 		sprintf(*reply, "none\n");
#12 lore raw

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

Quoted 15 lines · lixiaokeng

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

---
 multipathd/cli_handlers.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
multipathd/cli_handlers.c +0 -0
index 27e4574f..d345afd3 100644

multipathd/cli_handlers.c cli_getprkey(void * v, char ** reply, int * · line 1543

Quoted 10 lines
multipathd/cli_handlers.c +5 -1
@@ -1535,7 +1535,11 @@ cli_getprkey(void * v, char ** reply, int *
 len, void * data)
 	if (!mpp)
 		return 1;
 
-	*reply = malloc(26);
+	*reply = MALLOC(26);
+	if (!*reply) {
+		condlog(0, "malloc *reply failed.");
+		return 1;
+	}

MALLOC is not necessary (*reply isn't left uninialized), nor is the error message.

Regards,
Martin

#13 lore raw

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

Quoted 20 lines · Martin Wilck
Quoted 19 lines · lixiaokeng

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

---
 multipathd/cli_handlers.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
multipathd/cli_handlers.c +0 -0
index 27e4574f..d345afd3 100644
@@ -1535,7 +1535,11 @@ cli_getprkey(void * v, char ** reply, int *
 len, void * data)
 	if (!mpp)
 		return 1;
 
Quoted 9 lines

multipathd/cli_handlers.c cli_getprkey(void * v, char ** reply, int * len, void * data) · line 1542

Quoted 6 lines
multipathd/cli_handlers.c +5 -1
-	*reply = malloc(26);
+	*reply = MALLOC(26);
+	if (!*reply) {
+		condlog(0, "malloc *reply failed.");
+		return 1;
+	}

MALLOC is not necessary (*reply isn't left uninialized), nor is the error message.

What's you objection to the error message? Admittedly there is basically no chance that malloc(26) would ever actually fail. But when things fail, having error messages so that we can debug them faster is helpful.

If your objection is that malloc checks are mostly just there for good form, and so those error messages won't actually help in practice, I agree. But as a general rule, I think we should print error messages on things that are unambiguoulsy errors.

-Ben

Quoted 4 lines

Regards,
Martin

#14 lore raw

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

Quoted 39 lines · Benjamin Marzinski
Quoted 22 lines · Martin Wilck
Quoted 21 lines · lixiaokeng

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

---
 multipathd/cli_handlers.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
multipathd/cli_handlers.c +0 -0
index 27e4574f..d345afd3 100644
@@ -1535,7 +1535,11 @@ cli_getprkey(void * v, char ** reply, int
 *
 len, void * data)
 	if (!mpp)
 		return 1;
 
Quoted 9 lines

multipathd/cli_handlers.c cli_getprkey(void * v, char ** reply, int * len, void * data) · line 1542

Quoted 6 lines
multipathd/cli_handlers.c +5 -1
-	*reply = malloc(26);
+	*reply = MALLOC(26);
+	if (!*reply) {
+		condlog(0, "malloc *reply failed.");
+		return 1;
+	}

MALLOC is not necessary (*reply isn't left uninialized), nor is the error message.

What's you objection to the error message? Admittedly there is basically
no chance that malloc(26) would ever actually fail. But when things fail, having error messages so that we can debug them faster is helpful.

Quoted 6 lines

If your objection is that malloc checks are mostly just there for good
form, and so those error messages won't actually help in practice, I agree. But as a general rule, I think we should print error messages on
things that are unambiguoulsy errors.

See my reply to 00/14. I'd like to standardize and streamline "out of memory" error messages, rather than hand-coding them in every procedure. I think that in 99% of cases, if multipathd crashes or errors out due to OOM, the information in which function OOM occured first will not be helpful. Even if we had a major memory leak, its unlikely that such error messages would help us find it.

Do you disagree?

Martin

author
#15 lore raw

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

---
 kpartx/kpartx.c | 4 ++++
 1 file changed, 4 insertions(+)
kpartx/kpartx.c +4 -0
index 98f6176e..2f6dea7c 100644
@@ -384,6 +384,10 @@ main(int argc, char **argv){
 
 	if (delim == NULL) {
 		delim = malloc(DELIM_SIZE);
+		if (!delim) {
+			fprintf(stderr, "malloc delim failed.\n");
+			exit(1);
+		}
 		memset(delim, 0, DELIM_SIZE);
 		set_delimiter(mapname, delim);
 	}
#16 lore raw

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

Quoted 14 lines · lixiaokeng

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

---
 kpartx/kpartx.c | 4 ++++
 1 file changed, 4 insertions(+)
kpartx/kpartx.c +0 -0
index 98f6176e..2f6dea7c 100644

kpartx/kpartx.c main(int argc, char **argv){ · line 393

Quoted 10 lines
kpartx/kpartx.c +4 -0
@@ -384,6 +384,10 @@ main(int argc, char **argv){
 
 	if (delim == NULL) {
 		delim = malloc(DELIM_SIZE);
+		if (!delim) {
+			fprintf(stderr, "malloc delim failed.\n");
+			exit(1);
+		}
 		memset(delim, 0, DELIM_SIZE);
 		set_delimiter(mapname, delim);
 	}

Please have a look at kpartx' xmalloc() function.

Regards,
Martin

author
#17 lore raw

PATCH 7/14 libmultipath: check return value of dm_mapname in, sysfs_check_holders

In reply to lixiaokeng

In sysfs_check_holders func, table_name is obtained by calling dm_mapname func, and then call dm_reassign_table for reassigning table. However, we donnot check whether dm_mapname func returns NULL, and then it may cause a segmentation fault in dm_task_set_name.

Here, we will check whether dm_mapname func returns NULL before using it.

---
 libmultipath/sysfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
libmultipath/sysfs.c +5 -1
index 12a82d95..5390de62 100644
@@ -278,7 +278,11 @@ int sysfs_check_holders(char * check_devt, char * new_devt)
 			continue;
 		}
 		table_name = dm_mapname(major, table_minor);
-
+		if (!table_name) {
+			condlog(2, "%s: mapname not found for %d:%d", check_dev,
+				major, table_minor);
+			continue;
+		}
 		condlog(0, "%s: reassign table %s old %s new %s", check_dev,
 			table_name, check_devt, new_devt);
 
maintainer Reviewed-by on v1 7/14
#18 lore raw

Re: [PATCH 07/14] libmultipath: check return value of dm_mapname in, sysfs_check_holders

Quoted 7 lines · lixiaokeng

In sysfs_check_holders func, table_name is obtained by calling dm_mapname func, and then call dm_reassign_table for reassigning table. However, we donnot check whether dm_mapname func returns NULL, and then it may cause a segmentation fault in dm_task_set_name.

Here, we will check whether dm_mapname func returns NULL before using it.

Quoted 25 lines
---
 libmultipath/sysfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
libmultipath/sysfs.c +5 -1
index 12a82d95..5390de62 100644
@@ -278,7 +278,11 @@ int sysfs_check_holders(char * check_devt, char * new_devt)
 			continue;
 		}
 		table_name = dm_mapname(major, table_minor);
-
+		if (!table_name) {
+			condlog(2, "%s: mapname not found for %d:%d", check_dev,
+				major, table_minor);
+			continue;
+		}
 		condlog(0, "%s: reassign table %s old %s new %s", check_dev,
 			table_name, check_devt, new_devt);
 
author
#19 lore raw

PATCH 8/14 libmultipath: donot free *dst if REALLOC fails in, merge_words

In reply to lixiaokeng

In merge_words func, if REALLOC() fails, the input *dst will be freed. If so, mpp->hwhandler| mpp->features|mpp->selector may be set to NULL after calling merge_words func in disassemble_map func. This may cause accessing freed memory problem.

Here, we donot free *dst if REALLOC() fails in merge_words func.

---
 libmultipath/dmparser.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
libmultipath/dmparser.c +3 -4
index c1031616..482e9d0e 100644
@@ -26,13 +26,12 @@ merge_words(char **dst, const char *word)
 
 	dstlen = strlen(*dst);
 	len = dstlen + strlen(word) + 2;
-	*dst = REALLOC(*dst, len);
+	p = REALLOC(*dst, len);
 
-	if (!*dst) {
-		free(p);
+	if (!p)
 		return 1;
-	}
 
+	*dst = p;
 	p = *dst + dstlen;
 	*p = ' ';
 	++p;
#20 lore raw

Re: [PATCH 08/14] libmultipath: donot free *dst if REALLOC fails in, merge_words

Quoted 6 lines · lixiaokeng

In merge_words func, if REALLOC() fails, the input *dst will be freed. If so, mpp->hwhandler| mpp->features|mpp->selector may be set to NULL after calling merge_words func in disassemble_map func. This may cause accessing freed memory problem.

I'm not sure that this is the right way to fix the issue you're seeing. If merge_words() frees mpp->hwhandler| mpp->features|mpp->selector, it also sets them to NULL. I don't see any place in disassemble_map() where these would be accessed if merge_words() freed them. Even with this fix, there are still cases where disassemble_map() will return 1, with these members set to NULL. If there is something that is dereferencing them without checking if they're NULL, we should fix that. But simply making them include partial information doesn't seem like it fixes anything.

Am I missing something here?

-Ben

Quoted 30 lines

Here, we donot free *dst if REALLOC() fails in merge_words func.

---
 libmultipath/dmparser.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
libmultipath/dmparser.c +3 -4
index c1031616..482e9d0e 100644
@@ -26,13 +26,12 @@ merge_words(char **dst, const char *word)
 
 	dstlen = strlen(*dst);
 	len = dstlen + strlen(word) + 2;
-	*dst = REALLOC(*dst, len);
+	p = REALLOC(*dst, len);
 
-	if (!*dst) {
-		free(p);
+	if (!p)
 		return 1;
-	}
 
+	*dst = p;
 	p = *dst + dstlen;
 	*p = ' ';
 	++p;
author
#21 lore raw

Re: [PATCH 08/14] libmultipath: donot free *dst if REALLOC fails in, merge_words

Hi Ben:
I think you are right. We should not keep them with partial information.
Thanks for your review.

-Lixiaokeng

Quoted 56 lines · Benjamin Marzinski
Quoted 6 lines · lixiaokeng

In merge_words func, if REALLOC() fails, the input *dst will be freed. If so, mpp->hwhandler| mpp->features|mpp->selector may be set to NULL after calling merge_words func in disassemble_map func. This may cause accessing freed memory problem.

I'm not sure that this is the right way to fix the issue you're seeing. If merge_words() frees mpp->hwhandler| mpp->features|mpp->selector, it also sets them to NULL. I don't see any place in disassemble_map() where these would be accessed if merge_words() freed them. Even with this fix, there are still cases where disassemble_map() will return 1, with these members set to NULL. If there is something that is dereferencing them without checking if they're NULL, we should fix that. But simply making them include partial information doesn't seem like it fixes anything.

Am I missing something here?

-Ben

Quoted 20 lines

Here, we donot free *dst if REALLOC() fails in merge_words func.

---
 libmultipath/dmparser.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
libmultipath/dmparser.c +1 -1
index c1031616..482e9d0e 100644
@@ -26,13 +26,12 @@ merge_words(char **dst, const char *word)
 
 	dstlen = strlen(*dst);
 	len = dstlen + strlen(word) + 2;
-	*dst = REALLOC(*dst, len);
+	p = REALLOC(*dst, len);
 

libmultipath/dmparser.c merge_words(char **dst, const char *word) · line 37

Quoted 10 lines
libmultipath/dmparser.c +2 -3
-	if (!*dst) {
-		free(p);
+	if (!p)
 		return 1;
-	}
 
+	*dst = p;
 	p = *dst + dstlen;
 	*p = ' ';
 	++p;

.

author
#22 lore raw

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

---
 libmultipath/dmparser.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
libmultipath/dmparser.c +6 -5
index 482e9d0e..12182bf5 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,9 +86,12 @@ 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);
+	if (!mp->features) {
+		condlog(0, "mp->features is still NULL.");
+		goto err;
+	}
 
-	APPEND(p, end, "%s %s %i %i", f, mp->hwhandler, nr_priority_groups,
+	APPEND(p, end, "%s %s %i %i", mp->features, mp->hwhandler, nr_priority_groups,
 	       initial_pg_nr);
 
 	vector_foreach_slot (mp->pg, pgp, i) {
@@ -110,12 +113,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;
 }
 
#23 lore raw

Re: [PATCH 09/14] libmultipath: check whether mp->features is NULl in, assemble_map

Quoted 27 lines · 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.

---
 libmultipath/dmparser.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
libmultipath/dmparser.c +1 -1
index 482e9d0e..12182bf5 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";

libmultipath/dmparser.c assemble_map (struct multipath * mp, char * params, int len) · line 89

Quoted 4 lines
libmultipath/dmparser.c +0 -1
@@ -86,9 +86,12 @@ 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);

clearly strdup()ing without checking if mp->features NULL is incorrect. However, I'm not sure that we need to fail if mp->features is NULL. instead, int the APPEND call, we could use the gcc ternary operator extension

(mp->features)?: "0"

to use "0" if mp->features is NULL.

Also, have you seen this actually occur? Or is this just a theoretical issue that you've found from reading the code. It seems like setup_map() will always call select_features() before calling assemble_map(), which should mean that mp->features will always be set in this case. Perhaps I'm missing something here.

-Ben

Quoted 24 lines
libmultipath/dmparser.c +5 -3
+	if (!mp->features) {
+		condlog(0, "mp->features is still NULL.");
+		goto err;
+	}
 
-	APPEND(p, end, "%s %s %i %i", f, mp->hwhandler, nr_priority_groups,
+	APPEND(p, end, "%s %s %i %i", mp->features, mp->hwhandler, nr_priority_groups,
 	       initial_pg_nr);
 
 	vector_foreach_slot (mp->pg, pgp, i) {
@@ -110,12 +113,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
#24 lore raw

Re: [PATCH 09/14] libmultipath: check whether mp->features is NULl in, assemble_map

Quoted 16 lines

libmultipath/dmparser.c assemble_map (struct multipath * mp, char * params, int len) · line 89

Quoted 6 lines
libmultipath/dmparser.c +0 -1
@@ -86,9 +86,12 @@ 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);

clearly strdup()ing without checking if mp->features NULL is incorrect. However, I'm not sure that we need to fail if mp->features is NULL. instead, int the APPEND call, we could use the gcc ternary operator extension

(mp->features)?: "0"

to use "0" if mp->features is NULL.

Quoted 8 lines

Also, have you seen this actually occur? Or is this just a theoretical issue that you've found from reading the code. It seems like setup_map() will always call select_features() before calling assemble_map(), which should mean that mp->features will always be set in this case. Perhaps I'm missing something here.

-Ben

Hi Ben,
This just a theoretical issue and I did not see it. But it's not necessary
to call strdup. In your opinion, need multipath be checked? I will make new patch with your suggestion.

-Lixiaokeng

Quoted 4 lines
Quoted 4 lines
libmultipath/dmparser.c +4 -0
+	if (!mp->features) {
+		condlog(0, "mp->features is still NULL.");
+		goto err;
+	}
#25 lore raw

Re: [PATCH 09/14] libmultipath: check whether mp->features is NULl in, assemble_map

Quoted 22 lines · lixiaokeng
Quoted 22 lines

libmultipath/dmparser.c assemble_map (struct multipath * mp, char * params, int len) · line 89

Quoted 6 lines
libmultipath/dmparser.c +0 -1
@@ -86,9 +86,12 @@ 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);

clearly strdup()ing without checking if mp->features NULL is incorrect. However, I'm not sure that we need to fail if mp->features is NULL. instead, int the APPEND call, we could use the gcc ternary operator extension

(mp->features)?: "0"

to use "0" if mp->features is NULL.

Also, have you seen this actually occur? Or is this just a theoretical issue that you've found from reading the code. It seems like setup_map() will always call select_features() before calling assemble_map(), which should mean that mp->features will always be set in this case. Perhaps I'm missing something here.

Quoted 6 lines
Quoted 2 lines

-Ben

Hi Ben,
This just a theoretical issue and I did not see it. But it's not necessary
to call strdup. In your opinion, need multipath be checked? I will make new patch with your suggestion.

Since we don't believe it's possible for mp->features (or mp->hwhandler) to be set to NULL here, it makes sense to print an error if it is NULL. So, I guess my suggestion would be to print an error message if mp->features or mp->hwhandler are NULL, but to assemble the map anyway, using the default value of "0" if they are NULL. That's how assemble_map() currently handles failures in add_feature(). add_feature() will print an error, but assemble_map() will go ahead with assembling the map.

I'm willing to be convinced that there is a better solution, however.

-Ben

Quoted 7 lines

-Lixiaokeng

Quoted 4 lines
Quoted 4 lines
libmultipath/dmparser.c +4 -0
+	if (!mp->features) {
+		condlog(0, "mp->features is still NULL.");
+		goto err;
+	}
#26 lore raw

Re: [PATCH 09/14] libmultipath: check whether mp->features is NULl in, assemble_map

Quoted 50 lines · Benjamin Marzinski
Quoted 27 lines · lixiaokeng
Quoted 27 lines

libmultipath/dmparser.c assemble_map (struct multipath * mp, char * · line 90

Quoted 6 lines
libmultipath/dmparser.c +0 -1
@@ -86,9 +86,12 @@ 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);

clearly strdup()ing without checking if mp->features NULL is incorrect.
However, I'm not sure that we need to fail if mp->features is NULL.
instead, int the APPEND call, we could use the gcc ternary operator
extension

(mp->features)?: "0"

to use "0" if mp->features is NULL.

Also, have you seen this actually occur? Or is this just a theoretical
issue that you've found from reading the code. It seems like setup_map() will always call select_features() before calling assemble_map(), which should mean that mp->features will always be set
in this case. Perhaps I'm missing something here.

Quoted 8 lines
Quoted 2 lines

-Ben

Hi Ben,
This just a theoretical issue and I did not see it. But it's not
necessary
to call strdup. In your opinion, need multipath be checked? I will make new
patch with your suggestion.

Since we don't believe it's possible for mp->features (or mp-

Quoted 1 line

hwhandler)

to be set to NULL here, it makes sense to print an error if it is NULL.
So, I guess my suggestion would be to print an error message if mp->features or mp->hwhandler are NULL, but to assemble the map anyway,
using the default value of "0" if they are NULL. That's how assemble_map() currently handles failures in add_feature(). add_feature() will print an error, but assemble_map() will go ahead with
assembling the map.

Quoted 1 line

I'm willing to be convinced that there is a better solution, however.

What about this:

assemble_map() is only called from setup_map(), which sets mp->features in select_features(). So what we should do is check for NULL after select_features(), or have that function return an error code if strdup fails, and bail out early in setup_map() in that case.

Then we simply need to add a comment in assemble_map() saying that mp->features must be non-null when this function is called.

As I said earlier, I'm of course not against checking function parameters, but here we should fail to setup a "struct multipath" in the first place in setup map(), rather than returning an incompletely initialized one. If we handle it this way, we don't need to check the fields of struct multipath over and over again. Similar arguments hold for other structs.

Of course this kind of assumption needs to be better documented in the code.

Regards
Martin

#27 lore raw

Re: [PATCH 09/14] libmultipath: check whether mp->features is NULl in, assemble_map

Quoted 70 lines · Martin Wilck
Quoted 50 lines · Benjamin Marzinski
Quoted 27 lines · lixiaokeng
Quoted 27 lines

libmultipath/dmparser.c assemble_map (struct multipath * mp, char * · line 90

Quoted 6 lines
libmultipath/dmparser.c +0 -1
@@ -86,9 +86,12 @@ 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);

clearly strdup()ing without checking if mp->features NULL is incorrect.
However, I'm not sure that we need to fail if mp->features is NULL.
instead, int the APPEND call, we could use the gcc ternary operator
extension

(mp->features)?: "0"

to use "0" if mp->features is NULL.

Also, have you seen this actually occur? Or is this just a theoretical
issue that you've found from reading the code. It seems like setup_map() will always call select_features() before calling assemble_map(), which should mean that mp->features will always be set
in this case. Perhaps I'm missing something here.

Quoted 8 lines
Quoted 2 lines

-Ben

Hi Ben,
This just a theoretical issue and I did not see it. But it's not
necessary
to call strdup. In your opinion, need multipath be checked? I will make new
patch with your suggestion.

Since we don't believe it's possible for mp->features (or mp-

Quoted 1 line

hwhandler)

to be set to NULL here, it makes sense to print an error if it is NULL.
So, I guess my suggestion would be to print an error message if mp->features or mp->hwhandler are NULL, but to assemble the map anyway,
using the default value of "0" if they are NULL. That's how assemble_map() currently handles failures in add_feature(). add_feature() will print an error, but assemble_map() will go ahead with
assembling the map.

Quoted 1 line

I'm willing to be convinced that there is a better solution, however.

What about this:

assemble_map() is only called from setup_map(), which sets mp->features in select_features(). So what we should do is check for NULL after select_features(), or have that function return an error code if strdup fails, and bail out early in setup_map() in that case.

Then we simply need to add a comment in assemble_map() saying that mp->features must be non-null when this function is called.

As I said earlier, I'm of course not against checking function parameters, but here we should fail to setup a "struct multipath" in the first place in setup map(), rather than returning an incompletely initialized one. If we handle it this way, we don't need to check the fields of struct multipath over and over again. Similar arguments hold for other structs.

Quoted 2 lines

Of course this kind of assumption needs to be better documented in the code.

I'm fine with that.

-Ben

Quoted 3 lines

Regards
Martin

author
#28 lore raw

Re: [PATCH 09/14] libmultipath: check whether mp->features is NULl in, assemble_map

In reply to Martin Wilck
Quoted 70 lines · Martin Wilck
Quoted 50 lines · Benjamin Marzinski
Quoted 27 lines · lixiaokeng
Quoted 27 lines

libmultipath/dmparser.c assemble_map (struct multipath * mp, char * · line 90

Quoted 6 lines
libmultipath/dmparser.c +0 -1
@@ -86,9 +86,12 @@ 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);

clearly strdup()ing without checking if mp->features NULL is incorrect.
However, I'm not sure that we need to fail if mp->features is NULL.
instead, int the APPEND call, we could use the gcc ternary operator
extension

(mp->features)?: "0"

to use "0" if mp->features is NULL.

Also, have you seen this actually occur? Or is this just a theoretical
issue that you've found from reading the code. It seems like setup_map() will always call select_features() before calling assemble_map(), which should mean that mp->features will always be set
in this case. Perhaps I'm missing something here.

Quoted 8 lines
Quoted 2 lines

-Ben

Hi Ben,
This just a theoretical issue and I did not see it. But it's not
necessary
to call strdup. In your opinion, need multipath be checked? I will make new
patch with your suggestion.

Since we don't believe it's possible for mp->features (or mp-

Quoted 1 line

hwhandler)

to be set to NULL here, it makes sense to print an error if it is NULL.
So, I guess my suggestion would be to print an error message if mp->features or mp->hwhandler are NULL, but to assemble the map anyway,
using the default value of "0" if they are NULL. That's how assemble_map() currently handles failures in add_feature(). add_feature() will print an error, but assemble_map() will go ahead with
assembling the map.

Quoted 1 line

I'm willing to be convinced that there is a better solution, however.

What about this:

assemble_map() is only called from setup_map(), which sets mp->features in select_features(). So what we should do is check for NULL after select_features(), or have that function return an error code if strdup fails, and bail out early in setup_map() in that case.

Then we simply need to add a comment in assemble_map() saying that mp->features must be non-null when this function is called.

As I said earlier, I'm of course not against checking function parameters, but here we should fail to setup a "struct multipath" in the first place in setup map(), rather than returning an incompletely initialized one. If we handle it this way, we don't need to check the fields of struct multipath over and over again. Similar arguments hold for other structs.

Quoted 7 lines

Of course this kind of assumption needs to be better documented in the code.

Regards
Martin

Hi Martin,
If I don't misunderstand, we check feature after select_features and
return 1 if feature is NULL in setup_map. We delete strdup and add message "mp->features must be non-null" in assemble_map. Like this:
---
select_features(conf, mpp);
if (!mpp->featrues)

	return 1;

---
/* mp->feature must not be NULL */
APPEND(p, end, "%s %s %i %i", mp->features, mp->hwhandler
---

Regards
Lixiaokeng

#29 lore raw

Re: [PATCH 09/14] libmultipath: check whether mp->features is NULl in, assemble_map

Quoted 42 lines · lixiaokeng
Quoted 24 lines · Martin Wilck

What about this:

assemble_map() is only called from setup_map(), which sets mp-

Quoted 1 line

features

in select_features(). So what we should do is check for NULL after select_features(), or have that function return an error code if strdup
fails, and bail out early in setup_map() in that case.

Then we simply need to add a comment in assemble_map() saying that mp->features must be non-null when this function is called.

As I said earlier, I'm of course not against checking function parameters, but here we should fail to setup a "struct multipath" in
the first place in setup map(), rather than returning an incompletely
initialized one. If we handle it this way, we don't need to check the
fields of struct multipath over and over again. Similar arguments hold
for other structs.

Quoted 8 lines

Of course this kind of assumption needs to be better documented in the
code.

Regards
Martin

Hi Martin,
If I don't misunderstand, we check feature after select_features
and
return 1 if feature is NULL in setup_map. We delete strdup and add message "mp->features must be non-null" in assemble_map. Like this:
---
select_features(conf, mpp);
if (!mpp->featrues)

Quoted 6 lines
	return 1;

---
/* mp->feature must not be NULL */
APPEND(p, end, "%s %s %i %i", mp->features, mp->hwhandler
---

Yes. I guess error handling in setup_map() needs a closer look, too.

Martin

author
#30 lore raw

PATCH 10/14 util/tests: use assert_non_null to ensure malloc, returns non-null pointer

In reply to lixiaokeng

In tests/util.c, we should use assert_non_null to ensure malloc() returns non-null pointer in both test_strlcpy_5 and test_strlcpy_6 func.

---
 tests/util.c | 2 ++
 1 file changed, 2 insertions(+)
tests/util.c +2 -0
index 16774dff..455eeee5 100644
@@ -523,6 +523,7 @@ static void test_strlcpy_5(void **state)
 	const int sz = sizeof(src_str);
 
 	tst = malloc(sz);
+	assert_non_null(tst);
 	memset(tst, 'f', sizeof(src_str));
 
 	rc = strlcpy(tst, src_str, sz);
@@ -540,6 +541,7 @@ static void test_strlcpy_6(void **state)
 	const int sz = sizeof(src_str);
 
 	tst = malloc(sz + 2);
+	assert_non_null(tst);
 	memset(tst, 'f', sz + 2);
 
 	rc = strlcpy(tst, src_str, sz + 2);
maintainer Reviewed-by on v1 10/14
#31 lore raw

Re: [PATCH 10/14] util/tests: use assert_non_null to ensure malloc, returns non-null pointer

Quoted 4 lines · lixiaokeng

In tests/util.c, we should use assert_non_null to ensure malloc() returns non-null pointer in both test_strlcpy_5 and test_strlcpy_6 func.

Quoted 28 lines
---
 tests/util.c | 2 ++
 1 file changed, 2 insertions(+)
tests/util.c +2 -0
index 16774dff..455eeee5 100644
@@ -523,6 +523,7 @@ static void test_strlcpy_5(void **state)
 	const int sz = sizeof(src_str);
 
 	tst = malloc(sz);
+	assert_non_null(tst);
 	memset(tst, 'f', sizeof(src_str));
 
 	rc = strlcpy(tst, src_str, sz);
@@ -540,6 +541,7 @@ static void test_strlcpy_6(void **state)
 	const int sz = sizeof(src_str);
 
 	tst = malloc(sz + 2);
+	assert_non_null(tst);
 	memset(tst, 'f', sz + 2);
 
 	rc = strlcpy(tst, src_str, sz + 2);
author
#32 lore raw

PATCH 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].

---
 mpathpersist/main.c | 55 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 46 insertions(+), 9 deletions(-)
mpathpersist/main.c +46 -9
index 28bfe410..f20c902c 100644
@@ -153,6 +153,39 @@ 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);
+	paramp = NULL;
+}
+
 static int handle_args(int argc, char * argv[], int nline)
 {
 	int c;
@@ -525,9 +558,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_transport);
+		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);
@@ -551,6 +587,12 @@ static int handle_args(int argc, char * argv[], int nline)
 			for (j = 0 ; j < num_transport; 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 +600,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)
#33 lore raw

Re: [PATCH 11/14] mpathpersist: check whether malloc paramp->trnptid_list, fails in handle_args func

Quoted 7 lines · 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].

This looks mostly fine to me. I have some minor nitpicks. But they don't actually effect the correctness of the patch.

-Ben

Quoted 36 lines
---
 mpathpersist/main.c | 55 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 46 insertions(+), 9 deletions(-)
mpathpersist/main.c +20 -0
index 28bfe410..f20c902c 100644
@@ -153,6 +153,39 @@ 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;
+}
+

mpathpersist/main.c static int do_batch_file(const char *batch_fn) · line 185

Quoted 10 lines
mpathpersist/main.c +10 -0
+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);

Setting paramp to NULL here doesn't actually do anything.

mpathpersist/main.c static int handle_args(int argc, char * argv[], int nline) · line 530

Quoted 13 lines
mpathpersist/main.c +3 -3
+	paramp = NULL;
+}
+
 static int handle_args(int argc, char * argv[], int nline)
 {
 	int c;
@@ -525,9 +558,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)));

When looking at your patch, I noticed that this function has both num_transport and num_transportids, but only num_transport is used, while num_transportids is reported back to the user. I realize that this is only tangentially related to your patch, but if you could combine these two varaiables, that would be helpful.

Quoted 37 lines
mpathpersist/main.c +13 -6
+		paramp = alloc_prout_param_descriptor(num_transport);
+		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);
@@ -551,6 +587,12 @@ static int handle_args(int argc, char * argv[], int nline)
 			for (j = 0 ; j < num_transport; 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 +600,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
#34 lore raw

Re: [PATCH 11/14] mpathpersist: check whether malloc paramp->trnptid_list, fails in handle_args func

Quoted 12 lines

mpathpersist/main.c static int handle_args(int argc, char * argv[], int nline) · line 530

Quoted 5 lines
mpathpersist/main.c +0 -3
-		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)));

When looking at your patch, I noticed that this function has both num_transport and num_transportids, but only num_transport is used, while num_transportids is reported back to the user. I realize that this is only tangentially related to your patch, but if you could combine these two varaiables, that would be helpful.

Hi Ben:
Thanks for you great advice. I will make patch v2 following your
advice.

-Lixiaokeng

author
#35 lore raw

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

---
 libmpathpersist/mpath_persist.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
libmpathpersist/mpath_persist.c +3 -8
index e7256049..046fda21 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,13 +340,9 @@ 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)
+			continue;
 
 	}
 	return MPATH_PR_SUCCESS ;
#36 lore raw

Re: [PATCH 12/14] libmultipathpersist: use update_multipath_table/status, in get_mpvec

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

Looks mostly good. I agree that we should be checking the results of getting the raw data before we try to disassemble it. But, there's not really any point to calling continue as the last operation of a loop. Perhaps

if (update_multipath_table(mpp, pathvec, DI_CHECKER) == DMP_OK)

	update_multipath_status(mpp);

makes more sense.

Quoted 37 lines
---
 libmpathpersist/mpath_persist.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
libmpathpersist/mpath_persist.c +3 -8
index e7256049..046fda21 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,13 +340,9 @@ 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)
+			continue;
 
 	}
 	return MPATH_PR_SUCCESS ;
author
#37 lore raw

Re: [PATCH 12/14] libmultipathpersist: use update_multipath_table/status, in get_mpvec

Quoted 11 lines

Looks mostly good. I agree that we should be checking the results of getting the raw data before we try to disassemble it. But, there's not really any point to calling continue as the last operation of a loop. Perhaps

if (update_multipath_table(mpp, pathvec, DI_CHECKER) == DMP_OK)

	update_multipath_status(mpp);

makes more sense.

Hi Ben,
Thanks for your review. I will change it in patch v2.

-Lixiaokeng

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

#38 lore raw

Re: [PATCH 12/14] libmultipathpersist: use update_multipath_table/status, in get_mpvec

In reply to Benjamin Marzinski
Quoted 12 lines · Benjamin Marzinski
Quoted 4 lines · 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.

Looks mostly good. I agree that we should be checking the results of getting the raw data before we try to disassemble it. But, there's not
really any point to calling continue as the last operation of a loop. Perhaps

Quoted 4 lines

if (update_multipath_table(mpp, pathvec, DI_CHECKER) == DMP_OK)

	update_multipath_status(mpp);

makes more sense.

I was thinking about this before, and wondered whether we should call remove_map() here if we encounter an error. Looking at it again and comparing with get_dm_mpvec(), I think we should.

Regards
Martin

author
#39 lore raw

PATCH 13/14 multipath: use update_multipath_table/status in, check_useable_paths

In reply to lixiaokeng

The return values of dm_get_map, disassemble_map,dm_get_status and disassemble_status in check_usable_paths were not checked. Use update_multipath_table/status to instead of them.

---
 multipath/main.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
multipath/main.c +3 -6
index d227e0b3..9e920d89 100644
@@ -251,7 +251,6 @@ static int check_usable_paths(struct config *conf,
 	struct path *pp;
 	char *mapname;
 	vector pathvec = NULL;
-	char params[PARAMS_SIZE], status[PARAMS_SIZE];
 	dev_t devt;
 	int r = 1, i, j;
 
@@ -285,11 +284,9 @@ static int check_usable_paths(struct config *conf,
 	if (mpp == NULL)
 		goto free;
 
-	dm_get_map(mpp->alias, &mpp->size, params);
-	dm_get_status(mpp->alias, status);
-	disassemble_map(pathvec, params, mpp);
-	update_pathvec_from_dm(pathvec, mpp, 0);
-	disassemble_status(status, mpp);
+	if (update_multipath_table(mpp, pathvec, 0) != DMP_OK ||
+		    update_multipath_status(mpp) != DMP_OK)
+		    goto free;
 
 	vector_foreach_slot (mpp->pg, pg, i) {
 		vector_foreach_slot (pg->paths, pp, j) {
maintainer Reviewed-by on v1 13/14
#40 lore raw

Re: [PATCH 13/14] multipath: use update_multipath_table/status in, check_useable_paths

Quoted 4 lines · lixiaokeng

The return values of dm_get_map, disassemble_map,dm_get_status and disassemble_status in check_usable_paths were not checked. Use update_multipath_table/status to instead of them.

Quoted 35 lines
---
 multipath/main.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
multipath/main.c +3 -6
index d227e0b3..9e920d89 100644
@@ -251,7 +251,6 @@ static int check_usable_paths(struct config *conf,
 	struct path *pp;
 	char *mapname;
 	vector pathvec = NULL;
-	char params[PARAMS_SIZE], status[PARAMS_SIZE];
 	dev_t devt;
 	int r = 1, i, j;
 
@@ -285,11 +284,9 @@ static int check_usable_paths(struct config *conf,
 	if (mpp == NULL)
 		goto free;
 
-	dm_get_map(mpp->alias, &mpp->size, params);
-	dm_get_status(mpp->alias, status);
-	disassemble_map(pathvec, params, mpp);
-	update_pathvec_from_dm(pathvec, mpp, 0);
-	disassemble_status(status, mpp);
+	if (update_multipath_table(mpp, pathvec, 0) != DMP_OK ||
+		    update_multipath_status(mpp) != DMP_OK)
+		    goto free;
 
 	vector_foreach_slot (mpp->pg, pg, i) {
 		vector_foreach_slot (pg->paths, pp, j) {
author
#41 lore raw

PATCH 14/14 multipathpersist: delete unused variable in handle_args

In reply to lixiaokeng

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

---
 mpathpersist/main.c | 1 -
 1 file changed, 1 deletion(-)
mpathpersist/main.c +0 -1
index f20c902c..ccf0024e 100644
@@ -213,7 +213,6 @@ static int handle_args(int argc, char * argv[], int nline)
 	int num_transport =0;
 	char *batch_fn = NULL;
 	void *resp = NULL;
-	struct transportid * tmp;
 
 	memset(transportids, 0, MPATH_MX_TIDS * sizeof(struct transportid));
 
maintainer Reviewed-by on v1 14/14
#42 lore raw

Re: [PATCH 14/14] multipathpersist: delete unused variable in handle_args

Quoted 2 lines · lixiaokeng

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

Quoted 20 lines
---
 mpathpersist/main.c | 1 -
 1 file changed, 1 deletion(-)
mpathpersist/main.c +0 -1
index f20c902c..ccf0024e 100644
@@ -213,7 +213,6 @@ static int handle_args(int argc, char * argv[], int nline)
 	int num_transport =0;
 	char *batch_fn = NULL;
 	void *resp = NULL;
-	struct transportid * tmp;
 
 	memset(transportids, 0, MPATH_MX_TIDS * sizeof(struct transportid));
 
#43 lore raw
In reply to lixiaokeng

Hello Lixiaokeng,

Quoted 3 lines · lixiaokeng

Hi:
Now, we check multipath-tools codes with codedex tool. Here
are some some cleanups and fixes.

Thank you. However I'm going to nack all patches that add error messages after unsuccesful memory allocations. Such messages are unhelpful most of the time, and increase the code size without a true benefit. I've actually considered to get rid of all these, and replace them by a log_oom() macro.

See an untested prototype attached, to better understand what I mean.

Regards
Martin

From fbbca2c5076a489ee4ae643d6d9199ca5085be95 Mon Sep 17 00:00:00 2001
From: Martin Wilck <[email protected]>
Date: Thu, 3 Sep 2020 22:03:22 +0200
Subject: [PATCH] libmultipath: prototype implementation of log_oom()

Rationale: with VERBOSE_OOM_LOGGING, we log the part of the code where OOM occured, with minimal runtime effort (no string formatting). With lots of log_oom() invocations, our binary will increase by many static strings. Without VERBOSE_OOM_LOGGING, we just print a minimal error message, which will be enough most of the time.

---
 libmultipath/debug.c | 7 +++++++
 libmultipath/debug.h | 8 ++++++++
 2 files changed, 15 insertions(+)
libmultipath/debug.c +7 -0
index 4128cb9..9062581 100644
@@ -48,3 +48,10 @@ void dlog (int sink, int prio, const char * fmt, ...)
 	}
 	va_end(ap);
 }
+
+#ifndef VERBOSE_OOM_LOGGING
+void log_oom(void)
+{
+	condlog(0, "Out of memory");
+}
+#endif
libmultipath/debug.h +8 -0
index c6120c1..f61ecb6 100644
@@ -11,3 +11,11 @@ extern int logsink;
 
 #define condlog(prio, fmt, args...) \
 	dlog(logsink, prio, fmt "\n", ##args)
+
+#ifdef VERBOSE_OOM_LOGGING
+#define __log_oom(file, line) condlog(0, "Out of memory in " file ":" #line)
+#define _log_oom(file, line) __log_oom(file, line)
+#define log_oom() _log_oom(__FILE__, __LINE__)
+#else
+void log_oom(void);
+#endif
Signature · 1 line
2.28.0
author
#44 lore raw

Hello Martin,
Thanks for your reviews. I will remove the error messages and send
them again.

Regards
Lixiaokeng

Quoted 19 lines · Martin Wilck

Hello Lixiaokeng,

Quoted 3 lines · lixiaokeng

Hi:
Now, we check multipath-tools codes with codedex tool. Here
are some some cleanups and fixes.

Thank you. However I'm going to nack all patches that add error messages after unsuccesful memory allocations. Such messages are unhelpful most of the time, and increase the code size without a true benefit. I've actually considered to get rid of all these, and replace them by a log_oom() macro.

See an untested prototype attached, to better understand what I mean.

Regards
Martin

#45 lore raw
In reply to Martin Wilck
Quoted 7 lines · Martin Wilck

Hello Lixiaokeng,

Quoted 3 lines · lixiaokeng

Hi:
Now, we check multipath-tools codes with codedex tool. Here
are some some cleanups and fixes.

Quoted 5 lines

Thank you. However I'm going to nack all patches that add error messages after unsuccesful memory allocations. Such messages are unhelpful most of the time, and increase the code size without a true benefit. I've actually considered to get rid of all these, and replace them by a log_oom() macro.

O.k. This answers my question from patch 0005. I'm fine with this.

As a side note: man, those are some ugly preprocessor hoops you need to jump through to stringify __LINE__.

-Ben

Quoted 59 lines

See an untested prototype attached, to better understand what I mean.

Regards
Martin

From fbbca2c5076a489ee4ae643d6d9199ca5085be95 Mon Sep 17 00:00:00 2001
From: Martin Wilck <[email protected]>
Date: Thu, 3 Sep 2020 22:03:22 +0200
Subject: [PATCH] libmultipath: prototype implementation of log_oom()

Rationale: with VERBOSE_OOM_LOGGING, we log the part of the code where OOM occured, with minimal runtime effort (no string formatting). With lots of log_oom() invocations, our binary will increase by many static strings. Without VERBOSE_OOM_LOGGING, we just print a minimal error message, which will be enough most of the time.

---
 libmultipath/debug.c | 7 +++++++
 libmultipath/debug.h | 8 ++++++++
 2 files changed, 15 insertions(+)
libmultipath/debug.c +7 -0
index 4128cb9..9062581 100644
@@ -48,3 +48,10 @@ void dlog (int sink, int prio, const char * fmt, ...)
 	}
 	va_end(ap);
 }
+
+#ifndef VERBOSE_OOM_LOGGING
+void log_oom(void)
+{
+	condlog(0, "Out of memory");
+}
+#endif
libmultipath/debug.h +8 -0
index c6120c1..f61ecb6 100644
@@ -11,3 +11,11 @@ extern int logsink;
 
 #define condlog(prio, fmt, args...) \
 	dlog(logsink, prio, fmt "\n", ##args)
+
+#ifdef VERBOSE_OOM_LOGGING
+#define __log_oom(file, line) condlog(0, "Out of memory in " file ":" #line)
+#define _log_oom(file, line) __log_oom(file, line)
+#define log_oom() _log_oom(__FILE__, __LINE__)
+#else
+void log_oom(void);
+#endif
Signature · 1 line
2.28.0
#46 lore raw
Quoted 16 lines · Benjamin Marzinski
Quoted 7 lines · Martin Wilck

Hello Lixiaokeng,

Quoted 3 lines · lixiaokeng

Hi:
Now, we check multipath-tools codes with codedex tool. Here
are some some cleanups and fixes.

Quoted 7 lines

Thank you. However I'm going to nack all patches that add error messages after unsuccesful memory allocations. Such messages are unhelpful most of the time, and increase the code size without a true
benefit. I've actually considered to get rid of all these, and replace
them by a log_oom() macro.

Quoted 1 line

O.k. This answers my question from patch 0005. I'm fine with this.

Great, thanks.

Quoted 3 lines

As a side note: man, those are some ugly preprocessor hoops you need to
jump through to stringify __LINE__.

Yeah, it took me some attempts to get it right.
It's actually documented:
https://gcc.gnu.org/onlinedocs/gcc-4.8.5/cpp/Stringification.html

Thanks,
Martin