diff mbox

[4/9] multipathd: don't call strlen on NULL variables

Message ID 1491545798-22183-5-git-send-email-bmarzins@redhat.com (mailing list archive)
State Not Applicable, archived
Delegated to: christophe varoqui
Headers show

Commit Message

Benjamin Marzinski April 7, 2017, 6:16 a.m. UTC
strlen has undefined results when passed a NULL variable, so don't do
it.

Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
---
 multipathd/main.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

Comments

Martin Wilck April 13, 2017, 2:12 p.m. UTC | #1
On Fri, 2017-04-07 at 01:16 -0500, Benjamin Marzinski wrote:
> strlen has undefined results when passed a NULL variable, so don't do
> it.
> 
> Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
> ---

This is certainly correct. Yet I have two remarks: 

1) There are many more calls to strlen() in the multipath-tools code
which would need to be likewise protected.

2) If STRDUP("ok\n") returns NULL, we're likely to be so hosed that we
might as well call abort() anyway (which is a philosophy that I
personally quite like - multipathd is not such a vital part of the
system that it can't risk dying).

Regards
Martin

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel
diff mbox

Patch

diff --git a/multipathd/main.c b/multipathd/main.c
index 283d81d..f671d58 100644
--- a/multipathd/main.c
+++ b/multipathd/main.c
@@ -1076,7 +1076,8 @@  uxsock_trigger (char * str, char ** reply, int * len, bool is_root,
 	    (strncmp(str, "list", strlen("list")) != 0) &&
 	    (strncmp(str, "show", strlen("show")) != 0)) {
 		*reply = STRDUP("permission deny: need to be root");
-		*len = strlen(*reply) + 1;
+		if (*reply)
+			*len = strlen(*reply) + 1;
 		return 1;
 	}
 
@@ -1087,12 +1088,14 @@  uxsock_trigger (char * str, char ** reply, int * len, bool is_root,
 			*reply = STRDUP("timeout\n");
 		else
 			*reply = STRDUP("fail\n");
-		*len = strlen(*reply) + 1;
+		if (*reply)
+			*len = strlen(*reply) + 1;
 		r = 1;
 	}
 	else if (!r && *len == 0) {
 		*reply = STRDUP("ok\n");
-		*len = strlen(*reply) + 1;
+		if (*reply)
+			*len = strlen(*reply) + 1;
 		r = 0;
 	}
 	/* else if (r < 0) leave *reply alone */